Reputation: 671
I'm very new to React Native (iOS) and I have read a lot about Bridging, however I cannot seem to make this working.
I need to be able to send an object/string from AppDelegate to JavaScript. My AppDelegate returns a UserName which is retrieved by a native code in one of my methods inside AppDelegate.
I tried to use in AppDelegate.m
#import "RCTBridge.h"
#import "RCTEventDispatcher.h"
and then
@synthesize bridge = _bridge;
and inside my method
NSString *eventName = notification.userInfo[@"name"];
[self.bridge.eventDispatcher sendAppEventWithName:@"EventReminder"
body:@{@"name": eventName}];
but none of the above works and I'm getting errors. Do I need to do anything to AppDelegate.h
as well?
Can anyone please help me? What is the easiest way to sent data from native to javascript?
UPDATE:
I found a workaround, but I don't think it's effective as I'm calling RCTRootView
twice. First to load the app with initialProperties
nil, like this:
- (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];
}
And for the second time when I retrieve a UserName I then update initialProperties
like this:
-(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];
}
By doing the way above I'm then able to send and receive username via this.props
in JavaScript.
But like I said, I'm not sure this is an effective way as I'm calling RCTRootView
twice.
Any help is appreciated please.
Upvotes: 1
Views: 2483
Reputation: 5643
You could, instead of trying to put it in the initial launch code on the iOS side, put it in a Native Module that you export a function to return the information from. See React Natives official documentation on it: https://facebook.github.io/react-native/docs/native-modules-ios.html
Upvotes: 2