iOS Kingdom — 权限判定

iOS Kingdom — 权限判定_第1张图片
Bunny
在 iOS 中,权限申请提示框只会在用户安装软件后第一次申请时才会提示出来,如果用户到"设置->隐私->权限"处关闭了权限,此时,当你 APP 须要某权限时,系统将不会再次提示。当我们从用户角度出发去看待这个 APP 时,这样的 APP 很不人性化。为了提高软件的人性化,我们可以这样做,判断用户之前是否申请过此类权限,如果没有申请,则不让用户进行下一步操作。
以相机授权状态为例,以下各授权状态类似
// 授权状态
 AVAuthorizationStatusNotDetermined: 第一次请求权限
 AVAuthorizationStatusRestricted: 受家长模式控制
 AVAuthorizationStatusDenied: 拒绝权限授权
 AVAuthorizationStatusAuthorized: 已授权

1、相机

#import 
AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
    switch (authStatus) {
        case AVAuthorizationStatusNotDetermined:
        case AVAuthorizationStatusRestricted:
        case AVAuthorizationStatusDenied: 
        case AVAuthorizationStatusAuthorized: 
    }

2、定位

#import 
CLAuthorizationStatus authStatus = [CLLocationManager authorizationStatus];
    switch (authStatus) {
        case kCLAuthorizationStatusNotDetermined: 
        case kCLAuthorizationStatusRestricted:
        case kCLAuthorizationStatusDenied: 
        case kCLAuthorizationStatusAuthorizedAlways:
        case kCLAuthorizationStatusAuthorizedWhenInUse:
    }

3、日历

EKAuthorizationStatus authStatus = [EKEventStore authorizationStatusForEntityType:EKEntityTypeEvent];
    switch (authStatus) {
        case EKAuthorizationStatusNotDetermined:
        case EKAuthorizationStatusRestricted:
        case EKAuthorizationStatusDenied:
        case EKAuthorizationStatusAuthorized:
    }

4、提醒事项

EKAuthorizationStatus authStatus = [EKEventStore authorizationStatusForEntityType:EKEntityTypeReminder];
    switch (authStatus) {
        case EKAuthorizationStatusNotDetermined: 
        case EKAuthorizationStatusRestricted:
        case EKAuthorizationStatusDenied: 
        case EKAuthorizationStatusAuthorized:
    }

5、相册

if ([[[UIDevice currentDevice] systemVersion] floatValue] < 8.0) {
        ALAuthorizationStatus authStatus =[ALAssetsLibrary authorizationStatus];
        switch (authStatus) {
            case ALAuthorizationStatusNotDetermined: 
            case ALAuthorizationStatusRestricted:
            case ALAuthorizationStatusDenied: 
            case ALAuthorizationStatusAuthorized: 
        }
    } else {
        PHAuthorizationStatus authStatus = [PHPhotoLibrary authorizationStatus];
        switch (authStatus) {
            case PHAuthorizationStatusNotDetermined:
            case PHAuthorizationStatusRestricted:
            case PHAuthorizationStatusDenied: 
            case PHAuthorizationStatusAuthorized: 
        }
    }

6、通知

UIUserNotificationType type = [[UIApplication sharedApplication] currentUserNotificationSettings].types;
    switch (type) {
        case UIUserNotificationTypeNone: {
                 // 没有授权
        }
        case UIUserNotificationTypeBadge:
        case UIUserNotificationTypeSound:
        case UIUserNotificationTypeAlert: 
    }

7、麦克风

AVAudioSessionRecordPermission authStatus = [[AVAudioSession sharedInstance] recordPermission];
    switch (authStatus) {
        case AVAudioSessionRecordPermissionUndetermined: {
               // 第一次授权
        }
        case AVAudioSessionRecordPermissionDenied: 
        case AVAudioSessionRecordPermissionGranted: {
               // 授权成功
        }
    }

8、健康(Health)

HKHealthStore *healthStore = [[HKHealthStore alloc] init];
    HKObjectType *hkObjectType = [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierHeight];
    HKAuthorizationStatus authStatus = [healthStore authorizationStatusForType:hkObjectType];
    switch (authStatus) {
        case HKAuthorizationStatusNotDetermined: 
        case HKAuthorizationStatusSharingDenied: 
        case HKAuthorizationStatusSharingAuthorized:
    }

9、通讯录

if ([[[UIDevice currentDevice] systemVersion] floatValue] > 9.0) {
        CNAuthorizationStatus authStatus = [CNContactStore authorizationStatusForEntityType:CNEntityTypeContacts];
        switch (authStatus) {
            case CNAuthorizationStatusNotDetermined: 
            case CNAuthorizationStatusRestricted:
            case CNAuthorizationStatusDenied: 
            case CNAuthorizationStatusAuthorized: 
        }
    }else {
        ABAuthorizationStatus authStatus = ABAddressBookGetAuthorizationStatus();
        switch (authStatus) {
            case kABAuthorizationStatusNotDetermined: 
            case kABAuthorizationStatusRestricted:
            case kABAuthorizationStatusDenied: 
            case kABAuthorizationStatusAuthorized:
        }
    }

10、网络

CTCellularData *cellularData = [[CTCellularData alloc] init];
        CTCellularDataRestrictedState authStatus = cellularData.restrictedState;
        switch (authStatus) {
            case kCTCellularDataRestrictedStateUnknown: 
            // 第一次请求
            case kCTCellularDataRestricted: 
            // 权限被禁止
            case kCTCellularDataNotRestricted:
            // 权限已授权
        }
GitHub 示例: https://github.com/AppleDP/WQPermissionRequest
权限手动请求: http://www.jianshu.com/p/4ffdd86bd93c

你可能感兴趣的:(iOS Kingdom — 权限判定)