iOS判斷一些許可權是否被禁止

來源:互聯網
上載者:User

標籤:epo   自己   tin   bluetooth   bad   服務   support   stat   original   

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判斷一些許可權是否被禁止

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.