我对Reactive原住民(iOS)非常陌生,我读过很多关于桥接的文章,但是我似乎无法让它发挥作用。
我需要能够将对象/字符串从AppDelegate发送到JavaScript。我的AppDelegate返回一个UserName,它是由本机代码在AppDelegate中的一个方法中检索的。
我试着在AppDelegate.m中使用
#import "RCTBridge.h"
#import "RCTEventDispatcher.h"然后
@synthesize bridge = _bridge;在我的方法里
NSString *eventName = notification.userInfo[@"name"];
[self.bridge.eventDispatcher sendAppEventWithName:@"EventReminder"
body:@{@"name": eventName}];但上述任何一种方法都不起作用,而且我也会犯错误。我也需要对AppDelegate.h做些什么吗?
有人能帮帮我吗?从本机发送数据到javascript的最简单方法是什么?
更新:
我找到了一个解决办法,但我不认为它是有效的,因为我给RCTRootView打了两次电话。首先使用initialProperties 0加载应用程序,如下所示:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
NSURL *jsCodeLocation;
jsCodeLocation = [NSURL URLWithString:@"http://localhost:8081/index.ios.bundle?platform=ios&dev=true"];
RCTRootView *rootView = [[RCTRootView alloc] initWithBundleURL:jsCodeLocation
moduleName:@"myApp"
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];
[self getUsername];
}第二次当我检索UserName时,我会像这样更新initialProperties:
-(void) getUsername {
//3rd party code to retrieve the username
NSString *username = username
NSURL *jsCodeLocation;
jsCodeLocation = [NSURL URLWithString:@"http://localhost:8081/index.ios.bundle?platform=ios&dev=true"];
RCTBridge *bridge = [[RCTBridge alloc] initWithBundleURL:jsCodeLocation moduleProvider:nil launchOptions:nil];
RCTRootView *rootView = [[RCTRootView alloc] initWithBridge:bridge
moduleName:@"myApp"
initialProperties: @{@"username" : username}];
self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
UIViewController *rootViewController = [UIViewController new];
rootViewController.view = rootView;
self.window.rootViewController = rootViewController;
[self.window makeKeyAndVisible];
}通过这样做,我就可以通过this.props在JavaScript中发送和接收用户名。
但是就像我说的,我不确定这是一种有效的方法,因为我要给RCTRootView打两次电话。
如有任何帮助,请见谅。
发布于 2016-07-28 10:53:31
您可以不尝试将其放在iOS端的初始启动代码中,而是将其放在一个本地模块中,然后导出一个函数来返回信息。参见React原住民关于它的官方文档:https://facebook.github.io/react-native/docs/native-modules-ios.html
https://stackoverflow.com/questions/38593549
复制相似问题