2016-12-02 13:28:48 u012198553 阅读数 886
// 这是向系统请求访问相机的代码
 [AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo completionHandler:^(BOOL granted) {
            NSLog(@"requestAccessForMediaType");
        }];
//  一般这个是系统默认的,所以不需要我们手动添加,只是稍微了解一下。
       //相机权限的获取
        AVAuthorizationStatus cameraStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
        switch (cameraStatus) {
            case AVAuthorizationStatusDenied: {
                [MessageTools showMessageWithSure:@"相机访问权限受限,请到系统\"设置\"页面打开相机访问权限!"];
                return;
            }
                break;
            case AVAuthorizationStatusAuthorized: {
                //已经授权
            }
                break;
            case AVAuthorizationStatusRestricted: {
                //client还未申请相机访问权限,用户不能更改相机访问权限状态
            }
                break;
            case AVAuthorizationStatusNotDetermined: {
                //状态未知,用户还未处理是否允许应用访问相机
            }
                break;
            default:
                break;
        }
2016-11-14 17:09:21 coderMy 阅读数 3236

info.plist文件配置字段

1 . 定位权限 : Privacy - Location When In Use Usage Description

2 . 通讯录权限: Privacy - Contacts Usage Description

3 . 相机权限 : Privacy - Camera Usage Description

4 . 相册权限 : Privacy - Photo Library Usage Description

5 . 麦克风权限 : Privacy - Microphone Usage Description


定位权限 : #import < CoreLocation/CoreLocation.h>

//定位权限状态
    CLAuthorizationStatus status = [CLLocationManager authorizationStatus];

    if (status == kCLAuthorizationStatusRestricted) {

        return;

    }else if (status == kCLAuthorizationStatusNotDetermined){

        //请求用户授权
        [self.manager requestWhenInUseAuthorization];
    }else{

        //定位相关设置
        self.manager.delegate = self;
        self.manager.desiredAccuracy = kCLLocationAccuracyBest;
        self.manager.distanceFilter = kCLLocationAccuracyNearestTenMeters;

        //开始定位
         [self.manager startUpdatingLocation];
    }

相册权限 #import < Photos/Photos.h>

    PHAuthorizationStatus status = [PHPhotoLibrary authorizationStatus];

    switch (status) {
            //未决定
        case PHAuthorizationStatusNotDetermined:
        {
            [PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) {

                if (status == PHAuthorizationStatusRestricted ||
                    status == PHAuthorizationStatusDenied) {

                }else{

                    //打开相册
                    [self uploadOperationWithOperationStyle:JYUploadOperationStylePhoto];
                }
            }];
        }
            break;

            //拒绝
          case AVAuthorizationStatusRestricted:
        {
            //引导用户打开权限
            [NSObject alertShowForAuthorizationWithTarget:nil callBack:^{

                //打开用户设置
                [[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];
            }];

        }
            break;

            //已经授权过
            case AVAuthorizationStatusAuthorized:
        {
        //打开相册
            [self uploadOperationWithOperationStyle:JYUploadOperationStylePhoto];
        }
            break;
        default:
            break;
    }

通讯录权限 #import < AddressBook/AddressBook.h>

    //    kABAuthorizationStatusNotDetermined = 0,  未决定
    //    kABAuthorizationStatusRestricted,  特殊原因禁止访问用户通讯录
    //    kABAuthorizationStatusDenied,   用户拒绝访问
    //    kABAuthorizationStatusAuthorized 已经授权访问
    //    倒数第二种除非用户在设置里主动打开。否则在APP里永远无法访问 。 只有第一种情况下,才会告知用户授权。

    //判断用户授权状态
    ABAuthorizationStatus state = ABAddressBookGetAuthorizationStatus();

    //判断   如果授权状态是未决定的,则请求授权
    if (state == kABAuthorizationStatusNotDetermined) {

        //请求
        //1.获取通讯录对象
        ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, NULL);

        //2.请求授权 (用户决定后就会回调block)
        ABAddressBookRequestAccessWithCompletion(addressBook, ^(bool granted, CFErrorRef error) {

            if (granted) {
                NSLog(@"用户授权成功");
            }else{
                NSLog(@"用户授权失败");
            }
        });
    }

