當你第一次在應(yīng)用中打開相冊的時候,系統(tǒng)會提示你是否允許用戶訪問相冊,如果你選擇不允許的話,你以后在該應(yīng)用中將無法訪問相冊。如果想要重新允許,那么需要去“隱私設(shè)置”里面去設(shè)置。在程序中怎么獲取用戶是否擁有對相冊的訪問權(quán)限,然后做相應(yīng)地操作呢,下面列出了相冊的權(quán)限
iOS8之前
// 所在的庫:<AssetsLibrary/AssetsLibrary.h>
typedef NS_ENUM(NSInteger, ALAuthorizationStatus) {
ALAuthorizationStatusNotDetermined = 0, // 用戶還沒有做出選擇
ALAuthorizationStatusRestricted, // 家長控制,不允許訪問
ALAuthorizationStatusDenied, // 用戶拒絕當前應(yīng)用訪問相冊,我們需要提醒用戶打開訪問開關(guān)
ALAuthorizationStatusAuthorized // 用戶允許當前應(yīng)用訪問相冊
}
// 獲取當前照片庫授權(quán)狀態(tài)的方法,可以通過判斷這個狀態(tài),來決定是否要提醒用戶到設(shè)置中開啟服務(wù)
ALAuthorizationStatus status = [ALAssetsLibrary authorizationStatus];
iOS8之后
// 所在的庫:<Photos/Photos.h>
typedef NS_ENUM(NSInteger, PHAuthorizationStatus) {
PHAuthorizationStatusNotDetermined = 0, // 用戶還沒有做出選擇
PHAuthorizationStatusRestricted, // 家長控制,不允許訪問
PHAuthorizationStatusDenied, // 用戶拒絕當前應(yīng)用訪問相冊,我們需要提醒用戶打開訪問開關(guān)
PHAuthorizationStatusAuthorized // 用戶允許當前應(yīng)用訪問相冊
}
// 獲取當前照片庫授權(quán)狀態(tài)的方法,可以通過判斷這個狀態(tài),來決定是否要提醒用戶到設(shè)置中開啟服務(wù)
PHAuthorizationStatus status = [PHPhotoLibrary authorizationStatus];
在ios7之前攝像頭是一直可以訪問的,隱私設(shè)置選項中沒有關(guān)閉相應(yīng)軟件攝像頭功能的選項。在ios7以后攝像頭和相冊一樣增加了訪問權(quán)限的設(shè)置,應(yīng)用中第一次訪問攝像頭的時候,系統(tǒng)會詢問你是否授權(quán)應(yīng)用訪問你的攝像頭。攝像頭的權(quán)限和相冊的權(quán)限基本上一樣
// 所在的庫:<AVFoundation/AVFoundation.h>
typedef NS_ENUM(NSInteger, AVAuthorizationStatus) {
AVAuthorizationStatusNotDetermined = 0, // 用戶還沒有做出選擇
AVAuthorizationStatusRestricted, // 家長控制,不允許訪問
AVAuthorizationStatusDenied, // 用戶拒絕當前應(yīng)用訪問相冊,我們需要提醒用戶打開訪問開關(guān)
AVAuthorizationStatusAuthorized // 用戶允許當前應(yīng)用訪問相機
}
// 獲取當前攝像頭授權(quán)狀態(tài)的方法,可以通過判斷這個狀態(tài),來決定是否要提醒用戶到設(shè)置中開啟服務(wù)
AVAuthorizationStatus status = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
如果是iOS8之后,則可以直接跳轉(zhuǎn)到APP設(shè)置面板
+ (void)goAppSetting
{
// 打開應(yīng)用設(shè)置面板
NSURL *appSettingUrl = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
UIApplication *app = [UIApplication sharedApplication];
if ([app canOpenURL:appSettingUrl])
{
if ([UIDevice currentDevice].systemVersion.floatValue >= 10.0)
{
[app openURL:appSettingUrl options:@{} completionHandler:^(BOOL success) {
if (!success) [self showJumpErrorAlert];
}];
}
else
{
#pragma clang diagnostic push
#pragma clang diagnostic ignored"-Wdeprecated-declarations"
[app openURL:appSettingUrl];
#pragma clang diagnostic pop
}
}
else
{
[self showJumpErrorAlert];
}
}
+ (void)showJumpErrorAlert
{// DVVAlertView GitHub 鏈接:https://github.com/devdawei/DVVAlertView.git
[DVVAlertView showAlertWithTitle:@"跳轉(zhuǎn)失敗"
message:@"請手動到設(shè)置中打開服務(wù)"
buttonTitles:@[@"取消"]
completion:nil];
}
iOS10需要在info.plist中加入訪問相機和相冊的使用權(quán)限
相機
Key:Privacy - Camera Usage Description
Type:String
相冊
Key:Privacy - Photo Library Usage Description
Type:String