iOS相關許可權檢測和申請
iOS許可權相關的檢測和申請
在iOS開發過程中常用到的許可權整理如下:
- 相簿許可權檢測
PHAuthorizationStatus status = [PHPhotoLibrary authorizationStatus];
switch (status) {
case PHAuthorizationStatusNotDetermined:
NSLog(@"使用者還未做出任何選擇");
break;
case PHAuthorizationStatusRestricted:
NSLog(@"許可權收到限制,可能是家長控制許可權");
break;
case PHAuthorizationStatusDenied:
NSLog(@"沒有授權");
break;
case PHAuthorizationStatusAuthorized:
NSLog(@"已經授權");
break;
default:
break;
}
相簿許可權申請
[PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) {
if (status == PHAuthorizationStatusAuthorized) {
NSLog(@"同意授權");
}else{
NSLog(@"未同意或未選擇");
}
}];
- 相機許可權檢測
AVAuthorizationStatus status = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
if (status == AVAuthorizationStatusAuthorized) {
NSLog(@"有許可權");
}else{
NSLog(@"沒有");
}
相機許可權申請
[AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo completionHandler:^(BOOL granted) {
if (granted) {
NSLog(@"同意");
}
}];
麥克風
把上面的AVMediaTypeVideo換成AVMediaTypeAudio即可網路許可權
需要進入標頭檔案#import <CoreTelephony/CTCellularData.h
CTCellularData *cellularData = [CTCellularData new];
CTCellularDataRestrictedState state = [cellularData restrictedState];
if (state == kCTCellularDataRestricted) {
NSLog(@"kCTCellularDataRestricted");
}else if (state == kCTCellularDataNotRestricted){
NSLog(@"kCTCellularDataNotRestricted");
}else{
NSLog(@"kCTCellularDataRestrictedStateUnknown");
}
- 推送許可權檢測
UIUserNotificationSettings *setting = [[UIApplication sharedApplication] currentUserNotificationSettings];
switch (setting.types) {
case UIUserNotificationTypeNone:
NSLog(@"在收到通知後,應用程式可能不呈現任何UI。");
break;
case UIUserNotificationTypeBadge:
NSLog(@"應用程式可以在收到通知時標記其圖示。");
break;
case UIUserNotificationTypeSound:
NSLog(@"應用程式可以在接收到通知時播放聲音。");
break;
case UIUserNotificationTypeAlert:
NSLog(@"應用程式可以在接收到通知時顯示警報。");
break;
default:
break;
}
推送許可權申請
UIUserNotificationSettings *requeatSet = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert | UIUserNotificationTypeSound | UIUserNotificationTypeBadge categories:nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:requeatSet];
- 通訊錄許可權檢測
//ios9之前,檢測
ABAuthorizationStatus status = ABAddressBookGetAuthorizationStatus();
if (status == kABAuthorizationStatusAuthorized) {
NSLog(@"有許可權");
}else{
NSLog(@"沒有");
}
//iOS9及以上,檢測
CNAuthorizationStatus statu = [CNContactStore authorizationStatusForEntityType:CNEntityTypeContacts];
if (statu == CNAuthorizationStatusAuthorized) {
NSLog(@"有許可權");
}else{
NSLog(@"沒有");
}
通訊錄許可權申請
//ios9之前,申請
ABAddressBookRef ref = ABAddressBookCreate();
ABAddressBookRequestAccessWithCompletion(ref, ^(bool granted, CFErrorRef error) {
if (granted) {
NSLog(@"同意");
}
});
//iOS9及以上,申請
CNContactStore *store = [CNContactStore new];
[store requestAccessForEntityType:CNEntityTypeContacts completionHandler:^(BOOL granted, NSError * _Nullable error) {
if (granted) {
NSLog(@"同意");
}
}];
- 定位許可權檢測
引入標頭檔案#import <CoreLocation/CoreLocation.h>
CLAuthorizationStatus status = [CLLocationManager authorizationStatus];
switch (status) {
case kCLAuthorizationStatusNotDetermined:
NSLog(@"kCLAuthorizationStatusNotDetermined");
break;
case kCLAuthorizationStatusRestricted:
NSLog(@"kCLAuthorizationStatusRestricted");
break;
case kCLAuthorizationStatusDenied:
NSLog(@"kCLAuthorizationStatusDenied");
break;
case kCLAuthorizationStatusAuthorizedAlways:
NSLog(@"kCLAuthorizationStatusAuthorizedAlways");
break;
case kCLAuthorizationStatusAuthorizedWhenInUse:
NSLog(@"kCLAuthorizationStatusAuthorizedWhenInUse");
break;
default:
break;
}
定位許可權申請
CLLocationManager *manager = [CLLocationManager new];
[manager requestAlwaysAuthorization];
[manager requestWhenInUseAuthorization];
manager.delegate = self;
//代理方法中判斷使用者的選擇
- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status{
switch (status) {
case kCLAuthorizationStatusNotDetermined:
NSLog(@"kCLAuthorizationStatusNotDetermined");
break;
case kCLAuthorizationStatusRestricted:
NSLog(@"kCLAuthorizationStatusRestricted");
break;
case kCLAuthorizationStatusDenied:
NSLog(@"kCLAuthorizationStatusDenied");
break;
case kCLAuthorizationStatusAuthorizedAlways:
NSLog(@"kCLAuthorizationStatusAuthorizedAlways");
break;
case kCLAuthorizationStatusAuthorizedWhenInUse:
NSLog(@"kCLAuthorizationStatusAuthorizedWhenInUse");
break;
default:
break;
}
}
相關文章
- Android6.0------許可權申請管理(單個許可權和多個許可權申請)Android
- oracle許可權相關檢視Oracle
- android強制申請許可權Android
- delphi安卓動態許可權申請安卓
- pg許可權相關
- Oracle許可權(二)許可權相關的動態效能檢視與資料字典檢視Oracle
- 如何用 Vue 實現前端許可權控制(路由許可權 + 檢視許可權 + 請求許可權)Vue前端路由
- 簡單幾行程式碼申請許可權行程
- Oracle許可權相關查詢Oracle
- Android優雅地申請動態許可權Android
- 在Android上優雅的申請許可權Android
- 原生Android之(6.0及以上)許可權申請Android
- android 6.0許可權機制的簡單封裝(支援批量申請許可權)Android封裝
- Android開發在Activity外申請許可權呼叫相機開啟相簿Android
- APP許可權相關的東西APP
- DB2 public許可權相關DB2
- android 6.0許可權申請機制(簡單案例)Android
- 和物件許可權相關的表table_privilege_map物件
- 系統、角色、物件相關許可權字典物件
- 系統許可權相關資料字典
- Android開發-更”聰明”的申請許可權方式Android
- 專案需求討論 - 動態許可權申請分析及相關第三方庫原始碼分析原始碼
- 【FAQ】申請Health Kit許可權的常見問題及解答
- React-Native之Android(6.0及以上)許可權申請ReactAndroid
- 如何檢測應用通知許可權?如何跳轉通知許可權設定頁?
- Oracle檢視許可權Oracle
- iOS 10 infolist 許可權iOS
- 系統許可權傳遞和物件許可權傳遞的測試物件
- linux 檔案許可權 s 許可權和 t 許可權解析Linux
- 這個許可權動態申請庫,值得嘗試一下
- 國產 Android 許可權申請最佳適配方案 —— permissions4mAndroid
- 動態許可權相關的幾個庫分析
- Oracle中使用者、角色、與許可權涉及的相關表及檢視Oracle
- MySQL資料庫許可權體系入門(6)---管理表、列及程式相關許可權MySql資料庫
- Android系統許可權和root許可權Android
- 鴻蒙Next許可權申請全攻略:系統授權與使用者授權之道鴻蒙
- EasyAndroid基礎整合元件庫之:EasyPermissions 動態許可權申請庫Android元件
- 基於全流量許可權漏洞檢測技術