相机权限

//权限判断
    AVAuthorizationStatus  authStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];

    switch (authStatus) {
            //未决定状态
        case AVAuthorizationStatusNotDetermined:

        {
            [AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo completionHandler:^(BOOL granted) {

                //接收
                if (granted) {

                    [self takePhotos];
                }
            }];
        }
            break;
            //拒绝
          case AVAuthorizationStatusRestricted:
        {
            //alert提示用户去设置里打开权限
        }
            break;
            case AVAuthorizationStatusAuthorized:
        {
            [self takePhotos];
        }
            break;
        default:
            break;
    }
//拍照或录像
- (void)takePhotos
{
    UIImagePickerController *controller = [[UIImagePickerController alloc] init];

    controller.sourceType = UIImagePickerControllerSourceTypeCamera;// 设置类型

    NSString *requiredMediaType = ( NSString *)kUTTypeImage;

    NSString *requiredMediaType1 = ( NSString *)kUTTypeMovie;

    NSArray *arrMediaTypes=[NSArray arrayWithObjects:requiredMediaType, requiredMediaType1,nil];

    controller.mediaTypes = arrMediaTypes;


    // 设置录制视频的质量
    controller.videoQuality = UIImagePickerControllerQualityTypeHigh;

    //设置最长摄像时间
    controller.videoMaximumDuration = 10.f;

    //设置代理    回调方法 , 之前文章有写过 , 这里就不写了
    controller.delegate = self;

    [self presentViewController:controller animated:YES completion:nil];
}
//然后在代理方法中得到内容 ... 

代理回调方法
http://blog.csdn.net/codermy/article/details/53055861


麦克风权限

    AVAudioSessionRecordPermission status = [[AVAudioSession sharedInstance]recordPermission];

    switch (status) {

            //未决定状态
        case AVAudioSessionRecordPermissionUndetermined:
        {
            [[AVAudioSession sharedInstance]requestRecordPermission:^(BOOL granted) {

                if (granted) {

                    //打开麦克风录音

                }
            }];
        }
            break;

            //已经取得权限
            case AVAudioSessionRecordPermissionGranted:

            //打开麦克风录音
            break;
        default:
            break;
    }
2017-08-17 15:13:04 weixin_36259863 阅读数 299

1.相机

[AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo completionHandler:^(BOOL granted) {//相机权限
        if (granted) {

        }else{

        }
    }];

判断权限

AVAuthorizationStatus status = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
    if (status != AVAuthorizationStatusAuthorized){

    }

2.麦克风

AVAudioSession *audioSession = [AVAudioSession sharedInstance];
    if ([audioSession respondsToSelector:@selector(requestRecordPermission:)]) {
        [audioSession performSelector:@selector(requestRecordPermission:) withObject:^(BOOL granted) {
            if (granted) {

            } else {

            }
        }];
    }
    或者
     [AVCaptureDevice requestAccessForMediaType:AVMediaTypeAudio completionHandler:^(BOOL granted) {
        if (granted) {

        }else{

        }
    }];

判断权限

AVAuthorizationStatus status = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeAudio];
    if (status != AVAuthorizationStatusAuthorized){

    }

3.相册

 ALAuthorizationStatus author = [ALAssetsLibrary authorizationStatus];

 if (author ==kCLAuthorizationStatusRestricted || author ==kCLAuthorizationStatusDenied){

}

iOS10之后,如果需要使用系统的某些资源,在info.plist里面需要添加下列相对应的键值。


