React-Native 與iOS通信

? ? ? ? 這里我要簡(jiǎn)單的說(shuō)一下,由于react-native不太完善,所以有些涉及到調(diào)用原生這塊對(duì)于初學(xué)者來(lái)說(shuō)簡(jiǎn)直太傷了。今天我給大家分享的是怎樣從react跳轉(zhuǎn)到ios原生界面,我今天為了這個(gè)卡了一天,話不多說(shuō),直接上代碼。純手打。。。。

1.現(xiàn)在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.接下來(lái)我們創(chuàng)建個(gè)NSObject對(duì)象

MyVc.h

MyVc.m

這里需要注意的是,必須得在主線程push,其實(shí)你不加這個(gè)到時(shí)候也會(huì)報(bào)錯(cuò)。

#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調(diào)用




最后就不上動(dòng)圖啦,希望能幫助大家!?

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容