react-native-my-native-module
v1.0.1
Published
## Getting started
Downloads
3
Readme
react-native-my-native-module
Getting started
$ npm install react-native-my-native-module --save
Mostly automatic installation
$ react-native link react-native-my-native-module
Manual installation
iOS
- In XCode, in the project navigator, right click
Libraries
➜Add Files to [your project's name]
- Go to
node_modules
➜react-native-my-native-module
and addRNMyNativeModule.xcodeproj
- In XCode, in the project navigator, select your project. Add
libRNMyNativeModule.a
to your project'sBuild Phases
➜Link Binary With Libraries
- Open info.plist, in the project navigator, add
<key>NSLocationWhenInUseUsageDescription</key> <string></string>
- Run your project (
Cmd+R
)<
Android
- Append the following lines to
android/settings.gradle
:include ':react-native-my-native-module' project(':react-native-my-native-module').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-my-native-module/android')
- Insert the following lines inside the dependencies block in
android/app/build.gradle
:compile project(':react-native-my-native-module')
- Insert the following lines inside the dependencies block in
android/app/build.gradle
:compile 'com.google.android.gms:play-services-location:x.y.z'
- with x.y.z is version you want to add, suggest version higher 11.0.0
- Open up
android/app/src/main/java/[...]/MainActivity.java
- Add
import com.reactlibrary.RNMyNativeModulePackage;
to the imports at the top of the file - Add
new RNMyNativeModulePackage()
to the list returned by thegetPackages()
method
- Add permision for location follow open
android/app/src/main/AndroidManifest.xml
- Add
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
- Add
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
Usage
import RNMyNativeModule from 'react-native-my-native-module';
// TODO: What to do with the module?
RNMyNativeModule;
const MyModule = new NativeEventEmitter(RNMyNativeModule);
....
regiterEvent() {
Plaftform.OS ==='ios' && MyModule.addListener('authStatusDidChange', status => {
console.log('=======> status', status);
alert('status auth location thay doi nay!', status);
});
MyModule.addListener('didUpdateLocation', location => {
this.setState({
newLocation: location,
});
});
}
RNMyNativeModule.getTimeStamp()
.then(timestamp => {
this.setState({ timestamp });
})
.catch(e => alert('Can not get timestapm'));
Medthod
- helloworld
- type: callback -param: callback(string)
- getTimeStamp -type: promise -resolve(Number) -- current timestamp -reject(Object) -- Error
- checkPermissions -type: callback -param: callback(string) +ios: 'authorizedAlways','authorizedWhenInUse','denied','notDetermined','restricted' +android: 'granted', 'denied'
- checkEnableLocationService -type: callback -param: callback(boolen) #ios
- requestWhenInUseAuth
- requestAlwaysAuth #android
- openLocationServiceActivity -note: when locationservice (GPS) turn off open setting!
- startListenLoctionChange
- note: started listen location change
- stopListenLocationChange -type: promise -resolve(true) -- turn off listener loction change -reject(false) -- Error
Event emiter
- didUpdateLocation
- note: emit when loction changed
- result: object
#ios
- authStatusDidChange
- note: emit when user change setting loction service
- result: string ('authorizedAlways','authorizedWhenInUse','denied','notDetermined','restricted')