iOS 系統設置隱私中經常使用的功能權限獲取

#####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);
                    }
                }
            });
        }];
  • (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//通知下的各個權限子開關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

相關文章
相關標籤/搜索