RN-权限整理(iOS)

获取系统权限

1、相机
2、相册
3、位置
4、麦克风
5、通知状态

iOS工具类:PermissionsManager.h

#import 
#import 

#import 


@interface PermissionsManager : NSObject

@end

iOS工具类:PermissionsManager.m

//
//  PermissionsManager.m
//  Driver
//
//  Created by Mac on 2017/8/1.
//  Copyright © 2017年 Facebook. All rights reserved.
//

#import "PermissionsManager.h"

#import 
#import 
#import 


//iOS调用RN
#import 


@implementation PermissionsManager
@synthesize bridge = _bridge;

//导出模块
RCT_EXPORT_MODULE();    //此处不添加参数即默认为这个OC类的名字


//Promises
//最后两个参数是RCTPromiseResolveBlock(成功block)和RCTPromiseRejectBlock(失败block)




/*
 //正确回调,传递参数
 
 typedef void (^RCTPromiseResolveBlock)(id result);
 
 */


/*
 //错误回调,传三个参数
 
 typedef void (^RCTPromiseRejectBlock)(NSString *code, NSString *message, NSError *error);
 
 */



/**
 相机权限

 */
RCT_EXPORT_METHOD(cameraPermission:(RCTPromiseResolveBlock)resolve reject:(RCTPromiseRejectBlock)reject) {
 
  AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
  
  if (authStatus == AVAuthorizationStatusRestricted || authStatus ==AVAuthorizationStatusDenied)
    
  {
    //无权限
    reject(@"500",@"没有访问相机权限,请前往 ‘设置’-‘隐私’-‘相机’ 开启权限",nil);
   
  }else {
    // 有权限
    
    resolve(@YES);

  }
}


/**
 相册权限
 
 */
RCT_EXPORT_METHOD(photoPermission:(RCTPromiseResolveBlock)resolve reject:(RCTPromiseRejectBlock)reject) {
  
  ALAuthorizationStatus author = [ALAssetsLibrary authorizationStatus];
  
  if (author == ALAuthorizationStatusRestricted || author ==ALAuthorizationStatusDenied)
  {
    //无权限
    reject(@"500",@"没有访问相册权限,请前往 ‘设置’-‘隐私’-‘相册’ 开启权限",nil);
    
  }else {
    // 有权限
    
    resolve(@YES);
    
  }
}

/**
 定位权限
 
 */
RCT_EXPORT_METHOD(locationPermission:(RCTPromiseResolveBlock)resolve reject:(RCTPromiseRejectBlock)reject) {
  
  if([CLLocationManager locationServicesEnabled] && [CLLocationManager authorizationStatus] == kCLAuthorizationStatusDenied) {
    //无权限
    reject(@"500",@"没有访问位置权限,请前往 ‘设置’-‘隐私’-‘位置’ 开启权限",nil);
    
  }else {
    // 有权限
    
    resolve(@YES);
    
  }
}

/**
 麦克风权限
 
 */
RCT_EXPORT_METHOD(microphonePermission:(RCTPromiseResolveBlock)resolve reject:(RCTPromiseRejectBlock)reject) {
  
  AVAudioSession *avSession = [AVAudioSession sharedInstance];
  
  if ([avSession respondsToSelector:@selector(requestRecordPermission:)]) {
    
    [avSession requestRecordPermission:^(BOOL available) {
      if (available) {
        resolve(@YES);
      }else {
        reject(@"500",@"没有访问麦克风权限,请前往 ‘设置’-‘隐私’-‘麦克风’ 开启权限",nil);
      }
    }];
    
  }
}

/**
 通知状态
 
 */
#define IOS8 ([[[UIDevice currentDevice] systemVersion] doubleValue] >=8.0 ? YES : NO)

RCT_EXPORT_METHOD(notificationStatus:(RCTResponseSenderBlock)callback){

  if (IOS8) { //iOS8以上包含iOS8
    if ([[UIApplication sharedApplication] currentUserNotificationSettings].types  == UIUserNotificationTypeNone) {
      callback(@[@NO]);
    }else{
      callback(@[@YES]);
    }
  }else{ // ios7 以下
    if ([[UIApplication sharedApplication] enabledRemoteNotificationTypes]  == UIRemoteNotificationTypeNone) {
      callback(@[@NO]);
    }else{
      callback(@[@YES]);
    }
  }

}
@end

ReactNative:工具类 permissionsManager.js

/**
 * Created by mymac on 2017/7/19.
 */

import React, { Component } from 'react';
import {
    View,
    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
};

使用

import PermissionsManager from '../../utils/permissionManager';


 PermissionsManager.cameraPermission().then(data=>{
     this.selectCamera();
 }).catch(err=>{
     Toast.showShortCenter(err.message);
 });


你可能感兴趣的:(RN-权限整理(iOS))