<!-- 相册 -->   
<key>NSPhotoLibraryUsageDescription</key>   
<string>App需要您的同意,才能访问相册</string>   
<!-- 相机 -->   
<key>NSCameraUsageDescription</key>   
<string>App需要您的同意,才能访问相机</string>   
<!-- 麦克风 -->   
<key>NSMicrophoneUsageDescription</key>   
<string>App需要您的同意,才能访问麦克风</string>   
<!-- 位置 -->   
<key>NSLocationUsageDescription</key>   
<string>App需要您的同意,才能访问位置</string>   
<!-- 在使用期间访问位置 -->   
<key>NSLocationWhenInUseUsageDescription</key>   
<string>App需要您的同意,才能在使用期间访问位置</string>   
<!-- 始终访问位置 -->   
<key>NSLocationAlwaysUsageDescription</key>   
<string>App需要您的同意,才能始终访问位置</string>   
<!-- 日历 -->   
<key>NSCalendarsUsageDescription</key>   
<string>App需要您的同意,才能访问日历</string>   
<!-- 提醒事项 -->   
<key>NSRemindersUsageDescription</key>   
<string>App需要您的同意,才能访问提醒事项</string>   
<!-- 运动与健身 -->   
<key>NSMotionUsageDescription</key> <string>App需要您的同意,才能访问运动与健身</string>   
<!-- 健康更新 -->   
<key>NSHealthUpdateUsageDescription</key>   
<string>App需要您的同意,才能访问健康更新 </string>   
<!-- 健康分享 -->   
<key>NSHealthShareUsageDescription</key>   
<string>App需要您的同意,才能访问健康分享</string>   
<!-- 蓝牙 -->   
<key>NSBluetoothPeripheralUsageDescription</key>   
<string>App需要您的同意,才能访问蓝牙</string>   
<!-- 媒体资料库 -->   
<key>NSAppleMusicUsageDescription</key>  <string>App需要您的同意,才能访问媒体资料库</string>
2019-08-05 10:06:40 u010462316 阅读数 211

如何在app权限申请提示中,加入不同国家的语言?很简单,只需要在工程文件中的InfoPlist.string中增加对应权限提示的字段。不知道InfoPlist.string,可以搜索一下iOS app名称国际化。

 

下面列出常用权限字段:

NSCameraUsageDescription = "App想使用您的相机,允许吗?";
NSPhotoLibraryUsageDescription = "App想访问您的相册";
NSLocationWhenInUseUsageDescription = "App需要你的同意,才能訪問位置";
NSMicrophoneUsageDescription = "App需要您的同意,才能訪問麥克風";
NSContactsUsageDescription = "App想访问您的通讯录,允许吗?";
NSLocationAlwaysUsageDescription = "App想使用您的地理位置,允许吗?"
NSLocationWhenInUseUsageDescription = "App想使用您的地理位置,允许吗?"
 

 

2019-05-27 18:23:17 u013505811 阅读数 332

当我们需要调用相机时,通常有两个方案,一是使用系统定制UI的UIImagePickerController,二是使用可以自定义UI的AVCaptureSession。

如果我们不要求自定义UI则可以直接使用UIImagePickerController,可以节省时间。

@interface UIImagePickerController : UINavigationController

