App開發許可權
一、請求許可權的方式
1.開啟App時請求許可權
2.告知使用者授權益處後請求許可權
3.在必要的情況下請求許可權
4.先展示自定義對話方塊,同意後再請求許可權
二、許可權分類
1.聯網許可權
2.相簿許可權
3.相機、麥克風許可權
4.定位許可權
5.推送許可權
6.通訊錄許可權
7.日曆、備忘錄許可權
三、聯網許可權
引入標頭檔案 import CoreTelephony
CTCellularData *cellularData = [[CTCellularData alloc]init];
cellularData.cellularDataRestrictionDidUpdateNotifier = ^(CTCellularDataRestrictedState state){
//獲取聯網狀態
switch (state) {
case kCTCellularDataRestricted:
NSLog(@"Restricrted");
break;
case kCTCellularDataNotRestricted:
NSLog(@"Not Restricted");
break;
case kCTCellularDataRestrictedStateUnknown:
NSLog(@"Unknown");
break;
default:
break;
};
};
查詢應用是否有聯網功能
CTCellularData *cellularData = [[CTCellularData alloc]init];
CTCellularDataRestrictedState state = cellularData.restrictedState;
switch (state) {
case kCTCellularDataRestricted:
NSLog(@"Restricrted");
break;
case kCTCellularDataNotRestricted:
NSLog(@"Not Restricted");
break;
case kCTCellularDataRestrictedStateUnknown:
NSLog(@"Unknown");
break;
default:
break;
}
四、相簿許可權
匯入標頭檔案 import AssetsLibrary
檢查是否有相簿許可權
PHAuthorizationStatus photoAuthorStatus = [PHPhotoLibrary authorizationStatus];
switch (photoAuthorStatus) {
case PHAuthorizationStatusAuthorized:
NSLog(@"Authorized");
break;
case PHAuthorizationStatusDenied:
NSLog(@"Denied");
break;
case PHAuthorizationStatusNotDetermined:
NSLog(@"not Determined");
break;
case PHAuthorizationStatusRestricted:
NSLog(@"Restricted");
break;
default:
break;
}
[PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) {
if (status == PHAuthorizationStatusAuthorized) {
NSLog(@"Authorized");
}else{
NSLog(@"Denied or Restricted");
}
}];
六、相機和麥克風許可權
匯入標頭檔案 import AVFoundation
AVAuthorizationStatus AVstatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];//相機許可權
AVAuthorizationStatus AVstatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeAudio];//麥克風許可權
switch (AVstatus) {
case AVAuthorizationStatusAuthorized:
NSLog(@"Authorized");
break;
case AVAuthorizationStatusDenied:
NSLog(@"Denied");
break;
case AVAuthorizationStatusNotDetermined:
NSLog(@"not Determined");
break;
case AVAuthorizationStatusRestricted:
NSLog(@"Restricted");
break;
default:
break;
}
獲取相機或麥克風許可權
[AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo completionHandler:^(BOOL granted) {//相機許可權
if (granted) {
NSLog(@"Authorized");
}else{
NSLog(@"Denied or Restricted");
}
}];
[AVCaptureDevice requestAccessForMediaType:AVMediaTypeAudio completionHandler:^(BOOL granted) {//麥克風許可權
if (granted) {
NSLog(@"Authorized");
}else{
NSLog(@"Denied or Restricted");
}
}];
七、定位許可權
匯入標頭檔案 import CoreLocation
ios8.0後需要在info.plist中新增String配置:NSLocationWhenUseUsageDescription\NSLocationAlwaysUsageDesCription
檢查是否有定位許可權
BOOL isLocation = [CLLocationManager locationServicesEnabled];
if (!isLocation) {
NSLog(@"not turn on the location");
}
CLAuthorizationStatus CLstatus = [CLLocationManager authorizationStatus];
switch (CLstatus) {
case kCLAuthorizationStatusAuthorizedAlways:
NSLog(@"Always Authorized");
break;
case kCLAuthorizationStatusAuthorizedWhenInUse:
NSLog(@"AuthorizedWhenInUse");
break;
case kCLAuthorizationStatusDenied:
NSLog(@"Denied");
break;
case kCLAuthorizationStatusNotDetermined:
NSLog(@"not Determined");
break;
case kCLAuthorizationStatusRestricted:
NSLog(@"Restricted");
break;
default:
break;
}
獲取定位許可權
CLLocationManager *manager = [[CLLocationManager alloc] init];
[manager requestAlwaysAuthorization];//一直獲取定位資訊
[manager requestWhenInUseAuthorization];//使用的時候獲取定位資訊
在代理方法中檢視許可權是否改變
- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status{ switch (status) {
case kCLAuthorizationStatusAuthorizedAlways:
NSLog(@"Always Authorized");
break;
case kCLAuthorizationStatusAuthorizedWhenInUse:
NSLog(@"AuthorizedWhenInUse");
break;
case kCLAuthorizationStatusDenied:
NSLog(@"Denied");
break;
case kCLAuthorizationStatusNotDetermined:
NSLog(@"not Determined");
break;
case kCLAuthorizationStatusRestricted:
NSLog(@"Restricted");
break;
default:
break;
}
}
八、推送許可權
檢查是否有通訊許可權
UIUserNotificationSettings *settings = [[UIApplication sharedApplication] currentUserNotificationSettings];switch (settings.types) {
case UIUserNotificationTypeNone:
NSLog(@"None");
break;
case UIUserNotificationTypeAlert:
NSLog(@"Alert Notification");
break;
case UIUserNotificationTypeBadge:
NSLog(@"Badge Notification");
break;
case UIUserNotificationTypeSound:
NSLog(@"sound Notification'");
break;
default:
break;
}
獲取推動許可權
UIUserNotificationSettings *setting = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge categories:nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:setting];
九、通訊錄許可權
匯入標頭檔案 import AddressBook
檢查是否有通訊許可權
ABAuthorizationStatus ABstatus = ABAddressBookGetAuthorizationStatus();switch (ABstatus) {
case kABAuthorizationStatusAuthorized:
NSLog(@"Authorized");
break;
case kABAuthorizationStatusDenied:
NSLog(@"Denied'");
break;
case kABAuthorizationStatusNotDetermined:
NSLog(@"not Determined");
break;
case kABAuthorizationStatusRestricted:
NSLog(@"Restricted");
break;
default:
break;
}
獲取通訊錄許可權
ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, NULL);
ABAddressBookRequestAccessWithCompletion(addressBook, ^(bool granted, CFErrorRef error) {
if (granted) {
NSLog(@"Authorized");
CFRelease(addressBook);
}else{
NSLog(@"Denied or Restricted");
}
});
十、日曆、備忘錄許可權
匯入標頭檔案
檢查是否有日曆或者備忘錄許可權
typedef NS_ENUM(NSUInteger, EKEntityType) {
EKEntityTypeEvent,//日曆
EKEntityTypeReminder //備忘
};
EKAuthorizationStatus EKstatus = [EKEventStore authorizationStatusForEntityType:EKEntityTypeEvent];switch (EKstatus) {
case EKAuthorizationStatusAuthorized:
NSLog(@"Authorized");
break;
case EKAuthorizationStatusDenied:
NSLog(@"Denied'");
break;
case EKAuthorizationStatusNotDetermined:
NSLog(@"not Determined");
break;
case EKAuthorizationStatusRestricted:
NSLog(@"Restricted");
break;
default:
break;
}
獲取日曆、備忘錄許可權
EKEventStore *store = [[EKEventStore alloc]init];
[store requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError * _Nullable error) {
if (granted) {
NSLog(@"Authorized");
}else{
NSLog(@"Denied or Restricted");
}
}];
相關文章
- EDP .Net開發框架--許可權框架
- django開發之許可權管理(一)——許可權管理詳解(許可權管理原理以及方案)、不使用許可權框架的原始授權方式詳解Django框架
- 小知識:軟體開發的許可權控制和許可權驗證
- APP許可權相關的東西APP
- 許可權之選單許可權
- linux 檔案許可權 s 許可權和 t 許可權解析Linux
- 【odoo14】【開發側】許可權配置Odoo
- 如何用 Vue 實現前端許可權控制(路由許可權 + 檢視許可權 + 請求許可權)Vue前端路由
- 基於tp3.2.3開發的許可權管理系統,路由,微信,cdn,許可權路由
- 語音交友app開發許可權系統,不容錯過的設計方案APP
- 許可權系統:一文搞懂功能許可權、資料許可權
- 上傳APP到Google Play許可權問題APPGo
- Linux特殊許可權之suid、sgid、sbit許可權LinuxUI
- .NET 平臺 WPF 通用許可權開發框架 (ABP)框架
- 遇到問題,需要開發角色許可權模組
- Linux許可權Linux
- 許可權控制
- mysql許可權MySql
- 選單許可權和按鈕許可權設定
- Linux的檔案存取許可權和0644許可權Linux
- android動態許可權到自定義許可權框架Android框架
- 【自然框架】許可權的視訊演示(二):許可權到欄位、許可權到記錄框架
- Android6.0------許可權申請管理(單個許可權和多個許可權申請)Android
- 給安卓 app 新增許可權的一種方法安卓APP
- 創業板交易許可權如何開通 網上可以開通創業板許可權嗎創業
- 系統開發中許可權控制的重要性
- thinkphp 5.0.10開發auth後臺許可權管理系統PHP
- Android開發-更”聰明”的申請許可權方式Android
- 許可權系統:許可權應用服務設計
- [BUG反饋]許可權條目中缺少兩個公開方法的許可權設定
- 基於本人開發的許可權系統開發的企業站
- Android 通知許可權Android
- Android SELinux許可權AndroidLinux
- 4、許可權管理
- sql許可權管理SQL
- RBAC許可權管理
- 許可權管控
- MySQL許可權管理MySql
- Linux 特殊許可權Linux