【React Native】請求設備權限

React Native請求用戶權限

Request user permissions from React Native, iOS + Android

參考鏈接:https://www.npmjs.com/package/react-native-permissions

一、如何使用

1.1 添加組件庫:npm install --save react-native-permissions --- or ---  yarn add react-native-permissions

1.2 連接原生庫:react-native link react-native-permissions

1.3 iOS端初始化內庫:In the XCode's "Project navigator", right click on your project's Libraries folder ➜ Add Files to <...>

1.4 iOS端加載組件庫:Go to node_modules ➜ react-native-permissions ➜ selectReactNativePermissions.xcodeproj

1.5 iOS端加載內庫: Add libReactNativePermissions.a to Build Phases -> Link Binary With Libraries

1.6 Android端現在在Android上使用React Native自己的JS PermissionsAndroid模塊,不再需要在Android上做任何額外的連接

  • 注意:Don't forget to add permissions to AndroidManifest.xml for android and Info.plistfor iOS (Xcode >= 8). See iOS Notes or Android Notes for more details. 
// example

    Permissions.check('location', { type: 'always' }).then(response => {
        this.setState({ locationPermission: response })
    })
 
    Permissions.request('location', { type: 'always' }).then(response => {
        this.setState({ locationPermission: response })
    })
 
    Permissions.request('notification', { type: ['alert', 'badge'] }).then(
        response => {
        this.setState({ notificationPermission: response })
        },
    )

二、權限狀態

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 prompted multiple times until they select 'Never ask me again'

用戶至少一次拒絕此權限。

在iOS上,這意味着用戶不會再次被提示。

安卓用戶可以被多次提示,直到他們選擇“永遠不要再問我”

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

iOS - 這意味着用戶不能授予這個權限,要麼是因爲設備不支持它,要麼是因爲它被父母的控制阻止了。

Android - 這意味着用戶在拒絕許可的同時選擇了“永遠不要再問我”

undetermined

User has not yet been prompted with a permission dialog

用戶還沒有被提示使用權限對話框

三、方法

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

四、權限支持情況

  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 ✔️
mediaLibrary mediaLibrary ✔️
Motion Activity motion ✔️
Storage storage ❌️
Phone Call callPhone ❌️
Read SMS readSms ❌️
Receive SMS receiveSms ❌️

五、注意

(1)location權限請求request() 和檢查 check() 支持第二個參數,字符串類型,表示總是使用或者在使用時使用(默認項)

(2)notification權限請求request() 支持第二個參數,是具有所需警報類型的數組類型,表示警報、徽章和聲音的任何組合(默認是這三種請求都請求)

(3)iOS端的權限描述必須規範,必須指定爲什麼應用程序請求該權限。例:‘ APP會在上傳頭像圖片等服務中訪問您的相冊權限,是否允許?’、’APP需要您的同意,才能訪問相機進行拍照/掃描二維碼,如禁止將無法拍照/掃描二維碼‘。官方語句:please revise the permission modal alert to specify why the app is requesting access to the camera and location

(4)……

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章