【React Native】文件翻譯閱讀紀錄 - APIs - PermissionsAndroid
Facebook Open Source React Native |
PermissionsAndroid
例
import { PermissionsAndroid } from 'react-native';
async function requestCameraPermission() {
try {
const granted = await PermissionsAndroid.request(
PermissionsAndroid.PERMISSIONS.CAMERA,
{
'title': 'Cool Photo App Camera Permission',
'message': 'Cool Photo App needs access to your camera ' +
'so you can take awesome pictures.'
}
)
if (granted === PermissionsAndroid.RESULTS.GRANTED) {
console.log("You can use the camera")
} else {
console.log("Camera permission denied")
}
} catch (err) {
console.warn(err)
}
}
需要提示用戶的權限
在 PermissionsAndroid.PERMISSIONS 下以常量形式提供:
READ_CALENDAR
: 'android.permission.READ_CALENDAR'WRITE_CALENDAR
: 'android.permission.WRITE_CALENDAR'CAMERA
: 'android.permission.CAMERA'READ_CONTACTS
: 'android.permission.READ_CONTACTS'WRITE_CONTACTS
: 'android.permission.WRITE_CONTACTS'GET_ACCOUNTS
: 'android.permission.GET_ACCOUNTS'ACCESS_FINE_LOCATION
: 'android.permission.ACCESS_FINE_LOCATION'ACCESS_COARSE_LOCATION
: 'android.permission.ACCESS_COARSE_LOCATION'RECORD_AUDIO
: 'android.permission.RECORD_AUDIO'READ_PHONE_STATE
: 'android.permission.READ_PHONE_STATE'CALL_PHONE
: 'android.permission.CALL_PHONE'READ_CALL_LOG
: 'android.permission.READ_CALL_LOG'WRITE_CALL_LOG
: 'android.permission.WRITE_CALL_LOG'ADD_VOICEMAIL
: 'com.android.voicemail.permission.ADD_VOICEMAIL'USE_SIP
: 'android.permission.USE_SIP'PROCESS_OUTGOING_CALLS
: 'android.permission.PROCESS_OUTGOING_CALLS'BODY_SENSORS
: 'android.permission.BODY_SENSORS'SEND_SMS
: 'android.permission.SEND_SMS'RECEIVE_SMS
: 'android.permission.RECEIVE_SMS'READ_SMS
: 'android.permission.READ_SMS'RECEIVE_WAP_PUSH
: 'android.permission.RECEIVE_WAP_PUSH'RECEIVE_MMS
: 'android.permission.RECEIVE_MMS'READ_EXTERNAL_STORAGE
: 'android.permission.READ_EXTERNAL_STORAGE'WRITE_EXTERNAL_STORAGE
: 'android.permission.WRITE_EXTERNAL_STORAGE'
請求權限的結果字符串
可在 PermissionsAndroid.RESULTS 下作為常量使用:
GRANTED
: 'granted'DENIED
: 'denied'NEVER_ASK_AGAIN
: 'never_ask_again'
Methods
參考
Methods
constructor()
constructor();
check()
check(permission);
返回一個解析為布爾值的promise,以確定是否已授予指定的權限。
參數:
NAME | TYPE | REQUIRED | DESCRIPTION |
---|---|---|---|
permission | string | Yes | 要檢查的權限。 |
request()
request(permission, [rationale]);
提示用戶啟用權限並返回解析為字符串值的promise(請參閱上面的結果字符串),指示用戶是允許還是拒絕請求,或者是否不想再次詢問。
如果提供了基本原理,此功能會檢查操作系統是否有必要顯示一個對話框,說明需要權限的原因(https://developer.android.com/training/permissions/requesting.html#explain) ,然後顯示系統權限對話框。
如果提供了基本原理,此功能會檢查操作系統是否有必要顯示一個對話框,說明需要權限的原因(https://developer.android.com/training/permissions/requesting.html#explain) ,然後顯示系統權限對話框。
參數:
NAME | TYPE | REQUIRED | DESCRIPTION |
---|---|---|---|
permission | string | Yes | 請求的權限。 |
rationale | object | No | 帶標題和消息的對象。 |
requestMultiple()
requestMultiple(permissions);
提示用戶在同一對話框中啟用多個權限,並返回一個對象,其權限為鍵和字符串作為值(請參閱上面的結果字符串),指示用戶是允許還是拒絕請求,或者不想再次詢問。
參數:
NAME | TYPE | REQUIRED | DESCRIPTION |
---|---|---|---|
permissions | array | Yes | 要求的權限數組。 |
0 意見