iOS 判斷相冊權限、判斷相機權限、判斷定位權限

1.判斷用戶是否有權限訪問相冊

#import <AssetsLibrary/AssetsLibrary.h>
ALAuthorizationStatus author = [ALAssetsLibraryauthorizationStatus];
if (author == ALAuthorizationStatusRestricted || author ==ALAuthorizationStatusDenied){
    //無權限 做一個友好的提示
    UIAlertView * alart = [[UIAlertView alloc]initWithTitle:@"溫馨提示" message:@"請您設置允許APP訪問您的相冊\n設置>隱私>照片" delegate:self cancelButtonTitle:@"確定" otherButtonTitles:nil, nil];
    [alart show];
    return ;
} else {
    //打開相冊
}

ALAuthorizationStatus是一個系統(tǒng)的枚舉。含義如下。

typedef NS_ENUM(NSInteger, AVAuthorizationStatus) {
AVAuthorizationStatusNotDetermined = 0,// 用戶尚未做出選擇這個應用程序的問候
AVAuthorizationStatusRestricted,// 此應用程序沒有被授權訪問的照片數(shù)據(jù)。可能是家長控制權限
AVAuthorizationStatusDenied,// 用戶已經(jīng)明確否認了這一照片數(shù)據(jù)的應用程序訪問
AVAuthorizationStatusAuthorized// 用戶已經(jīng)授權應用訪問照片數(shù)據(jù)
 } NS_AVAILABLE_IOS(7_0) __TVOS_PROHIBITED;

2、判斷用戶是否有權限訪問相機
iOS7之前都可以訪問相機,iOS7之后訪問相機有權限設置

#import <AVFoundation/AVCaptureDevice.h>
#import <AVFoundation/AVMediaFormat.h>
AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
if (authStatus == AVAuthorizationStatusRestricted || authStatus ==AVAuthorizationStatusDenied)
{
    //無權限 做一個友好的提示
    UIAlertView * alart = [[UIAlertView alloc]initWithTitle:@"溫馨提示" message:@"請您設置允許APP訪問您的相機\n設置>隱私>相機" delegate:self cancelButtonTitle:@"確定" otherButtonTitles:nil, nil];
    [alart show];
    return ;
} else {
    //調(diào)用相機
}

3、判斷定位服務是否可用

if ([CLLocationManager locationServicesEnabled] &&
    ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusAuthorized
     || [CLLocationManager authorizationStatus] == kCLAuthorizationStatusNotDetermined)) {
        //定位功能可用,開始定位
        _locationManger = [[CLLocationManager alloc] init];
        locationManger.delegate = self;
        [locationManger startUpdatingLocation];
    }
else if ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusDenied){
    NSlog("定位功能不可用,提示用戶或忽略");
}

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

推薦閱讀更多精彩內(nèi)容