ios 判斷用戶是否開啟權限---并跳轉設置

ios 判斷用戶是否開啟權限---并跳轉“系統設置”

1.判斷 訪問相冊 或 相機 權限是否開啟

2.檢測是否開啟定位

后面將持續更新

只有在應用請求過位置權限 或者 通知權限的時候,才會跳進自己app里面的設置呢。不然直接跳到系統設置界面

//打開app定位設置

NSURL *settingsURL = [NSURL URLWithString:UIApplicationOpenSettingsURLString];

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

[[UIApplication sharedApplication] openURL:settingsURL];

}

工具/原料 Mac / Mac miniXcode方法/步驟? 1.? =====判斷 訪問相冊 或 相機 權限是否開啟====//在info.plist 里面設置? NSCameraUsageDescription Privacy - Camera Usage Description? ? ? App需要您的同意,才能訪問相機 NSPhotoLibraryUsageDescriptionPrivacy - Photo Library Usage Description? App需要您的同意,才能訪問相冊 科普://=================相冊=======================//相冊權限判斷 需要引入框架#import//相冊

===PHAuthorizationStatus===相冊權限狀態判斷

在8.0系統以后,新加入了Photos.framework框架,我們可以利用框架中的PHAuthorizationStatus進行相冊權限狀態判斷。

==判斷是否開啟相冊權限 的4中狀態

typedef NS_ENUM(NSInteger,PHAuthorizationStatus) {

//==1. 用戶還沒有關于這個應用程序做出了選擇

PHAuthorizationStatusNotDetermined = 0,

//==2. 這個應用程序未被授權訪問圖片數據。用戶不能更改該應用程序的狀態,可能是由于活動的限制,如家長控制到位。

PHAuthorizationStatusRestricted,

//==3. 用戶已經明確否認了這個應用程序訪問圖片數據

PHAuthorizationStatusDenied,

//==4. 用戶授權此應用程序訪問圖片數據

PHAuthorizationStatusAuthorized

}PHOTOS_AVAILABLE_IOS_TVOS(8_0, 10_0);

//========================相機===================== //相冊權限判斷 需要引入框架#import#import//=======AVAuthorizationStatus======判斷是否開啟相機權限 的4中狀態typedef NS_ENUM(NSInteger, AVAuthorizationStatus) {? ? ? ? ? //1. 表明用戶尚未選擇關于客戶端是否可以訪問硬件? ? ? ? AVAuthorizationStatusNotDetermined = 0,? ? ? ? //2. 客戶端未被授權訪問硬件的媒體類型。用戶不能改變客戶機的狀態,可能由于活躍的限制,如家長控制? ? ? ? AVAuthorizationStatusRestricted,? ? ? ? //3. 明確拒絕用戶訪問硬件支持的媒體類型的客戶? ? ? AVAuthorizationStatusDenied,? ? ? //4. 客戶端授權訪問硬件支持的媒體類型? ? ? AVAuthorizationStatusAuthorized? } NS_AVAILABLE_IOS(7_0) __TVOS_PROHIBITED; =================使用==================== //選擇從相冊獲取圖片//判斷狀態? 如果已經授權 則從相冊選取相片? ? ? ? ? ? ? ? ? 如果沒有授權? 則跳轉到授權設置界面? //選擇從相機獲取圖片//判斷狀態? 如果已經授權 則打開攝像頭? ? ? ? ? ? ? ? ? 如果沒有授權? 則跳轉到授權設置界面? //引入下面的框架? //相冊權限判斷 需要引入框架#import//相冊 //相冊權限判斷 需要引入框架#import#import【注意】? 控制器要遵循的協議相冊//自定義的枚舉

typedef NS_ENUM(NSInteger, ChosePhontType) {

ChosePhontTypeAlbum,? //相冊

ChosePhontTypeCamera? //相機

};

//下面部分可以直接粘貼復制使用

-(void)clickHeaderImageView{? //點擊頭像

UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"選擇相片" message:nil preferredStyle:UIAlertControllerStyleActionSheet];

