RN中手机权限的获取

1、IOS

import {
    NativeModules,
    NativeAppEventEmitter,
} from 'react-native';


//在JavaScript中调用Object-C定义的方法,需要先导入NativeModules
//此处的RNCalliOSAction就是我们在iOS上新建的类名
//如果在iOS中设置了导出了类的名字,此处需要和导出的名字一致
const manager = NativeModules.PermissionsManager;

/*相机权限*/
const cameraPermission = () => {

   return new Promise((resolve, reject) => {
       manager.cameraPermission().then(data => {
           resolve(data);
       }).catch(err => {
           reject(err);
       });
   });
};

/*相机权限*/
const photoPermission = () => {

    return new Promise((resolve, reject) => {
        manager.photoPermission().then(data => {
            resolve(data);
        }).catch(err => {
            reject(err);
        });
    });
};

/*位置权限*/
const locationPermission = () => {

    return new Promise((resolve, reject) => {
        manager.locationPermission().then(data => {
            resolve(data);
        }).catch(err => {
            reject(err);
        });
    });
};

/*麦克风权限*/
const microphonePermission = () => {

    return new Promise((resolve, reject) => {
        manager.microphonePermission().then(data => {
            resolve(data);
        }).catch(err => {
            reject(err);
        });
    });
};

/*通知状态*/
const notificationStatus = () => {

    return new Promise((resolve) => {
        manager.notificationStatus((status)=>{
            resolve(status);
        });
    });
};
export default {
    cameraPermission,
    photoPermission,
    locationPermission,
    microphonePermission,
    notificationStatus
};

2、Android

import {  NativeModules } from 'react-native';

function cameraPermission() {
    return NativeModules.PermissionManager.cameraPermission();
}
function photoPermission() {
    return NativeModules.PermissionManager.photoPermission();
}

function locationPermission() {
    return NativeModules.PermissionManager.locationPermission();
}

function phonePermission() {
    return NativeModules.PermissionManager.phonePermission();
}
/** 个别种类的手机获取相册的时候需要此权限,例如 有些 小米手机**/
function externalPermission() {
    return NativeModules.PermissionManager.externalPermission();
}

export default {
    cameraPermission,
    photoPermission,
    locationPermission,
    phonePermission,
    externalPermission
};

你可能感兴趣的:(简单的实例)