-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathindex.ios.js
126 lines (126 loc) · 3.69 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
119
120
121
122
123
124
125
126
/**
* Sample React Native App
* https://github.com/facebook/react-native
*/
import React,{
AppRegistry,
Component,
StyleSheet,
Text,
View,
TouchableHighlight,
} from 'react-native';
///进行导入NativeModules中的CalendarManger模块
import { NativeModules } from 'react-native';
import { NativeAppEventEmitter } from 'react-native';
var subscription;
var CalendarManager = NativeModules.CalendarManager;
class CustomButton extends React.Component {
render() {
return (
<TouchableHighlight
style={styles.button}
underlayColor="#a5a5a5"
onPress={this.props.onPress}>
<Text style={styles.buttonText}>{this.props.text}</Text>
</TouchableHighlight>
);
}
}
class ModulesDemo extends Component {
constructor(props){
super(props);
this.state={
events:'',
notice:'',
}
}
componentDidMount(){
console.log('开始订阅通知...');
subscription = NativeAppEventEmitter.addListener(
'EventReminder',
(reminder) => console.log('通知信息:'+reminder.name)
);
}
componentWillUnmount(){
subscription.remove();
}
//获取Promise对象处理
async _updateEvents(){
try{
var events=await CalendarManager.findEventsPromise();
this.setState({events});
}catch(e){
console.error(e);
}
}
render() {
return (
<View style={{marginTop:20}}>
<Text style={styles.welcome}>
封装iOS原生模块实例
</Text>
<CustomButton text="点击调用原生模块addEvent方法"
onPress={()=>CalendarManager.addEvent('生日聚会', '江苏南通 中天路')}
/>
<CustomButton text="点击调用原生模块addEventMoreDate方法"
onPress={()=>CalendarManager.addEventMoreDate('生日聚会', '江苏南通 中天路',1463987752)}
/>
<CustomButton text="调用原生模块addEventMoreDetails方法-传入字段格式"
onPress={()=>CalendarManager.addEventMoreDetails('生日聚会', {
location:'江苏 南通市 中天路',
time:1463987752,
description:'请一定准时来哦~'
})}
/>
<Text style={{marginLeft:5}}>
'Callback的返回数据为:'+{this.state.events}
</Text>
<CustomButton text="点击调用原生模块findEvents方法-Callback"
onPress={()=>CalendarManager.findEvents((error,events)=>{
if(error){
console.error(error);
}else{
this.setState({events:events,});
}
}
)}
/>
<CustomButton text="点击调用原生模块findEventsPromise方法-Promise"
onPress={()=>CalendarManager.findEvents((error,events)=>{
if(error){
console.error(error);
}else{
this.setState({events:events,});
}
}
)}
/>
<Text style={{marginLeft:5}}>
'静态数据为:'+{CalendarManager.firstDayOfTheWeek}
</Text>
<Text style={{marginLeft:5}}>
'发送通知信息:'+{this.state.notice}
</Text>
<CustomButton text="点击调用原生模块sendNotification方法"
onPress={()=>CalendarManager.sendNotification('准备发送通知...')}
/>
</View>
);
}
}
const styles = StyleSheet.create({
welcome: {
fontSize: 20,
textAlign: 'center',
margin: 10,
},
button: {
margin:5,
backgroundColor: 'white',
padding: 10,
borderWidth:1,
borderColor: '#cdcdcd',
},
});
AppRegistry.registerComponent('ModulesDemo', () => ModulesDemo);