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

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

作者: Peter杰 | 来源:发表于2020-05-01 19:36 被阅读0次

最近项目中用到系统隐私权限的管理,所以对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;
}

  • 授权
    注意: CLLocationManager 需要写成全局对象,否次会授权框会消失
- (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];
    }

}

待更新...

相关文章

  • iOS系统权限管理

    iOS系统权限管理主要有以下权限:网络、相册、推送、相机以及麦克风、通讯录、定位;https://www.jian...

  • iOS 手机权限设置

    手机权限设置 在app中需要访问手机相册,相机,定位,通讯录等隐私数据的时候,如果不做权限设置app会crash。...

  • iPhone系统权限

    需要引用到的头文件有 相册权限 照相机权限 麦克风权限 通知权限 定位权限 通讯录权限

  • APP基础功能的配置管理之系统权限控制

    开篇:在iOS系统上,对于系统权限、用户权限的审核是非常严格的。比如相机、相册、麦克风、通讯录、定位和蓝牙等,在a...

  • ios开发中的权限

    权限分类 联网权限 相册权限 相机 麦克风权限 定位权限 推送权限 通讯录权限 日历 备忘录权限 联网权限 引入头...

  • iOS 相关权限

    权限分类 联网权限 相册权限 相机、麦克风权限 定位权限 推送权限 通讯录权限 日历、备忘录权限 1.联网权限 引...

  • 20170313 iOS 权限相关 : 判断与 跳转 设置

    iOS 常见错误(持续更新) iOS权限获取 摄像头与麦克风 iOS各种权限判断(相机,相册,定位,录音) ios...

  • iOS 需要添加的权限配置

    iOS 10以后,苹果增强了对用户隐私的保护,访问照相机、相册、麦克风、定位、通讯录等,都需要在info.plis...

  • iOS 权限设置

    前言 iOS 10以后,苹果增强了对用户隐私的保护,访问照相机、相册、麦克风、定位、通讯录等,都需要在info.p...

  • App Store构建版本iTunes不显示

    · 这种情况出现是因为iOS 10以后,苹果增强了对用户隐私的保护,访问照相机、相册、麦克风、定位、通讯录等,都需...

网友评论

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

      本文链接:https://www.haomeiwen.com/subject/xzvpdhtx.html