1. 程式人生 > >iOS判斷一些權限是否被禁止

iOS判斷一些權限是否被禁止

com -a imageview 技術 val 接口 sre epo ogr

iOS中經常會遇到訪問相冊、相機、麥克瘋、藍牙、以及推送等權限,所以每次我們要使用這些權限是都要記得查看用戶是否允許了,如果用戶禁止了你的訪問權限,你仍然去調取相冊或者相機等,那麽就會先出現下面的這個提示。而且是英文的,這時候用戶可能有些懵逼了,這個時候我們最好給一個提示,用戶點擊確定後,我們最好貼心的跳轉到應用的權限出,讓用戶一鍵允許。

技術分享
權限被禁用

1.查看相冊權限是否被禁用

(1.)iOS7之前的判斷方法(包含iOS7)

導入頭文件#import <AssetsLibrary/AssetsLibrary.h>

下面是判斷是否有權限的代碼

ALAuthorizationStatus author =[ALAssetsLibrary authorizationStatus];

if (author == ALAuthorizationStatusRestricted || author ==ALAuthorizationStatusDenied){

//無權限 這個時候最好給個提示,用戶點擊是就跳轉到應用的權限設置內 用戶動動小手即可允許權限

}

下面是ALAuthorizationStatus的枚舉

typedef NS_ENUM(NSInteger, ALAuthorizationStatus) {

ALAuthorizationStatusNotDetermined NS_ENUM_DEPRECATED_IOS(6_0, 9_0) = 0, // 用戶尚未做出選擇這個應用程序的問候

ALAuthorizationStatusRestricted NS_ENUM_DEPRECATED_IOS(6_0, 9_0), // 此應用程序沒有被授權訪問的照片數據。可能是家長控制權限

ALAuthorizationStatusDenied NS_ENUM_DEPRECATED_IOS(6_0, 9_0), // 用戶已經明確否認了權限的訪問

ALAuthorizationStatusAuthorized NS_ENUM_DEPRECATED_IOS(6_0, 9_0) // 用戶已經授權應用訪問照片數據

} NS_DEPRECATED_IOS(6_0, 9_0, "Use PHAuthorizationStatus in the Photos framework instead");

(2)iOS8之後的判斷方法(包含iOS8)

導入頭文件#import<Photos/Photos.h>

判斷代碼

PHAuthorizationStatus status = [PHPhotoLibrary authorizationStatus];

if (status == PHAuthorizationStatusRestricted ||

status == PHAuthorizationStatusDenied) {

//無權限 這個時候最好給個提示,用戶點擊是就跳轉到應用的權限設置內 用戶動動小手即可允許權限

}

typedef NS_ENUM(NSInteger, PHAuthorizationStatus) {

PHAuthorizationStatusNotDetermined = 0,// 用戶尚未做出選擇這個應用程序的問候

PHAuthorizationStatusRestricted, // 此應用程序沒有被授權訪問的照片數據。可能是家長控制權限

PHAuthorizationStatusDenied, // 用戶已經明確否認了權限的訪問

PHAuthorizationStatusAuthorized //用戶已經授權應用訪問照片數據

} PHOTOS_AVAILABLE_IOS_TVOS(8_0, 10_0);

2.查看相機權限是否被允許訪問

#import <AVFoundation/AVCaptureDevice.h>

AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];

if (authStatus == AVAuthorizationStatusRestricted || authStatus ==AVAuthorizationStatusDenied)

{

//無權限

}

typedefNS_ENUM(NSInteger,AVAuthorizationStatus) {AVAuthorizationStatusNotDetermined=0,// 系統還未知是否訪問,第一次開啟相機時AVAuthorizationStatusRestricted,// 受限制的AVAuthorizationStatusDenied,//不允許AVAuthorizationStatusAuthorized// 允許狀態}NS_AVAILABLE_IOS(7_0) __TVOS_PROHIBITED;

3.查看麥克風權限是否被允許訪問

#import <AVFoundation/AVCaptureDevice.h>

AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeAudio];

if (authStatus == AVAuthorizationStatusRestricted || authStatus ==AVAuthorizationStatusDenied)

{

//無權限

}

我靠,怎麽感覺哪裏不對?為什麽跟查看相機權限一樣?細心的小夥伴或許已經發現兩者的差別只有一個參數不一樣AVMediaTypeVideo,AVMediaTypeAudio,當然判斷結果的枚舉也是一樣啦,這裏不再贅述。

4.判斷用戶是否允許推送

其中iOS8以上與iOS8以下有些區別,所以需要進行iOS版本判斷。

#define IOS8 ([[[UIDevice currentDevice] systemVersion] doubleValue] >=8.0 ? YES : NO)

if (IOS8) { //iOS8以上包含iOS8

if ([[UIApplication sharedApplication] currentUserNotificationSettings].types ==UIUserNotificationTypeNone) {

NSLog(@"沒有開啟");

}

}else{ // ios7 一下

if ([[UIApplication sharedApplication] enabledRemoteNotificationTypes] == UIRemoteNotificationTypeNone) {

NSLog(@"沒有開啟");

}

}

