【发布时间】:2018-05-31 03:28:17
【问题描述】:
编辑
为了澄清,我试图在我的应用程序中打开电子邮件中的附件。因此,数据需要在应用程序初始化以及打开时传递给 React。
原创
我对移动应用开发非常陌生,但我正在尝试通过 ios 传递文件名以响应本机。
应用启动时传递props的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 源代码找到一种方法(尽管我是 Objective C 的新手并且不完全理解代码)。我的直觉是我必须用新的道具重新渲染视图。
我不认为这是正确的,但我尝试创建一个新的 rootview 并将其提供给 rootViewController,但我收到了“TypeError: expected dynamic type not null/object/array’, but had typenull”。
- (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;
}
谁能指出我正确的方向?提前致谢!
【问题讨论】:
-
我认为你过于复杂了。如果你使用 Native Modules,你可以从你的 JS 代码中调用一个 ObjC 函数。在这种情况下,您不需要访问 RCTRootView 或 AppDelegate,并且可以减少需要编写的 ObjC 代码量
-
@dentemm 感谢您的回复。我在下面的回复中添加了评论,并附有后续问题
标签: ios objective-c react-native