iOS隐私权限设置(相机,相册,麦克风,定位,通讯录)

2020-05-01  本文已影响0人  Peter杰

最近项目中用到系统隐私权限的管理,所以对iOS常用隐私权限做一下总结,方便以后查阅

iOS常用隐私权限

<key>NSBluetoothPeripheralUsageDescription</key>
<string>需要获取蓝牙权限</string>
<key>NSCalendarsUsageDescription</key>
<string>日历</string>
<key>NSCameraUsageDescription</key>
<string>需要获取您的摄像头信息</string>
<key>NSContactsUsageDescription</key>
<string>需要获取您的通讯录权限</string>
<key>NSHealthShareUsageDescription</key>
<string>健康分享权限</string>
<key>NSHealthUpdateUsageDescription</key>
<string>健康数据更新权限</string>
<key>NSHomeKitUsageDescription</key>
<string>HomeKit权限</string>
<key>NSLocationAlwaysUsageDescription</key>
<string>一直定位权限</string>
<key>NSLocationUsageDescription</key>
<string>定位权限</string>
<key>NSLocationWhenInUseUsageDescription</key>
<string>使用app期间定位权限</string>
<key>NSMicrophoneUsageDescription</key>
<string>需要获取您的麦克风权限</string>
<key>NSPhotoLibraryUsageDescription</key>
<string>需要获取您的相册信息</string>
<key>NSRemindersUsageDescription</key>
<string>提醒事项</string>
<key>NSSiriUsageDescription</key>
<string>需要获取您的Siri权限</string>
<key>NSSpeechRecognitionUsageDescription</key>
<string>语音识别权限</string>
<key>NSVideoSubscriberAccountUsageDescription</key>
<string>AppleTV权限</string>
<key>NSAppleMusicUsageDescription</key>
<string>Add tracks to your music library.</string>
<key>NSMotionUsageDescription</key>
<string>运动与健身权限</string>

跳转到设置界面

- (void)openSettings{
    NSURL *url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
    if ([[UIApplication sharedApplication] canOpenURL:url]) {
        [[UIApplication sharedApplication] openURL: url options:@{} completionHandler:^(BOOL success) {
            dispatch_async(dispatch_get_main_queue(), ^{
                [self loadData];
            });
            
        }];
    }
}

相机权限

//Privacy - Camera
+ (BOOL)cameraPrivacy{
    
    AVAuthorizationStatus status = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
    if (status == AVAuthorizationStatusRestricted || status == AVAuthorizationStatusDenied||status ==AVAuthorizationStatusNotDetermined)
    {
         // 无权限
         // do something...
        return NO;
    }
    return YES;
}
- (void)requestCameraPrivacy{
    
    AVAuthorizationStatus status = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
    if (status == AVAuthorizationStatusNotDetermined){
        [AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo completionHandler:^(BOOL granted) {
            if (granted) {
                dispatch_async(dispatch_get_main_queue(), ^{
                    [self loadData];
                });
            }
        }];
    }else{
        [self openSettings];
    }
}

相册权限

+ (BOOL)photoLibraryPrivacy{
   PHAuthorizationStatus status = [PHPhotoLibrary authorizationStatus];
   if (status == PHAuthorizationStatusRestricted || status == PHAuthorizationStatusDenied||status ==PHAuthorizationStatusNotDetermined)
   {
       // 无权限
       // do something...
        return NO;
    }
    return YES;
}
- (void)requestPhotoLibraryPrivacy{
   
    PHAuthorizationStatus status = [PHPhotoLibrary authorizationStatus];
   if (status ==PHAuthorizationStatusNotDetermined)
   {
       [PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) {
           dispatch_async(dispatch_get_main_queue(), ^{
               [self loadData];
           });
           
       }];
   }else{
       [self openSettings];
   }
}

麦克风权限

+ (BOOL)microPhoneAuthPrivacy{
    AVAuthorizationStatus status = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeAudio];
    if (status == AVAuthorizationStatusRestricted || status == AVAuthorizationStatusDenied||status ==AVAuthorizationStatusNotDetermined)
    {
         // 无权限
         // do something...
        return NO;
    }
    return YES;
}
//麦克风
-(void) requestMicroPhoneAuth
{
    
    AVAuthorizationStatus status = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeAudio];
    if (status == AVAuthorizationStatusNotDetermined){
        [AVCaptureDevice requestAccessForMediaType:AVMediaTypeAudio completionHandler:^(BOOL granted) {
            if (granted) {
                dispatch_async(dispatch_get_main_queue(), ^{
                    [self loadData];
                });
            }
        }];
    }else{
        [self openSettings];
    }
    
}

定位权限

+ (BOOL)locationPrivacy{
    if ([CLLocationManager locationServicesEnabled]) {
        CLAuthorizationStatus status = CLLocationManager.authorizationStatus;
        if (status == kCLAuthorizationStatusAuthorizedAlways || status == kCLAuthorizationStatusAuthorizedWhenInUse||
            status == kCLAuthorizationStatusAuthorizedAlways) {
            return YES;
        }
        return NO;
    }
    return NO;
}

- (void)requestLocationPrivacy{
   self.locationManager = [CLLocationManager new];
    if ([CLLocationManager locationServicesEnabled]) {
        CLAuthorizationStatus status = CLLocationManager.authorizationStatus;
        if (status == kCLAuthorizationStatusNotDetermined) {
            [self.locationManager requestWhenInUseAuthorization];
        }else{
            [self openSettings];
        }
        
    }
}

通讯录权限

+(BOOL)contactsPrivacy{
    CNAuthorizationStatus status = [CNContactStore authorizationStatusForEntityType:CNEntityTypeContacts];
    return status == CNAuthorizationStatusAuthorized;
    
}
- (void)requestAuthorizationForAddressBook {

    CNAuthorizationStatus authorizationStatus = [CNContactStore authorizationStatusForEntityType:CNEntityTypeContacts];
    
    if(authorizationStatus == CNAuthorizationStatusNotDetermined) {
        
        CNContactStore *contactStore = [[CNContactStore alloc] init];
        
        [contactStore requestAccessForEntityType:CNEntityTypeContacts completionHandler:^(BOOL granted, NSError * _Nullable error) {
            
            if(granted) {
                dispatch_async(dispatch_get_main_queue(), ^{
                    [self loadData];
                });
                
            } else{
                NSLog(@"授权失败, error=%@", error);
            }
        }];
        
    }else{
        [self openSettings];
    }

}

待更新...

上一篇下一篇

猜你喜欢

热点阅读