简体   繁体   中英

Passing a string from Obj-C to React Native

I am currently building an app in React Native and faced an issue which I seemed not to be able to resolve in my RN framewok, and I probably need to mention that I am very new with Objective-C. However, I managed to get it by calling it in my AppDelegate.m file. But how can I pass this string variable that I created in my appdelegate to my javascript files?

I took a quick gander at the NativeAppEventEmitter, but it seems to be something different. In my AppDelegate.m it looks like this:

- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {
  BOOL handled = [[FBSDKApplicationDelegate sharedInstance] application:application
                                                                openURL:url
                                                      sourceApplication:sourceApplication
                                                             annotation:annotation
                  ];
  NSString *fbAccessToken = [FBSDKAccessToken currentAccessToken].tokenString;
  return handled;
}

Looking at the NativeAppEventEmitter trick given by RN I have tried to implement it. But it complains alot about my bridge.

#import "AppDelegate.h"

#import "RCTRootView.h"

#import <FBSDKCoreKit/FBSDKCoreKit.h>

#import "RCTBridge.h"

#import "RCTBridgeModule.h"

#import "RCTEventDispatcher.h"

@implementation AppDelegate

RCT_EXPORT_MODULE();

@synthesize bridge = _bridge;

Where I in my AppDelegate.h declare it as: @property (nonatomic) NSString *bridge;

But when I try to utilise the eventDispatcher it gives me the following:

在此输入图像描述

You can pass some data when starting an app using using initialProperties . I think it's the best solution in your case. You can pass your token on the RCTRootView and then grab it from props . There is a good example in RN docs . Alternatively you can send an event with embedded data.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM