自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

iOS開發(fā)中的這些權(quán)限,你搞懂了嗎?

移動開發(fā)
APP開發(fā)避免不開系統(tǒng)權(quán)限的問題,如何在APP以更加友好的方式向用戶展示系統(tǒng)權(quán)限,似乎也是開發(fā)過程中值得深思的一件事;那如何提高APP獲取iOS系統(tǒng)權(quán)限的通過率呢?

寫在前面

APP開發(fā)避免不開系統(tǒng)權(quán)限的問題,如何在APP以更加友好的方式向用戶展示系統(tǒng)權(quán)限,似乎也是開發(fā)過程中值得深思的一件事;

iOS開發(fā)中的這些權(quán)限,你搞懂了嗎?

那如何提高APP獲取iOS系統(tǒng)權(quán)限的通過率呢?有以下幾種方式:

  • 在用戶打開APP時就向用戶請求權(quán)限;
  • 告知用戶授權(quán)權(quán)限后能夠獲得好處之后,再向用戶請求權(quán)限;
  • 在絕對必要的情況下才向用戶請求權(quán)限,例如:用戶訪問照片庫時請求訪問系統(tǒng)相冊權(quán)限;
  • 在展示系統(tǒng)權(quán)限的對話框前,先向用戶顯示自定義的對話框,若用戶選擇不允許,默認無操作,若用戶選擇允許,再展示系統(tǒng)對話框。

上述情況在開發(fā)過程中是經(jīng)常遇到的,不同方式的選擇會影響最后用戶交互體驗。這一點感悟正是源于上一周工作遇到的問題:適配iOS10,如何獲取應(yīng)用聯(lián)網(wǎng)權(quán)限用以管理系統(tǒng)對話框的顯示管理。當我把這個問題解決后,感覺有必要將常用的iOS系統(tǒng)權(quán)限做一個總結(jié),以便后用。

權(quán)限分類

  • 聯(lián)網(wǎng)權(quán)限
  • 相冊權(quán)限
  • 相機、麥克風權(quán)限
  • 定位權(quán)限
  • 推送權(quán)限
  • 通訊錄權(quán)限
  • 日歷、備忘錄權(quán)限

聯(lián)網(wǎng)權(quán)限

引入頭文件 @import CoreTelephony;

應(yīng)用啟動后,檢測應(yīng)用中是否有聯(lián)網(wǎng)權(quán)限

  1. CTCellularData *cellularData = [[CTCellularData alloc]init]; 
  2. cellularData.cellularDataRestrictionDidUpdateNotifier =  ^(CTCellularDataRestrictedState state){ 
  3.   //獲取聯(lián)網(wǎng)狀態(tài) 
  4.   switch (state) { 
  5.       case kCTCellularDataRestricted: 
  6.           NSLog(@"Restricrted"); 
  7.           break; 
  8.       case kCTCellularDataNotRestricted: 
  9.           NSLog(@"Not Restricted"); 
  10.           break; 
  11.       case kCTCellularDataRestrictedStateUnknown: 
  12.           NSLog(@"Unknown"); 
  13.           break; 
  14.       default
  15.           break; 
  16.   }; 
  17. }; 

查詢應(yīng)用是否有聯(lián)網(wǎng)功能

  1. CTCellularData *cellularData = [[CTCellularData alloc]init]; 
  2. CTCellularDataRestrictedState state = cellularData.restrictedState; 
  3.  switch (state) { 
  4.   case kCTCellularDataRestricted: 
  5.       NSLog(@"Restricrted"); 
  6.       break; 
  7.   case kCTCellularDataNotRestricted: 
  8.       NSLog(@"Not Restricted"); 
  9.       break; 
  10.   case kCTCellularDataRestrictedStateUnknown: 
  11.       NSLog(@"Unknown"); 
  12.       break; 
  13.   default
  14.       break; 

相冊權(quán)限--IOS 9.0之前

導(dǎo)入頭文件@import AssetsLibrary;

檢查是否有相冊權(quán)限

  1. ALAuthorizationStatus status = [ALAssetsLibrary authorizationStatus]; 
  2. switch (status) { 
  3.   case ALAuthorizationStatusAuthorized: 
  4.       NSLog(@"Authorized"); 
  5.       break; 
  6.   case ALAuthorizationStatusDenied: 
  7.       NSLog(@"Denied"); 
  8.       break; 
  9.   case ALAuthorizationStatusNotDetermined: 
  10.       NSLog(@"not Determined"); 
  11.       break; 
  12.   case ALAuthorizationStatusRestricted: 
  13.       NSLog(@"Restricted"); 
  14.       break; 
  15.  
  16.   default
  17.       break; 

相冊權(quán)限--IOS 8.0之后

導(dǎo)入頭文件@import Photos;

檢查是否有相冊權(quán)限

  1. PHAuthorizationStatus photoAuthorStatus = [PHPhotoLibrary authorizationStatus]; 
  2. switch (photoAuthorStatus) { 
  3.   case PHAuthorizationStatusAuthorized: 
  4.       NSLog(@"Authorized"); 
  5.       break; 
  6.   case PHAuthorizationStatusDenied: 
  7.       NSLog(@"Denied"); 
  8.       break; 
  9.   case PHAuthorizationStatusNotDetermined: 
  10.       NSLog(@"not Determined"); 
  11.       break; 
  12.   case PHAuthorizationStatusRestricted: 
  13.       NSLog(@"Restricted"); 
  14.       break; 
  15.   default
  16.       break; 

![Uploading 144446-b8aca7ba38c5f8c0_695906.png . . .]獲取相冊權(quán)限

  1. [PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) { 
  2.     if (status == PHAuthorizationStatusAuthorized) { 
  3.         NSLog(@"Authorized"); 
  4.     }else
  5.         NSLog(@"Denied or Restricted"); 
  6.     } 
  7.     }]; 

相機和麥克風權(quán)限

導(dǎo)入頭文件@import AVFoundation;

檢查是否有相機或麥克風權(quán)限

  1. AVAuthorizationStatus AVstatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];//相機權(quán)限 
  2. AVAuthorizationStatus AVstatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeAudio];//麥克風權(quán)限 
  3.  
  4. switch (AVstatus) { 
  5.   case AVAuthorizationStatusAuthorized: 
  6.       NSLog(@"Authorized"); 
  7.       break; 
  8.   case AVAuthorizationStatusDenied: 
  9.       NSLog(@"Denied"); 
  10.       break; 
  11.   case AVAuthorizationStatusNotDetermined: 
  12.       NSLog(@"not Determined"); 
  13.       break; 
  14.   case AVAuthorizationStatusRestricted: 
  15.       NSLog(@"Restricted"); 
  16.       break; 
  17.   default
  18.       break; 

獲取相機或麥克風權(quán)限

  1. [AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo completionHandler:^(BOOL granted) {//相機權(quán)限 
  2.   if (granted) { 
  3.       NSLog(@"Authorized"); 
  4.   }else
  5.       NSLog(@"Denied or Restricted"); 
  6.   } 
  7. }]; 
  8.  
  9. [AVCaptureDevice requestAccessForMediaType:AVMediaTypeAudio completionHandler:^(BOOL granted) {//麥克風權(quán)限 
  10.   if (granted) { 
  11.       NSLog(@"Authorized"); 
  12.   }else
  13.       NSLog(@"Denied or Restricted"); 
  14.   } 
  15. }]; 

定位權(quán)限

導(dǎo)入頭文件@import CoreLocation;

由于iOS8.0之后定位方法的改變,需要在info.plist中進行配置; 

iOS開發(fā)中的這些權(quán)限,你搞懂了嗎?

