訪問手機相冊、相機、定位、通訊錄、麥克風等權限

獨立開發一款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(@"攝像頭無法使用");
}

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容