#####1.相册权限git
ALAuthorizationStatus authStatus = [ALAssetsLibrary authorizationStatus];
根据apple的sdk ALAuthorizationStatus 拥有如下状态github
typedef NS_ENUM(NSInteger, ALAuthorizationStatus) { ALAuthorizationStatusNotDetermined NS_ENUM_DEPRECATED_IOS(6_0, 9_0) = 0, // 用户尚未选择权限 ALAuthorizationStatusRestricted NS_ENUM_DEPRECATED_IOS(6_0, 9_0), // 应用未被受权 用户不能更改该应用程序的状态,可能因为活跃的限制/ /如家长控制到位。 ALAuthorizationStatusDenied NS_ENUM_DEPRECATED_IOS(6_0, 9_0), //用户拒绝应用访问 ALAuthorizationStatusAuthorized NS_ENUM_DEPRECATED_IOS(6_0, 9_0) // 用户容许应用访问 }
ALAuthorizationStatus authStatus = [ALAssetsLibrary authorizationStatus]; if (authStatus == ALAuthorizationStatusDenied || authStatus == ALAuthorizationStatusRestricted) { if (IS_IOS8_OR_HIGHER && [[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]]) { failedHandle(true); return; } else { failedHandle(false); return; } } succeedHandler();
iOS8 之后能够经过app
PHAuthorizationStatus status = [PHPhotoLibrary authorizationStatus];
PHAuthorizationStatus的权限值同上类似,这边就不写了,可查看具体的apple 的sdkasync
#####2.相机权限ide
AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
对应的权限枚举,解析如相册权限。spa
typedef NS_ENUM(NSInteger, AVAuthorizationStatus) { AVAuthorizationStatusNotDetermined = 0, AVAuthorizationStatusRestricted, AVAuthorizationStatusDenied, AVAuthorizationStatusAuthorized }
if (authStatus == AVAuthorizationStatusDenied || authStatus == AVAuthorizationStatusRestricted) { if (IS_IOS8_OR_HIGHER && [[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]]) { failedHandle (true); } else { failedHandle (false); } } else if(authStatus == AVAuthorizationStatusAuthorized) { succeedHandler(); } else if (authStatus == ALAuthorizationStatusNotDetermined){ [AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo completionHandler:^(BOOL granted) { dispatch_async(dispatch_get_main_queue(), ^{ if(granted){ succeedHandler(); } else { if (IS_IOS8_OR_HIGHER && [[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]]) { failedHandle (true); } else { failedHandle (false); } } }); }]; }
#####3.麦克风权限 AVAudioSession 这是个很重要的类 有关音频视频等都会用到它,并且也不是很容易理清楚的,这边就不详说这个类的用处了。code
[[AVAudioSession sharedInstance] requestRecordPermission:^(BOOL granted) { dispatch_async(dispatch_get_main_queue(), ^{ if (granted) { succeedHandler(); } else { if (IS_IOS8_OR_HIGHER && [[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]]) { failedHandle(true); } else { failedHandle(false); } } }); }];
typedef NS_OPTIONS(NSUInteger, AVAudioSessionRecordPermission) { AVAudioSessionRecordPermissionUndetermined = 'undt’,// 未获取权限 AVAudioSessionRecordPermissionDenied = 'deny’,//拒绝受权 AVAudioSessionRecordPermissionGranted = ‘grant’//赞成受权 }
#####4.定位权限 iOS8之后定位的获取方法有些变更,但也是简单的 [CLLocationManager locationServicesEnabled] 是否启用定位服务 [CLLocationManager authorizationStatus] 获取当前定位的权限值视频
typedef NS_ENUM(int, CLAuthorizationStatus) { // 用户还没有选择关于这个应用程序 kCLAuthorizationStatusNotDetermined = 0, //这个应用程序未被受权使用定位服务 kCLAuthorizationStatusRestricted, // 用户已经明确拒绝受权对于这个应用程序,或在设置里拒绝该应用使用 kCLAuthorizationStatusDenied, //用户已得到受权使用他们的位置在任什么时候候 kCLAuthorizationStatusAuthorizedAlways NS_ENUM_AVAILABLE(NA, 8_0), //在使用应用程序的时候受权使用 在前台 kCLAuthorizationStatusAuthorizedWhenInUse NS_ENUM_AVAILABLE(NA, 8_0), // 这个值是弃用,但至关于 kCLAuthorizationStatusAuthorizedAlways。 kCLAuthorizationStatusAuthorized NS_ENUM_DEPRECATED(10_6, NA, 2_0, 8_0) };
if (IS_IOS8_OR_HIGHER) { if ([CLLocationManager locationServicesEnabled] && [CLLocationManager authorizationStatus] != kCLAuthorizationStatusDenied && [CLLocationManager authorizationStatus] != kCLAuthorizationStatusRestricted) { succeedHandler(); } else { if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]]) { failedHandle(true); } else { failedHandle(false); } } } else { if ([CLLocationManager locationServicesEnabled]&& ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusAuthorized || [CLLocationManager authorizationStatus] == kCLAuthorizationStatusNotDetermined)) { succeedHandler(); } else { failedHandle(false); } }
#####5.通信录权限 通信录的权限获取 iOS8又是个分界点,这里就直接上获取权限的代码了。开发
if (IS_IOS8_OR_HIGHER) { if (&ABAddressBookRequestAccessWithCompletion != NULL) { ABAddressBookRequestAccessWithCompletion(self.addressBook, ^(bool granted, CFErrorRef error) { if (granted) { succeedHandler(); } else { if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]]) { failedHandle(true); } else { failedHandle(false); } } }); } } else { if (&ABAddressBookRequestAccessWithCompletion != NULL) { ABAddressBookRequestAccessWithCompletion(self.addressBook, ^(bool granted, CFErrorRef error) { if (granted) { succeedHandler(); } else { failedHandle(false); } }); } }
5.通知权限 [[UIApplication sharedApplication] isRegisteredForRemoteNotifications]// 系统设置总开关 [[UIApplication sharedApplication] currentUserNotificationSettings].types//通知下的各个权限子开关get
typedef NS_OPTIONS(NSUInteger, UIUserNotificationType) { UIUserNotificationTypeNone = 0, // the application may not present any UI upon a notification being received UIUserNotificationTypeBadge = 1 << 0, // the application may badge its icon upon a notification being received UIUserNotificationTypeSound = 1 << 1, // the application may play a sound upon a notification being received UIUserNotificationTypeAlert = 1 << 2, // the application may display an alert upon a notification being received }
BOOL pushEnabled; // 设置里的通知总开关是否打开 BOOL settingEnabled = [[UIApplication sharedApplication] isRegisteredForRemoteNotifications]; // 设置里的通知各子项是否都打开 BOOL subsettingEnabled = [[UIApplication sharedApplication] currentUserNotificationSettings].types != UIUserNotificationTypeNone; pushEnabled = settingEnabled && subsettingEnabled; if (pushEnabled) { succeedHandler(); } else { if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]]) { failedHandle(true); } else { failedHandle(false); } }
总结:经过各个功能权限的获取,发现apple对用户隐私愈来愈重视。方法也更完善,iOS8 是一个重要的临界点,开发能够考虑从iOS8开始支持,如
[[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];
这句话的做用是判断可否打开设置里该应用的隐私设置,应用直接跳转到设置里 会使用户的体验更好。 [在github上有我对这些的封装使用] https://github.com/weskhen/PaoBa/tree/master/SystemPermission