-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ios.js
118 lines (112 loc) · 3.51 KB
/
index.ios.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/**
* Sample React Native App
* https://github.com/facebook/react-native
* @flow
*/
import React, { Component } from 'react';
import {
AppRegistry,
StyleSheet,
Text,
View,
TouchableHighlight
} from 'react-native';
var { NativeModules } = require('react-native');
var RNBridgeModule=NativeModules.RNBridgeModule;
import { NativeAppEventEmitter } from 'react-native';
var subscription; //订阅者
class CustomButton extends Component {
render() {
return (
<TouchableHighlight
style={styles.button}
underlayColor="#a5a5a5"
onPress={this.props.onPress}>
<Text style={styles.buttonText}>{this.props.text}</Text>
</TouchableHighlight>
);
}
}
class RNIOS extends Component {
constructor(props){
super(props);
this.state={
events:'',
msg:'',
}
}
//获取Promise对象处理
async _updateEvents(){
try{
var events=await RNBridgeModule.RNInvokeOCPromise({'name':'syc'});
this.setState({events});
}catch(e){
this.setState({events:e.message});
}
}
componentDidMount(){
console.log('开始添加通知--监听IOS原生方法...');
subscription = NativeAppEventEmitter.addListener(
'EventReminder',
(reminder) => {
let errorCode=reminder.errorCode;
if(errorCode===0){
this.setState({msg:reminder.name});
}else{
this.setState({msg:reminder.msg});
}
}
);
}
componentWillUnmount(){
subscription.remove();
}
render() {
return (
<View style={{marginTop:20}}>
<Text style={styles.welcome}>
混合与RN,iOS通信实例讲解
</Text>
<Text style={{margin:20}}>
来自:syc
</Text>
<Text style={{margin:5}}>'返回数据为:'+{this.state.events}</Text>
<CustomButton text='RN调用iOS原生方法_CallBack回调'
onPress={()=>{RNBridgeModule.RNInvokeOCCallBack(
{'name':'syc','description':'https://www.jianshu.com/u/e3af96eb1430'},
(error,events)=>{
if(error){
console.error(error);
}else{
this.setState({events:events});
}
})}}
/>
<CustomButton text='RN调用iOS原生方法_Promise回调'
onPress={()=>this._updateEvents()}
/>
<Text style={{margin:20}}>
'返回数据为:'+{this.state.msg}
</Text>
<CustomButton text='iOS调用访问React Native'
onPress={()=>RNBridgeModule.VCOpenRN({'name':'sycq'})}
/>
</View>
);
}
}
const styles = StyleSheet.create({
welcome: {
fontSize: 20,
textAlign: 'center',
margin: 10,
},
button: {
margin:5,
backgroundColor: 'white',
padding: 10,
borderWidth: 1,
borderColor: '#facece',
},
});
AppRegistry.registerComponent('RNIOS', () => RNIOS);