UIAlertAction *album = [UIAlertAction actionWithTitle:@"相冊" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {

[self chosePhoto:ChosePhontTypeAlbum]; //從系統相冊選擇照片

}];

UIAlertAction *camera = [UIAlertAction actionWithTitle:@"相機" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {

[self chosePhoto:ChosePhontTypeCamera]; //相機

}];

UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {

//

}];

[alert addAction:album];

[alert addAction:camera];

[alert addAction:cancel];

[self presentViewController:alert animated:YES completion:^{

}];

}

//==========訪問系統? 相冊 / 相機? ===============

- (void)chosePhoto:(ChosePhontType)type{

UIImagePickerController *piker = [[UIImagePickerController alloc] init];

piker.delegate = self;

piker.allowsEditing = YES;

if (type == ChosePhontTypeAlbum) {? // 相冊

//======判斷 訪問相冊 權限是否開啟=======

PHAuthorizationStatus status = [PHPhotoLibrary authorizationStatus];

//有被授權訪問的照片數據? 用戶已經明確否認了這一照片數據的應用程序訪問

if (status == PHAuthorizationStatusRestricted ||

status == PHAuthorizationStatusDenied) {

//====沒有權限====

UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"去開啟訪問相冊權限?" message:nil preferredStyle:UIAlertControllerStyleAlert];

UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {

}];

UIAlertAction *ok = [UIAlertAction actionWithTitle:@"確定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {

//===無權限 引導去開啟===

[self openJurisdiction];

}];

// 將UIAlertAction添加到UIAlertController中

[alertController addAction:cancel];

[alertController addAction:ok];

// present顯示

[self presentViewController:alertController animated:YES completion:nil];

}else{? ? //====有訪問相冊的權限=======

piker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;

}

}else if (type == ChosePhontTypeCamera) {? // 相機

//======判斷 訪問相機 權限是否開啟=======

AVAuthorizationStatus authStatus =? [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];

//===無權限====

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

//====沒有權限====

UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"去開啟訪問相機權限?" message:nil preferredStyle:UIAlertControllerStyleAlert];

UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {

}];

UIAlertAction *ok = [UIAlertAction actionWithTitle:@"確定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {

//===無權限 引導去開啟===

[self openJurisdiction];

}];

// 將UIAlertAction添加到UIAlertController中

[alertController addAction:cancel];

[alertController addAction:ok];

// present顯示

[self presentViewController:alertController animated:YES completion:nil];

}else{? //===有權限======

if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {? //相機可用

piker.sourceType = UIImagePickerControllerSourceTypeCamera;

}else{? // 相機不可用

[SVProgressHUD showErrorWithStatus:@"相機不可用"];

return;

}

}

}

[self presentViewController:piker animated:YES completion:^{

}];

}

#pragma mark-------去設置界面開啟權限----------

-(void)openJurisdiction{

NSURL *url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];

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

[[UIApplication sharedApplication] openURL:url];

}

}

#pragma mark UIImagePickerController回調方法================

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info { //選取的照片

//選取的照片

UIImage *image = info[UIImagePickerControllerEditedImage];

_tableViewHeaderView.headerV.image = image;

[self dismissViewControllerAnimated:YES completion:nil];

}

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker { //取消選擇

[self dismissViewControllerAnimated:YES completion:nil];

}

//2.=======檢測是否開啟定位======? //在info.plist 里面配置NSLocationWhenInUseUsageDescriptionPrivacy - Location When In Use Usage Description? ? App需要使用定位功能 NSLocationAlwaysUsageDescriptionPrivacy - Location Always Usage Description? ? App需要使用定位功能 引入框架? #import//定位遵循協議//=========CLAuthorizationStatus=========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? ? }; 【注意】//1.//判斷定位是否開啟是判斷的整個手機系統的定位是否打開,并不是針對這一應用//[CLLocationManager locationServicesEnabled] //跳轉到? 整個手機系統的“定位”設置界面 [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"prefs:root=LOCATION_SERVICES"]]; //2.//跳轉至 系統的權限設置界面 NSURL *settingsURL = [NSURL URLWithString:UIApplicationOpenSettingsURLString];? ? ? ? [[UIApplication sharedApplication] openURL:settingsURL];? //================使用=================引入框架? #import//定位遵循協議//當前狀態CLAuthorizationStatus status = [CLLocationManager authorizationStatus];if ([CLLocationManager locationServicesEnabled] && [CLLocationManager authorizationStatus] != kCLAuthorizationStatusDenied) {? ? ? ? ? //定位開啟? ? }? //全局變量CLLocationManager * locationManager; NSString * currentCity; //當前城市 NSString *prv; //當前省? -(void)addLocation{? //開始定位? ? ? ? //判斷定位是否開啟是判斷的整個手機系統的定位是否打開,并不是針對這一應用? ? //判斷定位功能是否打開? ? if ([CLLocationManager locationServicesEnabled]) {? ? ? ? ? ? ? ? locationManager = [[CLLocationManager alloc] init];? ? ? ? locationManager.delegate = self;? //遵循協議? ? ? ? //精確定位? ? ? ? locationManager.desiredAccuracy = kCLLocationAccuracyBest;? ? ? ? [locationManager requestWhenInUseAuthorization];? //使用時定位? ? ? ? currentCity = [[NSString alloc] init];? ? ? ? [locationManager startUpdatingLocation];? //開始定位? ? }}#pragma mark CoreLocation delegate----- 定位---- //定位失敗則執行此代理方法//定位失敗彈出提示框,點擊"打開定位"按鈕,會打開系統的設置,提示打開定位服務- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error {? ? ? ? UIAlertController * alertVC = [UIAlertController alertControllerWithTitle:@"允許\"定位\"提示" message:@"請在設置中打開定位" preferredStyle:UIAlertControllerStyleAlert];? ? UIAlertAction * ok = [UIAlertAction actionWithTitle:@"打開定位" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {? ? ? ? ? ? ? ? //打開app定位設置? ? ? ? NSURL *settingsURL = [NSURL URLWithString:UIApplicationOpenSettingsURLString];? ? ? ? [[UIApplication sharedApplication] openURL:settingsURL];? ? }];? ? UIAlertAction * cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {? ? ? ? ? ? }];? ? [alertVC addAction:cancel];? ? [alertVC addAction:ok];? ? [self presentViewController:alertVC animated:YES completion:nil];? ? }//定位成功- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray*)locations {? ? //? ? [locationManager stopUpdatingLocation];? ? CLLocation *currentLocation = [locations lastObject];? ? CLGeocoder * geoCoder = [[CLGeocoder alloc] init];? ? ? ? //反編碼? ? [geoCoder reverseGeocodeLocation:currentLocation completionHandler:^(NSArray* _Nullable placemarks, NSError * _Nullable error) {? ? ? ? if (placemarks.count > 0) {? ? ? ? ? ? CLPlacemark *placeMark = placemarks[0];? ? ? ? ? ? currentCity = placeMark.locality;? ? ? ? ? ? if (!currentCity) {? ? ? ? ? ? ? ? currentCity = @"無法定位當前城市";? ? ? ? ? ? }? ? ? ? ? ? NSLog(@"%@",currentCity); //這就是當前的城市? ? ? ? ? ? NSLog(@"%@",placeMark.name);//具體地址:? xx市xx區xx街道? ? ? ? ? ? //administrativeArea? 省? ? ? ? ? ? NSLog(@"%@",placeMark.administrativeArea);? ? ? ? }? ? ? ? else if (error == nil && placemarks.count == 0) {? ? ? ? ? ? NSLog(@"No location and error return");? ? ? ? }? ? ? ? else if (error) {? ? ? ? ? ? NSLog(@"location error: %@ ",error);? ? ? ? }? ? ? ? ? ? }];}? //3.=======檢測是否允許消息推送======#import//====方法一

+ (BOOL)isAllowedNotification {

//

if ([UIDevice isSystemVersioniOS8]) {? // >= ios8

// system is iOS8

UIUserNotificationSettings *setting = [[UIApplication sharedApplication? ] currentUserNotificationSettings];

if (UIUserNotificationTypeNone != setting.types) {

return YES;

}

} else {//iOS7

UIRemoteNotificationType type = [[UIApplication sharedApplication] enabledRemoteNotificationTypes];

if(UIRemoteNotificationTypeNone != type) {

return YES;

}else

}

return NO;

}

+ (BOOL)isSystemVersioniOS8 {

//check systemVerson of device

UIDevice *device = [UIDevice currentDevice];

float sysVersion = [device.systemVersion floatValue];

if (sysVersion >= 8.0f) {

return YES;

}

return NO;

}

//====方法二

if ([[UIDevice currentDevice].systemVersion floatValue]>=8.0f) {

UIUserNotificationSettings *setting = [[UIApplication sharedApplication] currentUserNotificationSettings];

if (UIUserNotificationTypeNone == setting.types) {

NSLog(@"推送關閉");

}else{

NSLog(@"推送打開");

}

}else{

UIRemoteNotificationType type = [[UIApplication sharedApplication] enabledRemoteNotificationTypes];

if(UIRemoteNotificationTypeNone == type){

NSLog(@"推送關閉");

}else{

NSLog(@"推送打開");

}

}

// 去設置

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

//===方法三

+ (void)isOpenMessageNotificationServiceWithBlock:(ReturnBlock)returnBlock

{

BOOL isOpen = NO;

#if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_8_0

UIUserNotificationSettings *setting = [[UIApplication sharedApplication] currentUserNotificationSettings];

if (setting.types != UIUserNotificationTypeNone) {

isOpen = YES;

}

#else

UIRemoteNotificationType type = [[UIApplication sharedApplication] enabledRemoteNotificationTypes];

if (type != UIRemoteNotificationTypeNone) {

isOpen = YES;

}

#endif

if (returnBlock) {

returnBlock(isOpen);

}

}

//====方法四

+ (void)isOpenMessageNotificationServiceWithBlock:(ReturnBlock)returnBlock

{

#if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_10_0

[[UNUserNotificationCenter currentNotificationCenter] getNotificationSettingsWithCompletionHandler:^(UNNotificationSettings *settings) {

if (returnBlock) {

returnBlock(settings.authorizationStatus == UNAuthorizationStatusAuthorized);

}

}];

#elif __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_8_0

returnBlock([[UIApplication sharedApplication] isRegisteredForRemoteNotifications]);

#else

UIRemoteNotificationType type = [[UIApplication sharedApplication] enabledRemoteNotificationTypes];

if (returnBlock) {

returnBlock(type != UIRemoteNotificationTypeNone);

}

#endif

}

4

//4.

NSContactsUsageDescription -> 通訊錄

NSMicrophoneUsageDescription -> 麥克風


自:http://www.cnblogs.com/YangFuShun/p/6803115.html

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 228,316評論 6 531
  • 序言:濱河連續發生了三起死亡事件,死亡現場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機,發現死者居然都...
    沈念sama閱讀 98,481評論 3 415
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事。” “怎么了?”我有些...
    開封第一講書人閱讀 176,241評論 0 374
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 62,939評論 1 309
  • 正文 為了忘掉前任,我火速辦了婚禮,結果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當我...
    茶點故事閱讀 71,697評論 6 409
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發上,一...
    開封第一講書人閱讀 55,182評論 1 324
  • 那天,我揣著相機與錄音,去河邊找鬼。 笑死,一個胖子當著我的面吹牛,可吹牛的內容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 43,247評論 3 441
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 42,406評論 0 288
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后,有當地人在樹林里發現了一具尸體,經...
    沈念sama閱讀 48,933評論 1 334
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 40,772評論 3 354
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發現自己被綠了。 大學時的朋友給我發了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 42,973評論 1 369
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 38,516評論 5 359
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響,放射性物質發生泄漏。R本人自食惡果不足惜,卻給世界環境...
    茶點故事閱讀 44,209評論 3 347
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 34,638評論 0 26
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 35,866評論 1 285
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個月前我還...
    沈念sama閱讀 51,644評論 3 391
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 47,953評論 2 373

推薦閱讀更多精彩內容