当前位置:   article > 正文

react native中ios判断相机是否授权,没授权打开手机设置

react native中ios判断相机是否授权,没授权打开手机设置

react native中ios判断相机是否授权,没授权打开手机设置

CameraPermissionManager.h

#import <React/RCTBridgeModule.h>

@interface CameraPermissionManager : NSObject <RCTBridgeModule>

@end
  • 1
  • 2
  • 3
  • 4
  • 5

CameraPermissionManager.m

#import "CameraPermissionManager.h"
#import <AVFoundation/AVCaptureDevice.h>

@implementation CameraPermissionManager

RCT_EXPORT_MODULE();

RCT_EXPORT_METHOD(checkCameraStatus:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject)
{
    AVAuthorizationStatus status = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
    
    if (status == AVAuthorizationStatusAuthorized) {
        resolve(@"authorized");
    } else if (status == AVAuthorizationStatusNotDetermined) {
        resolve(@"undetermined");
    } else if(status == AVAuthorizationStatusDenied || status == AVAuthorizationStatusRestricted){
        resolve(@"rejected");
    } else {
        resolve(@"denied");
    }
}

@end

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24

cameraPermiss.js

import {NativeModules} from 'react-native';

export default NativeModules.CameraPermissionManager;
  • 1
  • 2
  • 3

使用

import cameraPermiss from '../cameraPermiss.js'

//判断用户是否相机授权
  const cameraPermiss = async () => {
    if (Platform.OS == 'android') {
      const response = await PermissionsAndroid.request(PermissionsAndroid.PERMISSIONS.CAMERA);
      console.log("[相机授权]", response)
      if (response != 'granted') {
        //用户拒绝
        Linking.openSettings();
      } 
    } else {
      const response = await cameraPermiss.checkCameraStatus();
      console.log("[相机授权]", response)
      if (response == 'rejected') {
        //用户拒绝
        Linking.openSettings();
      } 
    }

  }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小小林熬夜学编程/article/detail/638780
推荐阅读
相关标签
  

闽ICP备14008679号