iOS路上

访问手机相册、相机、定位、通讯录、麦克风等权限

2017-09-18  本文已影响67人  海0_0滨

独立开发一款APP,需要访问相册,平时不注意,突然要亲自上手写的时候才发现,每次都得百度然后复制粘贴,深感惭愧,于是在又一次百度之后,决定把经验记录下来,做一个不copy的iOS开发工程师

一:iOS10之后权限访问更加清晰了,所有涉及本地数据访问的都需要在Info.plist里添加声明

相机权限:
<key>NSCameraUsageDescription</key>
<string>文字描述</string>
相册权限:
<key>NSPhotoLibraryUsageDescription</key>
<string>文字描述</string>
通讯录权限:
<key>NSContactsUsageDescription</key>
<string>文字描述</string>

其他的一些访问权限:
麦克风权限:Privacy - Microphone Usage Description 是否允许此App使用你的麦克风?
相机权限: Privacy - Camera Usage Description 是否允许此App使用你的相机?
相册权限: Privacy - Photo Library Usage Description
通讯录权限: Privacy - Contacts Usage Description
蓝牙权限:Privacy - Bluetooth Peripheral Usage Description
语音转文字权限:Privacy - Speech Recognition Usage Description
日历权限:Privacy - Calendars Usage Description
定位权限:Privacy - Location When In Use Usage Description
定位权限: Privacy - Location Always Usage Description
位置权限:Privacy - Location Usage Description
媒体库权限:Privacy - Media Library Usage Description
健康分享权限:Privacy - Health Share Usage Description
健康更新权限:Privacy - Health Update Usage Description
运动使用权限:Privacy - Motion Usage Description
音乐权限:Privacy - Music Usage Description
提醒使用权限:Privacy - Reminders Usage Description
Siri使用权限:Privacy - Siri Usage Description
电视供应商使用权限:Privacy - TV Provider Usage Description
视频用户账号使用权限:Privacy - Video Subscriber Account Usage Description

二:判断相机的使用权限

//AVAuthorizationStatusRestricted:此应用程序没有被授权访问的。可能是家长控制权限
//AVAuthorizationStatusDenied:用户已经明确否认了应用程序访问

需要引入头文件<AVFoundation/AVFoundation.h>

  • (BOOL)limitedPhotoGraphDevice {
    AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
    if(authStatus == AVAuthorizationStatusRestricted || authStatus ==AVAuthorizationStatusDenied) {
    //跳到APP的设置页面,此处的CBCCAlertView是我自己对系统Alert的风封装
    [CBCCAlertView alertWithCallBackBlock:^(NSInteger buttonIndex) {
    if (buttonIndex == 1) {
    NSURL *url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
    if ([[UIApplication sharedApplication]canOpenURL:url]) {
    [[UIApplication sharedApplication]openURL:url];
    }
    }
    } title:@"😢未获得相机授权" message:@"请在iPhone的“设置>隐私>相机”界面中打开" preferredStyle:UIAlertControllerStyleAlert cancelButtonName:@"取消" otherButtonTitles:@"设置", nil];
    return NO;
    }
    return YES;
    }
三:判断相册的使用权限

需要引入头文件<AssetsLibrary/AssetsLibrary.h>
iOS8.0之后推荐使用#import <Photos/Photos.h>

if(>ios8.0):PHAuthorizationStatus status = [PHPhotoLibrary authorizationStatus];
if (status == PHAuthorizationStatusRestricted ||
status == PHAuthorizationStatusDenied) {
return NO;}

if(<iOS8.0):ALAuthorizationStatus authStatus = [ALAssetsLibrary authorizationStatus];
if(authStatus == AVAuthorizationStatusRestricted || authStatus ==AVAuthorizationStatusDenied) {
//跳到APP的设置页面此处的CBCCAlertView是我自己对系统Alert的风封装 [CBCCAlertView alertWithCallBackBlock:^(NSInteger buttonIndex) {
if (buttonIndex == 1) {
NSURL *url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
if ([[UIApplication sharedApplication]canOpenURL:url]) {
[[UIApplication sharedApplication]openURL:url];
}
}
} title:@"😢未获得相册授权" message:@"请在iPhone的“设置>隐私>照片”界面中打开" preferredStyle:UIAlertControllerStyleAlert cancelButtonName:@"取消" otherButtonTitles:@"设置", nil];
return NO;
}
return YES;

最后,个人感觉在判断授权权限的时候最好能先判断一下设备状态是否可用

//判断设备是否可用,摄像头与相册类似,只时候面的类型不同
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
//判断是否授权使用
if ([CBCCPreference sharedInstance].limitedPhotoGraphDevice) {
self.imagePickerVC.sourceType = UIImagePickerControllerSourceTypeCamera;
[self.navigationController presentViewController:self.imagePickerVC animated:YES completion:nil];
}
}else {
NSLog(@"摄像头无法使用");
}

上一篇下一篇

猜你喜欢

热点阅读