1.相册权限
ALAuthorizationStatus authStatus = [ALAssetsLibrary authorizationStatus];
根据apple的sdk ALAuthorizationStatus 拥有以下状态
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 以后可以通过
PHAuthorizationStatus status = [PHPhotoLibrary authorizationStatus];
PHAuthorizationStatus的权限值同上相似,这边就不写了,可查看具体的apple 的sdk
2.相机权限
AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
对应的权限枚举,解析如相册权限。
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 这是个很重要的类 有关音频视频等都会用到它,而且也不是很容易理清楚的,这边就不详说这个类的用处了。
[[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);
}
}
});
}];
- (AVAudioSessionRecordPermission)recordPermission NS_AVAILABLE_IOS(8_0)
这是iOS8 以后开始支持的方法,使用也非常方便简单。
下面说下这个属性的含义:获取当前应用录音权限
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//通知下的各个权限子开关
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
网友评论