我们来看一下整个流程:

  • 1.在项目的info.plist中添加相关使用描述:
	<key>NSCameraUsageDescription</key>
	<string>这里添加相机权限描述</string>

	<key>NSMicrophoneUsageDescription</key>
	<string>这里添加麦克风权限描述</string>

	<key>NSPhotoLibraryAddUsageDescription</key>
	<string>这里添加保存到相册权限描述</string>

	<key>NSPhotoLibraryUsageDescription</key>
	<string>这里添加相册权限描述</string>

 

  • 2.初始化UIImagePickerController
    @property (strong,nonatomic) UIImagePickerController* pickController;

    //在跳转到相机的方法中
    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
        self.pickController = [[UIImagePickerController alloc]init];
        self.pickController.sourceType = UIImagePickerControllerSourceTypeCamera;
        self.pickController.mediaTypes = @[@"public.image"];
        self.pickController.delegate = self;         //代理设置
        self.pickController.allowsEditing = NO;      //是否提供编辑交互界面 比如说拍完照之后的编辑页面(缩放,剪裁等)
     //使用内置编辑控件时,图像选择器控制器会强制执行某些选项。对于照片,强制执行方形裁剪以及最大像素尺寸。对于视频,选择器强制执行最大电影长度和分辨率。如果要让用户指定自定义裁剪,则必须提供自己的编辑UI。
        self.pickController.showsCameraControls = NO;//是否显示相机控制按钮
        self.pickController.cameraOverlayView = self.cameraOverLayView; //自定义相机控制页面
     //如果不需要自定义控制页面可以省略上面两行
     //设置闪光灯模式
       self.pickController.cameraFlashMode = UIImagePickerControllerCameraFlashModeAuto;
        /*
         typedef NS_ENUM(NSInteger, UIImagePickerControllerCameraFlashMode) {
         UIImagePickerControllerCameraFlashModeOff  = -1,
         UIImagePickerControllerCameraFlashModeAuto = 0,
         UIImagePickerControllerCameraFlashModeOn   = 1
         }
         */
    }else{
        return;
    }

其中:

[UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera];
//如果设备可用返回YES 否则返回NO

首先使用该方法来测试设备相机的可用性。

Note: Always call the isSourceTypeAvailable: class method of the UIImagePickerController class and respect its return value. Never assume that a device has a camera. Even if the device has a camera, this method returns NO if the camera is unavailable.    

永远要调用这个方法来测试设备相机。永远不要主观认为每个设备都有相机。一个设备即使有摄像头,在那个摄像头不可用时,该方法也会返回NO

 

self.pickController.sourceType
// default value is UIImagePickerControllerSourceTypePhotoLibrary.
// 选择要访问的源类型 
//typedef NS_ENUM(NSInteger, UIImagePickerControllerSourceType) {
//    UIImagePickerControllerSourceTypePhotoLibrary,     //图库
//    UIImagePickerControllerSourceTypeCamera,           //相机
//    UIImagePickerControllerSourceTypeSavedPhotosAlbum  //相机胶卷
//}
self.pickController.mediaTypes
//指定媒体类型是什么 照片还是视频
//默认为 照片
//通过下一行方法可以返回支持的类型
[UIImagePickerController availableMediaTypesForSourceType:UIImagePickerControllerSourceTypeCamera];
//查到很多资料都是"kUTTypeMovie","kUTTypeImage"这两个参数名称但是我测试后发现已经变成下面这两种名称
//"public.image"  照片
//"public.movie"  视频
//如果全部支持可以这么设置
self.pickController.mediaTypes = [UIImagePickerController availableMediaTypesForSourceType:UIImagePickerControllerSourceTypeCamera];
//单个支持
self.pickController.mediaTypes = @[@"public.image"];

最后设置完代理后,别忘了遵守协议

<UINavigationControllerDelegate,UIImagePickerControllerDelegate>
  • 3.实现协议方法:
// 控制器不会自己dismiss 需要我们手动在相应的地方实现
// 这两个代理方法只会收到其中一个,取决于用户的点击情况

//结束采集之后 之后怎么处理都在这里写 通过Infokey取出相应的信息  Infokey可在进入文件中查看
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<UIImagePickerControllerInfoKey, id> *)info{
//查看是视频还是照片  public.image 或 public.movie
NSString * mediaType = [info objectForKey:UIImagePickerControllerMediaType];
    if ([mediaType isEqualToString:@"public.image"]) {//照片
        UIImage* editedImage =(UIImage *)[info objectForKey:
                    UIImagePickerControllerEditedImage]; //取出编辑过的照片
        UIImage* originalImage =(UIImage *)[info objectForKey:
                    UIImagePickerControllerOriginalImage];//取出原生照片
        UIImage* imageToSave = nil;
        if(editedImage){
            imageToSave = editedImage;
        } else {
            imageToSave = originalImage;
        }
    //将新图像(原始图像或已编辑)保存到相机胶卷
        UIImageWriteToSavedPhotosAlbum(imageToSave,nil,nil,nil);
    }
}
    if ([mediaType isEqualToString:@"public.movie"]) {//视频
}
}


- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker; 
//用户点击了取消
  • 4.跳转到相机前确认权限:

首先还是要确认一下app的各种权限是否打开:

AVAuthorizationStatus cameraStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
//相机权限
//     typedef NS_ENUM(NSInteger, AVAuthorizationStatus) {
//     AVAuthorizationStatusNotDetermined = 0,
//     AVAuthorizationStatusRestricted    = 1,
//     AVAuthorizationStatusDenied        = 2,
//     AVAuthorizationStatusAuthorized    = 3,
//     } API_AVAILABLE(macos(10.14), ios(7.0))
PHAuthorizationStatus status = [PHPhotoLibrary authorizationStatus];

  /* 相册权限检测 需要导入 #import <Photos/Photos.h> 框架
     typedef NS_ENUM(NSInteger, PHAuthorizationStatus) {
     PHAuthorizationStatusNotDetermined = 0, // 用户还没有选择
     PHAuthorizationStatusRestricted,        // 客户端未被授权访问。用户不能改变状态,可能是由于家长控制
     PHAuthorizationStatusDenied,            // 用户明确拒绝
     PHAuthorizationStatusAuthorized         // 用户同意访问
     } PHOTOS_AVAILABLE_IOS_TVOS_OSX(8_0, 10_0, 10_13);
     */
 [PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) {
    //相册访问权限   
     if (status == PHAuthorizationStatusAuthorized) {
            NSLog(@"Authorized");
        }else{
            NSLog(@"Denied or Restricted");
        }
    }];

ps:这里有一个坑。

测试手机版本(12.1.4)

点击拒绝访问用户相册之后还是会进入相册,且下次也可以进入,貌似不需要用户授权即可访问用户相册。

把Info.plist中的相册使用权限描述删除之后 且 把相册权限检测方法删掉 可以直接访问用户相册不报错。

如果添加了相册检测方法则需要在Info.plist文件中添加相册使用描述,否则报错。

在需要跳转的地方执行:

- (void)presentViewController:(UIViewController *)viewControllerToPresent animated: (BOOL)flag completion:(void (^ __nullable)(void))completion;

即可进入到系统定制UI的相机页面,这时我们可能会发现为什么界面都是英文的呢?

这样做:

在Localizations中添加中文源,然后在target中改成China即可。

  • 说下CameraOverLayView

UIImageViewController的这个属性,可以给其赋值实现自定义相机控制view。就是说我们自己写好一个view上面有各种控制按钮,将其替代系统的相机控制按钮(快门键,切换前后置,闪光灯控制等)。

@property(nullable, nonatomic,strong) __kindof UIView *cameraOverlayView  NS_AVAILABLE_IOS(3_1);   // set a view to overlay the preview view.

需要搭配:


@property(nonatomic) BOOL showsCameraControls NS_AVAILABLE_IOS(3_1);   // 是否显示系统标准控制UI。默认YES
  • 可能会遇到的问题:

如果在选择

        self.pickController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;

运行时发生这样的错误提示:

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Source type must be UIImagePickerControllerSourceTypeCamera'

注意:

        当我们使用相册的时候 就无法将这个

self.pickController.showsCameraControls = YES;

设置为YES; 

        解决办法:  不写这一行,采用默认值;

 

【IOS】权限管理

阅读数 922

iOS权限设置

阅读数 1416

没有更多推荐了,返回首页