Mathieu Acthernoene 7d868238da Update example to 0.50 | 7 yıl önce | |
---|---|---|
.github | 7 yıl önce | |
ReactNativePermissions.xcodeproj | 7 yıl önce | |
example | 7 yıl önce | |
permissions | 7 yıl önce | |
.gitignore | 7 yıl önce | |
.npmignore | 7 yıl önce | |
.prettierignore | 7 yıl önce | |
.prettierrc | 7 yıl önce | |
LICENSE | 8 yıl önce | |
RCTConvert+RNPStatus.h | 7 yıl önce | |
RCTConvert+RNPStatus.m | 8 yıl önce | |
README.md | 7 yıl önce | |
ReactNativePermissions.h | 7 yıl önce | |
ReactNativePermissions.m | 7 yıl önce | |
ReactNativePermissions.podspec | 7 yıl önce | |
index.android.js | 7 yıl önce | |
index.ios.js | 7 yıl önce | |
index.js | 7 yıl önce | |
package.json | 7 yıl önce | |
yarn.lock | 7 yıl önce |
Request user permissions from React Native, iOS + Android
Version | React Native Support |
---|---|
1.0.1 | 0.40.0 - 0.48.4 |
0.2.5 | 0.33.0 - 0.39.0 |
Complies with react-native-version-support-table
PermissionsAndroid
module on Android, which
is great because we no longer have to do any additional linking on AndroidPermissionsAndroid
openSettings()
support on Android (to stay linking-free). There are
several NPM modules available for thisrestricted
status now supported on Android, although it means something
different than iOSnpm install --save react-native-permissions
# --- or ---
yarn add react-native-permissions
📌 Don’t forget to add permissions to AndroidManifest.xml
for android and
Info.plist
for iOS (Xcode >= 8). See notes below for more details.
Update the following line with your path to node_modules/
and add it to your
podfile:
pod 'ReactNativePermissions', :path => '../node_modules/react-native-permissions'
react-native link react-native-permissions
Add Files to <...>
node_modules
➜ react-native-permissions
➜ select
ReactNativePermissions.xcodeproj
libReactNativePermissions.a
to Build Phases
-> Link Binary With
Libraries
import Permissions from 'react-native-permissions'
//...
export default class extends React.Component {
//...
// Check the status of a single permission
componentDidMount() {
Permissions.check('photo').then(response => {
// Response is one of: 'authorized', 'denied', 'restricted', or 'undetermined'
this.setState({ photoPermission: response })
})
}
// Request permission to access photos
_requestPermission = () => {
Permissions.request('photo').then(response => {
// Returns once the user has chosen to 'allow' or to 'not allow' access
// Response is one of: 'authorized', 'denied', 'restricted', or 'undetermined'
this.setState({ photoPermission: response })
})
}
// Check the status of multiple permissions
_checkCameraAndPhotos = () => {
Permissions.checkMultiple(['camera', 'photo']).then(response => {
//response is an object mapping type to permission
this.setState({
cameraPermission: response.camera,
photoPermission: response.photo,
})
})
}
// This is a common pattern when asking for permissions.
// iOS only gives you once chance to show the permission dialog,
// after which the user needs to manually enable them from settings.
// The idea here is to explain why we need access and determine if
// the user will say no, so that we don't blow our one chance.
// If the user already denied access, we can ask them to enable it from settings.
_alertForPhotosPermission() {
Alert.alert(
'Can we access your photos?',
'We need access so you can set your profile pic',
[
{
text: 'No way',
onPress: () => console.log('Permission denied'),
style: 'cancel',
},
this.state.photoPermission == 'undetermined'
? { text: 'OK', onPress: this._requestPermission }
: { text: 'Open Settings', onPress: Permissions.openSettings },
],
)
}
//...
}
Promises resolve into one of these statuses:
Return value | Notes |
---|---|
authorized |
User has authorized this permission |
denied |
User has denied this permission at least once. On iOS this means that the user will not be prompted again. Android users can be promted multiple times until they select ‘Never ask me again’ |
restricted |
iOS - this means user is not able to grant this permission, either because it’s not supported by the device or because it has been blocked by parental controls. Android - this means that the user has selected ‘Never ask me again’ while denying permission |
undetermined |
User has not yet been prompted with a permission dialog |
The current supported permissions are:
Type | iOS | Android | |
---|---|---|---|
Location | location |
✔️ | ✔ |
Camera | camera |
✔️ | ✔ |
Microphone | microphone |
✔️ | ✔ |
Photos | photo |
✔️ | ✔ |
Contacts | contacts |
✔️ | ✔ |
Events | event |
✔️ | ✔ |
Bluetooth | bluetooth |
✔️ | ❌ |
Reminders | reminder |
✔️ | ❌ |
Push Notifications | notification |
✔️ | ❌ |
Background Refresh | backgroundRefresh |
✔️ | ❌ |
Speech Recognition | speechRecognition |
✔️ | ❌ |
Storage | storage |
❌️ | ✔ |
Phone Call | callPhone |
❌️ | ✔ |
Read SMS | readSms |
❌️ | ✔ |
Receive SMS | receiveSms |
❌️ | ✔ |
Method Name | Arguments | Notes |
---|---|---|
check() |
type |
- Returns a promise with the permission status. See iOS Notes for special cases |
request() |
type |
- Accepts any permission type except backgroundRefresh . If the current status is undetermined , shows the permission dialog and returns a promise with the resulting status. Otherwise, immediately return a promise with the current status. See iOS Notes for special cases |
checkMultiple() |
[types] |
- Accepts an array of permission types and returns a promise with an object mapping permission types to statuses |
getTypes() |
none | - Returns an array of valid permission types |
openSettings() |
none | - (iOS only - 8.0 and later) Switches the user to the settings page of your app |
canOpenSettings() |
none | - (iOS only) Returns a boolean indicating if the device supports switching to the settings page |
bluetooth
represents the status of the
CBPeripheralManager
. Don’t use this if only need CBCentralManager
location
accepts a second parameter for request()
and
check()
; the second parameter is a string, either always
or whenInUse
(default).notification
accepts a second parameter for request()
. The
second parameter is an array with the desired alert types. Any combination of
alert
, badge
and sound
(default requests all three).// example
Permissions.check('location', 'always').then(response => {
this.setState({ locationPermission: response })
})
Permissions.request('location', 'always').then(response => {
this.setState({ locationPermission: response })
})
Permissions.request('notification', ['alert', 'badge']).then(response => {
this.setState({ notificationPermission: response })
})
Info.plist
➜ Add a key (starting with “Privacy -
…“) with your kit specific permission.Example: If you need Contacts permission you have to add the key Privacy -
Contacts Usage Description
.
If you need to submit you application to the AppStore, you need to add to your
Info.plist
all *UsageDescription
keys with a string value explaining to the
user how the app uses this data. Even if you don’t use them.
So before submitting your app to the App Store, make sure that in your
Info.plist
you have the following keys:
<key>NSBluetoothPeripheralUsageDescription</key>
<string>Some description</string>
<key>NSCalendarsUsageDescription</key>
<string>Some description</string>
<key>NSCameraUsageDescription</key>
<string>Some description</string>
<key>NSLocationWhenInUseUsageDescription</key>
<string>Some description</string>
<key>NSPhotoLibraryUsageDescription</key>
<string>Some description</string>
<key>NSSpeechRecognitionUsageDescription</key>
<string>Some description</string>
This is required because during the phase of processing in the App Store
submission, the system detects that you app contains code to request the
permission X
but don’t have the UsageDescription
key and then it rejects the
build.
Please note that it will only be shown to the users the usage descriptions of the permissions you really require in your app.
You can find more informations about this issue in #46.
PermissionsAndroid
JS API.AndroidManifest.xml
file before they can be requested. Otherwise request()
will immediately
return denied
.AndroidManifest.xml
file. Read more
here.check()
to check if the user has disabled them from Settings.You might need to elevate the targetSdkVersion version in your
build.gradle
:
android {
compileSdkVersion 23 // ← set at least 23
buildToolsVersion "23.0.1" // ← set at least 23.0.0
defaultConfig {
minSdkVersion 16
targetSdkVersion 23 // ← set at least 23
// ...
A: Starting with Xcode 8, you need to add permission descriptions. See iOS notes for more details. Thanks to @jesperlndk for discovering this.
A: This is normal. iOS restarts your app when your privacy settings change. Just google “iOS crash permission change”