typedef NS_OPTIONS(NSUInteger, UIUserNotificationType) {

UIUserNotificationTypeNone = 0, // 用戶禁止了推送

UIUserNotificationTypeBadge = 1 << 0, // 用戶開啟了推送角標

UIUserNotificationTypeSound = 1 << 1, // 用戶開啟了推送提示音

UIUserNotificationTypeAlert = 1 << 2, // 用戶開啟了通知欄提醒

} NS_ENUM_DEPRECATED_IOS(8_0, 10_0, "Use UserNotifications Framework‘s UNAuthorizationOptions") __TVOS_PROHIBITED;

5.判斷是否打開了藍牙,需要打開時跳轉到設置讓用戶打開

其實在我們使用藍牙的時候即創建時就需要遵循CBCentralManagerDelegate這個代理,他有一個代理方法是不停的監控藍牙狀態的變化。

- (void)centralManagerDidUpdateState:(CBCentralManager *)central{

switch (central.state) {

case CBManagerStateUnknown:

{

// 初始的時候是未知的(剛剛創建的時候)

}

break;

case CBManagerStateResetting:

{

//正在重置狀態

}

break;

case CBManagerStateUnsupported:

{

//設備不支持的狀態

}

break;

case CBManagerStateUnauthorized:

{

[stringForCentral appendString:@"Resetting\n"];

// 設備未授權狀態

}

break;

case CBManagerStatePoweredOff:

{

//設備關閉狀態

}

break;

case CBManagerStatePoweredOn:

{

// 設備開啟狀態 -- 可用狀態

}

break;

default:

{

}

break;

}

}

}

我們可以在不同的狀態下做一些事情。當然我們也可以通過CBCentralManager的state方法獲取藍牙的鏈接狀態,還有就是我們創建CBCentralManager系統如果發現藍牙沒有開啟會自動彈出一個窗口可以去設置裏面打開藍牙。

技術分享

當然如果我們想自己給一個用戶提示然後跳轉到設置頁面也是可以的,iOS10以後打開方式有些區別

NSString * urlString = @"App-Prefs:root=Bluetooth";

if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:urlString]]) {

if (IOS_VERSION>10.0) {

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString] options:@{} completionHandler:nil];

} else {

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString]];

}

}

6.判斷位置服務是否被禁用

if([CLLocationManager locationServicesEnabled] && [CLLocationManager authorizationStatus] == kCLAuthorizationStatusDenied) {

NSLog(@"沒打開");

}

locationServicesEnabled這個返回的結果是否設置過位置服務,大概是這個意思,我們第一次訪問位置是,系統會給用戶一個提示,是否允許app使用位置信息。如果你選擇了是或者否,這個值就是YES,kCLAuthorizationStatusDenied代表用戶明確拒絕了訪問位置信息。

typedef NS_ENUM(int, CLAuthorizationStatus) {

kCLAuthorizationStatusNotDetermined = 0,//定位服務授權狀態是用戶沒有決定是否使用定位服務。

kCLAuthorizationStatusRestricted,//定位服務授權狀態是受限制的。可能是由於活動限制定位服務,用戶不能改變。這個狀態可能不是用戶拒絕的定位服務。

kCLAuthorizationStatusDenied,//定位服務授權狀態已經被用戶明確禁止,或者在設置裏的定位服務中關閉。

kCLAuthorizationStatusAuthorizedAlways NS_ENUM_AVAILABLE(10_12, 8_0),//定位服務授權狀態已經被用戶允許在任何狀態下獲取位置信息。包括監測區域、訪問區域、或者在有顯著的位置變化的時候。

kCLAuthorizationStatusAuthorizedWhenInUse NS_ENUM_AVAILABLE(NA, 8_0),//定位服務授權狀態僅被允許在使用應用程序的時候。

kCLAuthorizationStatusAuthorized NS_ENUM_DEPRECATED(10_6, NA, 2_0, 8_0, "Use kCLAuthorizationStatusAuthorizedAlways") __TVOS_PROHIBITED __WATCHOS_PROHIBITED = kCLAuthorizationStatusAuthorizedAlways//這個枚舉值已經被廢棄了。他相當於

kCLAuthorizationStatusAuthorizedAlways這個值。

};

跳轉到設置頁面,讓用戶設置權限

如果我們需要跳轉到設置位置讓用戶允許權限的方法是

NSURL * url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];

if ([[UIApplication sharedApplication] canOpenURL:url]) {

if (IOS_VERSION>10.0) {

[[UIApplication sharedApplication] openURL:url options:@{} completionHandler:nil];

} else {

[[UIApplication sharedApplication] openURL:url];

}

}

調用這個方法時,一定要有這些權限的需求時才能調用,比如本身你的應用不涉及到任何的隱私權限問題,你直接調用這個接口他不會去設置,而是到home頁面。

iOS判斷一些權限是否被禁止