简体   繁体   English

将一个字符串从Obj-C传递给React Native

[英]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. 我目前正在React Native中构建一个应用程序,面临一个我似乎无法在我的RN框架中解决的问题,我可能需要提一下,我对Objective-C非常新。 However, I managed to get it by calling it in my AppDelegate.m file. 但是,我设法通过在AppDelegate.m文件中调用它来获取它。 But how can I pass this string variable that I created in my appdelegate to my javascript files? 但是,如何将我在appdelegate中创建的字符串变量传递给我的javascript文件?

I took a quick gander at the NativeAppEventEmitter, but it seems to be something different. 我快速地看了一下NativeAppEventEmitter,但它似乎有所不同。 In my AppDelegate.m it looks like this: 在我的AppDelegate.m中它看起来像这样:

- (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. 看看RN给出的NativeAppEventEmitter技巧,我试图实现它。 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; 我在AppDelegate.h中将其声明为: @property (nonatomic) NSString *bridge;

But when I try to utilise the eventDispatcher it gives me the following: 但是当我尝试使用eventDispatcher它给了我以下内容:

在此输入图像描述

You can pass some data when starting an app using using initialProperties . 使用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 . 您可以在RCTRootView上传递令牌,然后从props获取它。 There is a good example in RN docs . RN文档中有一个很好的例子。 Alternatively you can send an event with embedded data. 或者,您可以发送包含嵌入数据的事件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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