React-Native 與iOS通信

? ? ? ? 這里我要簡單的說一下,由于react-native不太完善,所以有些涉及到調用原生這塊對于初學者來說簡直太傷了。今天我給大家分享的是怎樣從react跳轉到ios原生界面,我今天為了這個卡了一天,話不多說,直接上代碼。純手打。。。。

1.現在app.m讓appdelegate成為視圖根控制器

#import "AppDelegate.h"

#import "RCTBundleURLProvider.h"

#import "RCTRootView.h"

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

{

NSURL *jsCodeLocation;

jsCodeLocation = [[RCTBundleURLProvider sharedSettings] jsBundleURLForBundleRoot:@"index.ios" fallbackResource:nil];

RCTRootView *rootView = [[RCTRootView alloc] initWithBundleURL:jsCodeLocation

moduleName:@"youname"

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];

self.rootViewController = [UIViewController new];

self.rootViewController.view = rootView;

//? self.navi = [[UINavigationController alloc]initWithRootViewController:self.rootViewController];

self.window.rootViewController = self.rootViewController;

[self.window makeKeyAndVisible];

return YES;

}

@end

2.接下來我們創建個NSObject對象

MyVc.h

MyVc.m

這里需要注意的是,必須得在主線程push,其實你不加這個到時候也會報錯。

#import "MyVc.h"

#import "RCTBridge.h"

#import "NothingVC.h"

#import "AppDelegate.h"

@implementation MyVc

RCT_EXPORT_MODULE()

- (void)show {

NothingVC *vc = [[NothingVC alloc]init];

AppDelegate *delegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];

//要在主線程

dispatch_async(dispatch_get_main_queue(), ^{

[delegate.rootViewController presentViewController:vc animated:YES completion:nil];

});

}

RCT_EXPORT_METHOD(showNew) {

[self show];

}

@end

3.配置要push的vc

需要在初始化里寫

- (void)viewDidLoad {

[super viewDidLoad];

// Do any additional setup after loading the view.

self.view.backgroundColor = [UIColor redColor];

}

4.最后一步要到我們js調用




最后就不上動圖啦,希望能幫助大家!?

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容