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
- android強制申請許可權Android
- pg許可權相關
- delphi安卓動態許可權申請安卓
- 如何用 Vue 實現前端許可權控制(路由許可權 + 檢視許可權 + 請求許可權)Vue前端路由
- 在Android上優雅的申請許可權Android
- Android優雅地申請動態許可權Android
- 原生Android之(6.0及以上)許可權申請Android
- Android開發在Activity外申請許可權呼叫相機開啟相簿Android
- android 6.0許可權機制的簡單封裝(支援批量申請許可權)Android封裝
- android 6.0許可權申請機制(簡單案例)Android
- APP許可權相關的東西APP
- Android開發-更”聰明”的申請許可權方式Android
- Delphi 7 編譯軟體申請管理員許可權編譯
- 【FAQ】申請Health Kit許可權的常見問題及解答
- linux 檔案許可權 s 許可權和 t 許可權解析Linux
- 這個許可權動態申請庫,值得嘗試一下
- EasyAndroid基礎整合元件庫之:EasyPermissions 動態許可權申請庫Android元件
- 動態許可權相關的幾個庫分析
- postgresql關於訪問檢視需要的許可權SQL
- 基於全流量許可權漏洞檢測技術
- 鴻蒙Next許可權申請全攻略:系統授權與使用者授權之道鴻蒙
- 選單許可權和按鈕許可權設定
- Linux的檔案存取許可權和0644許可權Linux
- checkpoint防火牆測試授權申請防火牆
- 【Linux】淺析檔案屬性與許可權相關命令Linux
- Linux使用者/使用者組/許可權相關命令Linux
- 協同平臺檢視許可權開啟業務物件提示"當前使用者沒有許可權!請檢查使用者[BOS設計器]的[編輯]許可權與應用的編輯許可權!"物件
- 許可權修飾符和final關鍵字
- 關於動態許可權
- 關於mysql許可權管理MySql
- 許可權之選單許可權
- linux的常用操作——檢視和修改檔案許可權Linux
- 華為申請meta相關商標QEL
- 2020.9.28(Hive檢視、索引、許可權管理)Hive索引
- 賬號和許可權管理
- 許可權系統:一文搞懂功能許可權、資料許可權
- 如何檢視postgresql使用者許可權SQL