檢查是否有定位權(quán)限

  1. BOOL isLocation = [CLLocationManager locationServicesEnabled]; 
  2. if (!isLocation) { 
  3.   NSLog(@"not turn on the location"); 
  4. CLAuthorizationStatus CLstatus = [CLLocationManager authorizationStatus]; 
  5. switch (CLstatus) { 
  6.   case kCLAuthorizationStatusAuthorizedAlways: 
  7.       NSLog(@"Always Authorized"); 
  8.       break; 
  9.   case kCLAuthorizationStatusAuthorizedWhenInUse: 
  10.       NSLog(@"AuthorizedWhenInUse"); 
  11.       break; 
  12.   case kCLAuthorizationStatusDenied: 
  13.       NSLog(@"Denied"); 
  14.       break; 
  15.   case kCLAuthorizationStatusNotDetermined: 
  16.       NSLog(@"not Determined"); 
  17.       break; 
  18.   case kCLAuthorizationStatusRestricted: 
  19.       NSLog(@"Restricted"); 
  20.       break; 
  21.   default
  22.       break; 

獲取定位權(quán)限

  1. CLLocationManager *manager = [[CLLocationManager alloc] init]; 
  2. [manager requestAlwaysAuthorization];//一直獲取定位信息 
  3. [manager requestWhenInUseAuthorization];//使用的時候獲取定位信息 

在代理方法中查看權(quán)限是否改變

  1. - (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status{ 
  2.  switch (status) { 
  3.   case kCLAuthorizationStatusAuthorizedAlways: 
  4.       NSLog(@"Always Authorized"); 
  5.       break; 
  6.   case kCLAuthorizationStatusAuthorizedWhenInUse: 
  7.       NSLog(@"AuthorizedWhenInUse"); 
  8.       break; 
  9.   case kCLAuthorizationStatusDenied: 
  10.       NSLog(@"Denied"); 
  11.       break; 
  12.   case kCLAuthorizationStatusNotDetermined: 
  13.       NSLog(@"not Determined"); 
  14.       break; 
  15.   case kCLAuthorizationStatusRestricted: 
  16.       NSLog(@"Restricted"); 
  17.       break; 
  18.   default
  19.       break; 
  20.   } 
  21.  

推送權(quán)限

檢查是否有通訊權(quán)限

  1. UIUserNotificationSettings *settings = [[UIApplication sharedApplication] currentUserNotificationSettings]; 
  2. switch (settings.types) { 
  3.   case UIUserNotificationTypeNone: 
  4.       NSLog(@"None"); 
  5.       break; 
  6.   case UIUserNotificationTypeAlert: 
  7.       NSLog(@"Alert Notification"); 
  8.       break; 
  9.   case UIUserNotificationTypeBadge: 
  10.       NSLog(@"Badge Notification"); 
  11.       break; 
  12.   case UIUserNotificationTypeSound: 
  13.       NSLog(@"sound Notification'"); 
  14.       break; 
  15.  
  16.   default
  17.       break; 

獲取推送權(quán)限

  1. UIUserNotificationSettings *setting = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge categories:nil]; 
  2. [[UIApplication sharedApplication] registerUserNotificationSettings:setting]; 

通訊錄權(quán)限

 iOS9.0之前

導(dǎo)入頭文件 @import AddressBook;

檢查是否有通訊錄權(quán)限

  1. ABAuthorizationStatus ABstatus = ABAddressBookGetAuthorizationStatus(); 
  2. switch (ABstatus) { 
  3.   case kABAuthorizationStatusAuthorized: 
  4.       NSLog(@"Authorized"); 
  5.       break; 
  6.   case kABAuthorizationStatusDenied: 
  7.       NSLog(@"Denied'"); 
  8.       break; 
  9.   case kABAuthorizationStatusNotDetermined: 
  10.       NSLog(@"not Determined"); 
  11.       break; 
  12.   case kABAuthorizationStatusRestricted: 
  13.       NSLog(@"Restricted"); 
  14.       break; 
  15.   default
  16.       break; 

獲取通訊錄權(quán)限

  1. ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULLNULL); 
  2. ABAddressBookRequestAccessWithCompletion(addressBook, ^(bool granted, CFErrorRef error) { 
  3.   if (granted) { 
  4.       NSLog(@"Authorized"); 
  5.       CFRelease(addressBook); 
  6.   }else
  7.       NSLog(@"Denied or Restricted"); 
  8.   } 
  9. }); 

iOS9.0及以后

導(dǎo)入頭文件 @import Contacts;

檢查是否有通訊錄權(quán)限

  1. CNAuthorizationStatus status = [CNContactStore authorizationStatusForEntityType:CNEntityTypeContacts]; 
  2.   switch (status) { 
  3.         case CNAuthorizationStatusAuthorized: 
  4.         { 
  5.             NSLog(@"Authorized:"); 
  6.         } 
  7.             break; 
  8.         case CNAuthorizationStatusDenied:{ 
  9.             NSLog(@"Denied"); 
  10.         } 
  11.             break; 
  12.         case CNAuthorizationStatusRestricted:{ 
  13.             NSLog(@"Restricted"); 
  14.         } 
  15.             break; 
  16.         case CNAuthorizationStatusNotDetermined:{ 
  17.              NSLog(@"NotDetermined"); 
  18.         } 
  19.             break; 
  20.  
  21.        } 

獲取通訊錄權(quán)限

  1. CNContactStore *contactStore = [[CNContactStore alloc] init]; 
  2.     [contactStore requestAccessForEntityType:CNEntityTypeContacts completionHandler:^(BOOL granted, NSError * _Nullable error) { 
  3.         if (granted) { 
  4.  
  5.            NSLog(@"Authorized"); 
  6.  
  7.         }else
  8.  
  9.            NSLog(@"Denied or Restricted"); 
  10.         } 
  11.     }]; 

日歷、備忘錄權(quán)限

導(dǎo)入頭文件

檢查是否有日歷或者備忘錄權(quán)限

  1. typedef NS_ENUM(NSUInteger, EKEntityType) { 
  2.  EKEntityTypeEvent,//日歷 
  3.  EKEntityTypeReminder //備忘 
  4. }; 
  1. EKAuthorizationStatus EKstatus = [EKEventStore  authorizationStatusForEntityType:EKEntityTypeEvent]; 
  2. switch (EKstatus) { 
  3.   case EKAuthorizationStatusAuthorized: 
  4.       NSLog(@"Authorized"); 
  5.       break; 
  6.   case EKAuthorizationStatusDenied: 
  7.       NSLog(@"Denied'"); 
  8.       break; 
  9.   case EKAuthorizationStatusNotDetermined: 
  10.       NSLog(@"not Determined"); 
  11.       break; 
  12.   case EKAuthorizationStatusRestricted: 
  13.       NSLog(@"Restricted"); 
  14.       break; 
  15.   default
  16.       break; 

獲取日歷或備忘錄權(quán)限

  1. EKEventStore *store = [[EKEventStore alloc]init]; 
  2. [store requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError * _Nullable error) { 
  3.   if (granted) { 
  4.       NSLog(@"Authorized"); 
  5.   }else
  6.       NSLog(@"Denied or Restricted"); 
  7.   } 
  8. }]; 

 最后一點

素有獲取權(quán)限的方法,多用于用戶第一次操作應(yīng)用,iOS 8.0之后,將這些設(shè)置都整合在一起,并且可以開啟或關(guān)閉相應(yīng)的權(quán)限。所有的權(quán)限都可以通過下面的方法打開:

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

上述的權(quán)限多為經(jīng)常用到的權(quán)限,當然不會很全面,大家如有需要其他的權(quán)限,可以在下方評論,我會及時加上去的。

希望這篇文章能夠給大家的開發(fā)帶來一些便利。

責任編輯:未麗燕 來源: 簡書
相關(guān)推薦

2024-08-12 15:23:43

LangChain

2019-08-28 08:57:05

2021-10-12 10:50:31

鴻蒙HarmonyOS應(yīng)用

2020-04-14 08:46:47

Java對象編譯器

2021-01-29 17:07:26

排序算法數(shù)組

2021-10-10 20:36:49

Android Root權(quán)限

2018-08-10 05:06:03

提速降費營運商漫游

2019-11-20 15:40:48

CPU軟件處理器

2023-06-16 14:10:00

TCPUDP網(wǎng)絡(luò)通信

2022-05-06 09:21:21

TypeScriptinterfacetype

2024-04-07 08:23:01

JS隔離JavaScript

2011-06-14 12:56:55

SQL Server復(fù)災(zāi)

2022-04-07 08:20:22

typeinterface前端

2022-06-07 08:14:35

PGPAGETUPLE

2022-11-28 07:10:57

2021-03-05 18:38:45

ESvue項目

2022-01-06 07:59:32

WebGPUOpenGL引擎

2022-06-06 07:58:52

勒索軟件惡意軟件解密

2024-09-30 09:28:34

軟件研發(fā)交付

2022-03-08 15:01:48

負載均衡IP服務(wù)器
點贊
收藏

51CTO技術(shù)棧公眾號