獨(dú)立開發(fā)一款A(yù)PP昔榴,需要訪問相冊(cè),平時(shí)不注意碘橘,突然要親自上手寫的時(shí)候才發(fā)現(xiàn)互订,每次都得百度然后復(fù)制粘貼,深感慚愧蛹屿,于是在又一次百度之后屁奏,決定把經(jīng)驗(yàn)記錄下來,做一個(gè)不copy的iOS開發(fā)工程師
一:iOS10之后權(quán)限訪問更加清晰了错负,所有涉及本地?cái)?shù)據(jù)訪問的都需要在Info.plist里添加聲明
相機(jī)權(quán)限:
<key>NSCameraUsageDescription</key>
<string>文字描述</string>
相冊(cè)權(quán)限:
<key>NSPhotoLibraryUsageDescription</key>
<string>文字描述</string>
通訊錄權(quán)限:
<key>NSContactsUsageDescription</key>
<string>文字描述</string>
其他的一些訪問權(quán)限:
麥克風(fēng)權(quán)限:Privacy - Microphone Usage Description 是否允許此App使用你的麥克風(fēng)坟瓢?
相機(jī)權(quán)限: Privacy - Camera Usage Description 是否允許此App使用你的相機(jī)勇边?
相冊(cè)權(quán)限: Privacy - Photo Library Usage Description
通訊錄權(quán)限: Privacy - Contacts Usage Description
藍(lán)牙權(quán)限:Privacy - Bluetooth Peripheral Usage Description
語(yǔ)音轉(zhuǎn)文字權(quán)限:Privacy - Speech Recognition Usage Description
日歷權(quán)限:Privacy - Calendars Usage Description
定位權(quán)限:Privacy - Location When In Use Usage Description
定位權(quán)限: Privacy - Location Always Usage Description
位置權(quán)限:Privacy - Location Usage Description
媒體庫(kù)權(quán)限:Privacy - Media Library Usage Description
健康分享權(quán)限:Privacy - Health Share Usage Description
健康更新權(quán)限:Privacy - Health Update Usage Description
運(yùn)動(dòng)使用權(quán)限:Privacy - Motion Usage Description
音樂權(quán)限:Privacy - Music Usage Description
提醒使用權(quán)限:Privacy - Reminders Usage Description
Siri使用權(quán)限:Privacy - Siri Usage Description
電視供應(yīng)商使用權(quán)限:Privacy - TV Provider Usage Description
視頻用戶賬號(hào)使用權(quán)限:Privacy - Video Subscriber Account Usage Description
二:判斷相機(jī)的使用權(quán)限
//AVAuthorizationStatusRestricted:此應(yīng)用程序沒有被授權(quán)訪問的≌哿可能是家長(zhǎng)控制權(quán)限
//AVAuthorizationStatusDenied:用戶已經(jīng)明確否認(rèn)了應(yīng)用程序訪問
需要引入頭文件<AVFoundation/AVFoundation.h>
- (BOOL)limitedPhotoGraphDevice {
AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
if(authStatus == AVAuthorizationStatusRestricted || authStatus ==AVAuthorizationStatusDenied) {
//跳到APP的設(shè)置頁(yè)面,此處的CBCCAlertView是我自己對(duì)系統(tǒng)Alert的風(fēng)封裝
[CBCCAlertView alertWithCallBackBlock:^(NSInteger buttonIndex) {
if (buttonIndex == 1) {
NSURL *url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
if ([[UIApplication sharedApplication]canOpenURL:url]) {
[[UIApplication sharedApplication]openURL:url];
}
}
} title:@"??未獲得相機(jī)授權(quán)" message:@"請(qǐng)?jiān)趇Phone的“設(shè)置>隱私>相機(jī)”界面中打開" preferredStyle:UIAlertControllerStyleAlert cancelButtonName:@"取消" otherButtonTitles:@"設(shè)置", nil];
return NO;
}
return YES;
}
三:判斷相冊(cè)的使用權(quán)限
需要引入頭文件<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的設(shè)置頁(yè)面此處的CBCCAlertView是我自己對(duì)系統(tǒng)Alert的風(fēng)封裝 [CBCCAlertView alertWithCallBackBlock:^(NSInteger buttonIndex) {
if (buttonIndex == 1) {
NSURL *url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
if ([[UIApplication sharedApplication]canOpenURL:url]) {
[[UIApplication sharedApplication]openURL:url];
}
}
} title:@"??未獲得相冊(cè)授權(quán)" message:@"請(qǐng)?jiān)趇Phone的“設(shè)置>隱私>照片”界面中打開" preferredStyle:UIAlertControllerStyleAlert cancelButtonName:@"取消" otherButtonTitles:@"設(shè)置", nil];
return NO;
}
return YES;
最后粒褒,個(gè)人感覺在判斷授權(quán)權(quán)限的時(shí)候最好能先判斷一下設(shè)備狀態(tài)是否可用
//判斷設(shè)備是否可用,攝像頭與相冊(cè)類似,只時(shí)候面的類型不同
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
//判斷是否授權(quán)使用
if ([CBCCPreference sharedInstance].limitedPhotoGraphDevice) {
self.imagePickerVC.sourceType = UIImagePickerControllerSourceTypeCamera;
[self.navigationController presentViewController:self.imagePickerVC animated:YES completion:nil];
}
}else {
NSLog(@"攝像頭無法使用");
}