Edit
为了澄清,我试图在我的应用程序中打开电子邮件中的附件 . 因此,数据需要在应用程序初始化时以及打开时传递到React .
Original
我是移动应用程序开发人员的新手,但我正在尝试通过ios传递文件名以反应原生 .
用于在应用启动时传递道具的RN代码
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
NSURL *jsCodeLocation;
jsCodeLocation = [[RCTBundleURLProvider sharedSettings] jsBundleURLForBundleRoot:@"index" fallbackResource:nil];
RCTRootView *rootView = [[RCTRootView alloc] initWithBundleURL:jsCodeLocation
moduleName:@"driveby"
initialProperties:nil
launchOptions:launchOptions];
rootView.backgroundColor = [[UIColor alloc] initWithRed:1.0f green:1.0f blue:1.0f alpha:1];
self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
UIViewController *rootViewController = [UIViewController new];
rootViewController.view = rootView;
self.window.rootViewController = rootViewController;
[self.window makeKeyAndVisible];
return YES;
}
但是,此方法似乎只在应用程序启动时执行 . 当我们打开文件时,我们需要使用 openFile
方法 . openURL方法正确获取文件的URL,但我无法弄清楚如何将其传递给RCTRootView .
如果我能在rootViewController中编辑道具,我会很高兴,但是我找不到通过阅读RN源代码来实现这一点的方法(虽然我是目标C的新手并且不完全理解代码) . 我的预感是我必须用新道具重新渲染视图 .
我没有't think it'是正确的,但我试图创建一个新的rootview并将其提供给rootViewController,但我得到了一个“TypeError:期望的动态类型 not null/object/array’, but had type
null” .
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
NSURL *jsCodeLocation;
NSDictionary *urlDict = [NSDictionary dictionaryWithObjectsAndKeys: @"url", url, nil];
NSLog(@"####################### Open URL");
NSLog(@"Open URL:\t%@\n" "From source:\t%@\n" "With annotation:%@", url, sourceApplication, annotation); //url is shown here!
//NSString *filepath = [url path];
//...
jsCodeLocation = [[RCTBundleURLProvider sharedSettings] jsBundleURLForBundleRoot:@"index" fallbackResource:nil];
RCTRootView *rootView = [[RCTRootView alloc] initWithBundleURL:jsCodeLocation
moduleName:@"driveby"
initialProperties:urlDict
launchOptions:urlDict];
rootView.backgroundColor = [[UIColor alloc] initWithRed:1.0f green:1.0f blue:1.0f alpha:1];
self.window.rootViewController.view = rootView;
[self.window makeKeyAndVisible];
return YES;
}
谁能指出我正确的方向?提前致谢!
1 回答
在React Native中,您在JavaScript代码和本机代码之间传递信息的机制是通过本机模块 .
您可以创建一个桥接模块,并从JS端调用该特定方法,以便为您的视图提供您指定的URL .
另一方面,您还可以使用事件 Launcher 向JS端的事件处理程序发出URL,然后可以在收到事件时打开该文件 .
看看here,这应该可以帮助您满足您的需求 .