Skip to content

Commit 8aee08c

Browse files
authored
Merge pull request crazycodeboy#11 from WT-Road/master
原生模块调用(iOS)
2 parents 4514f72 + 02b8379 commit 8aee08c

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed

React Native 每日一学/Readme.md

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
5. [D5:React Native setNativeProps使用 (2016-8-24)](#d5react-native-setnativeprops使用2016-8-24)
1414
6. [D6:ref属性不只是string(2016-8-25)](#d6ref属性不只是string2016-8-25)
1515
7. [D7:解构赋值(Destructuring assignment)(2016-8-26)](#d7解构赋值destructuring-assignment2016-8-26)
16+
8. [D8:React-Native 原生模块调用(iOS) (2016-8-29)](#d8react-native-原生模块调用(iOS)-2016-8-29)
1617

1718
```
1819
模板:
@@ -26,6 +27,54 @@ D1:标题 (日期)
2627
另外:记得在列表中添加链接
2728
```
2829

30+
D8:React-Native 原生模块调用(iOS) (2016-8-29)
31+
------
32+
在项目中遇到地图,拨打电话,清除缓存等iOS与Andiorid机制不同的功能,就需要调用原生的界面或模块,这里说下React Native调用iOS原生模块,Andiorid也是大同小异
33+
###1.创建原生模块,实现“RCTBridgeModule”协议
34+
```
35+
#import <Foundation/Foundation.h>
36+
#import "RCTBridgeModule.h"
37+
38+
@interface KSDMapManager : NSObject <RCTBridgeModule>
39+
40+
@end
41+
```
42+
###2 导出模块,导出方法
43+
```
44+
@implementation KSDMapManager
45+
//导出模块
46+
RCT_EXPORT_MODULE();
47+
48+
RCT_EXPORT_METHOD(gotoIM:(RCTResponseSenderBlock)callback)
49+
{
50+
__weak typeof(self) weakSelf = self;
51+
self.callback = callback;
52+
53+
UIViewController *controller = (UIViewController*)[[[UIApplication sharedApplication] keyWindow] rootViewController];
54+
KSDMapLocationViewController *mapVc = [[KSDMapLocationViewController alloc] init];
55+
mapVc.handle = ^(NSString *address) {
56+
weakSelf.itemValue = address;
57+
NSArray *events = [[NSArray alloc] initWithObjects:self.itemValue, nil];
58+
callback(events);
59+
};
60+
[controller presentViewController:mapVc animated:YES completion:nil];
61+
}
62+
63+
```
64+
65+
###3 js文件中调用
66+
```
67+
//创建原生模块实例
68+
var KSDMapManager = NativeModules.KSDMapManager;
69+
//方法调用
70+
KSDMapManager.gotoIM(
71+
(events)=>{
72+
this._inputReceiveAddress(events);
73+
console.log(events);
74+
})
75+
76+
```
77+
2978
D7:解构赋值([Destructuring assignment][0])(2016-8-26)
3079
------
3180
解构赋值语法是JavaScript的一种表达式,可以方便的从数组或者对象中快速提取值赋给定义的变量。

0 commit comments

Comments
 (0)