ios 判断打开相机权限_ios 判断用户是否开启权限---并跳转设

这篇博客详细介绍了在iOS应用中如何判断并处理相机、相册、定位和通知等权限的开启状态。提供了相应的代码示例,当权限未开启时如何引导用户进入设置页面授权。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、相册相机

2、通知

3、通讯录--->

4、定位

1.  判断 访问相册 或 相机 权限是否开启

//在info.plist 里面设置

Privacy - Camera Usage Description      App需要您的同意,才能访问相

Privacy - Photo Library Usage Description   App需要您的同意,才能访问相册

科普:

相册

//相册权限判断 需要引入框架#import   //相册

PHAuthorizationStatus相册权限状态判断

在8.0系统以后,新加入了Photos.framework框架,我们可以利用框架中的PHAuthorizationStatus进行相册权限状态判断。

判断是否开启相册权限 的4中状态typedef NS_ENUM(NSInteger,PHAuthorizationStatus) {

//1. 用户还没有关于这个应用程序做出了选择

PHAuthorizationStatusNotDetermined = 0,

//2. 这个应用程序未被授权访问图片数据。用户不能更改该应用程序的状态,可能是由于活动的限制,如家长控制到位。

PHAuthorizationStatusRestricted,

//3. 用户已经明确否认了这个应用程序访问图片数据

PHAuthorizationStatusDenied,       //4. 用户授权此应用程序访问图片数据

PHAuthorizationStatusAuthorized

}PHOTOS_AVAILABLE_IOS_TVOS(8_0, 10_0);

相机

//相册权限判断 需要引入框架#import #import 

判断是否开启相机权限 的4中状态typedef NS_ENUM(NSInteger, AVAuthorizationStatus) {

//1. 表明用户尚未选择关于客户端是否可以访问硬件

AVAuthorizationStatusNotDetermined = 0,

//2. 客户端未被授权访问硬件的媒体类型。用户不能改变客户机的状态,可能由于活跃的限制,如家长控制

AVAuthorizationStatusRestricted,

//3. 明确拒绝用户访问硬件支持的媒体类型的客户

AVAuthorizationStatusDenied,

//4. 客户端授权访问硬件支持的媒体类型

AVAuthorizationStatusAuthorized } NS_AVAILABLE_IOS(7_0) __TVOS_PROHIBITED;=================使用====================//选择从相册获取图片//判断状态  如果已经授权 则从相册选取相片

如果没有授权  则跳转到授权设置界面//选择从相机获取图片//判断状态  如果已经授权 则打开摄像头

如果没有授权  则跳转到授权设置界面//引入下面的框架//相册权限判断 需要引入框架#import   //相册//相册权限判断 需要引入框架#import #import //【注意】  控制器要遵循的协议相册  

//自定义的枚举typedef NS_ENUM(NSInteger, ChosePhontType) {

ChosePhontTypeAlbum,  //相册

ChosePhontTypeCamera   //相机};//下面部分可以直接粘贴复制使用

-(void)clickHeaderImageView{   //点击头像

UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"选择相片" message:nil preferredStyle:UIAlertControllerStyleActionSheet];            UIAlertAction *album = [UIAlertAction actionWithTitle:@"相册" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {

[self chosePhoto:ChosePhontTypeAlbum]; //从系统相册选择照片

}];            UIAlertAction *camera = [UIAlertAction actionWithTitle:@"相机" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {

[self chosePhoto:ChosePhontTypeCamera]; //相机

}];            UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {

}];

[alert addAction:album];

[alert addAction:camera];

[alert addAction:cancel];

[self presentViewController:alert animated:YES completion:^{

}];

}    //==========访问系统  相册 / 相机  ===============

- (void)chosePhoto:(ChosePhontType)type{        UIImagePickerController *piker = [[UIImagePickerController alloc] init];

piker.delegate = self;

piker.allowsEditing = YES;        if (type == ChosePhontTypeAlbum) {   // 相册

//======判断 访问相册 权限是否开启=======

PHAuthorizationStatus status = [PHPhotoLibrary authorizationStatus];            //有被授权访问的照片数据   用户已经明确否认了这一照片数据的应用程序访问

if (status == PHAuthorizationStatusRestricted ||

status == PHAuthorizationStatusDenied) {                //====没有权限====

UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"去开启访问相册权限?" message:nil preferredStyle:UIAlertControllerStyleAlert];                UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {

}];                UIAlertAction *ok = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {                    //===无权限 引导去开启===

[self openJurisdiction];

}];                // 将UIAlertAction添加到UIAlertController中

[alertController addAction:cancel];

[alertController addAction:ok];                // present显示

[self presentViewController:alertController animated:YES completion:nil];

}else{    //====有访问相册的权限=======

piker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;

}

}else if (type == ChosePhontTypeCamera) {  // 相机

//======判断 访问相机 权限是否开启=======

AVAuthorizationStatus authStatus =  [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];            //===无权限====

if (authStatus == AVAuthorizationStatusRestricted || authStatus ==AVAuthorizationStatusDenied){                //====没有权限====

UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"去开启访问相机权限?" message:nil preferredStyle:UIAlertControllerStyleAlert];                UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {

}];                UIAlertAction *ok = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {                    //===无权限 引导去开启===

[self openJurisdiction];

}];                // 将UIAlertAction添加到UIAlertController中

[alertController addAction:cancel];

[alertController addAction:ok];                // present显示

[self presentViewController:alertController animated:YES completion:nil];

}else{  //===有权限======

if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {   //相机可用

piker.sourceType = UIImagePickerControllerSourceTypeCamera;

}else{  // 相机不可用

[SVProgressHUD showErrorWithStatus:@"相机不可用"];                    return;

}

}

}

[self presentViewController:piker animated:YES completion:^{

}];

}    #pragma mark-------去设置界面开启权限----------

-(void)openJurisdiction{        NSURL *url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];        if ([[UIApplication sharedApplication] canOpenURL:url]) {

[[UIApplication sharedApplication] openURL:url];

}

}    #pragma mark UIImagePickerController回调方法================

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info { //选取的照片

//选取的照片

UIImage *image = info[UIImagePickerControllerEditedImage];

_tableViewHeaderView.headerV.image = image;

[self dismissViewControllerAnimated:YES completion:nil];

}

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker { //取消选择

[self dismissViewControllerAnimated:YES completion:nil];

}

2 、定位

=======检测是否开启定位======

在info.plist 里面配置Privacy - Location When In Use Usage Description    App需要使用定位功能Privacy - Location Always Usage Description     App需要使用定位功能

引入框架#import   //定位遵循协议 typedef NS_ENUM(int, CLAuthorizationStatus) {        //定位服务授权状态是用户没有决定是否使用定位服务

kCLAuthorizationStatusNotDetermined = 0,        //定位服务授权状态是受限制的。可能是由于活动限制定位服务,用户不能改变。这个状态可能不是用户拒绝的定位服务

kCLAuthorizationStatusRestricted,        //定位服务授权状态已经被用户明确禁止,或者在设置里的定位服务中关闭

kCLAuthorizationStatusDenied,       //定位服务授权状态已经被用户允许在任何状态下获取位置信息。包括监测区域、访问区域、或者在有显著的位置变化的时候

kCLAuthorizationStatusAuthorizedAlways NS_ENUM_AVAILABLE(10_12, 8_0),       //定位服务授权状态仅被允许在使用应用程序的时候

kCLAuthorizationStatusAuthorizedWhenInUse NS_ENUM_AVAILABLE(NA, 8_0),

};

【注意】//1.

//判断定位是否开启是判断的整个手机系统的定位是否打开,并不是针对这一应用

[CLLocationManager locationServicesEnabled]    //跳转到  整个手机系统的“定位”设置界面

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"prefs:root=LOCATION_SERVICES"]];    //2.

//跳转至 系统的权限设置界面

NSURL *settingsURL = [NSURL URLWithString:UIApplicationOpenSettingsURLString];

[[UIApplication sharedApplication] openURL:settingsURL];

=================使用=================引入框架

#import   //定位遵循协议 //当前状态CLAuthorizationStatus status = [CLLocationManager authorizationStatus];if ([CLLocationManager locationServicesEnabled] && [CLLocationManager authorizationStatus] != kCLAuthorizationStatusDenied) {

//定位开启   }//全局变量CLLocationManager * locationManager; NSString * currentCity; //当前城市

NSString *prv; //当前省-(void)addLocation{   //开始定位

//判断定位是否开启是判断的整个手机系统的定位是否打开,并不是针对这一应用

//判断定位功能是否打开

if ([CLLocationManager locationServicesEnabled]) {

locationManager = [[CLLocationManager alloc] init];

locationManager.delegate = self;  //遵循协议

//精确定位

locationManager.desiredAccuracy = kCLLocationAccuracyBest;

[locationManager requestWhenInUseAuthorization];  //使用时定位

currentCity = [[NSString alloc] init];

[locationManager startUpdatingLocation];  //开始定位

}

}#pragma mark CoreLocation delegate----- 定位----//定位失败则执行此代理方法//定位失败弹出提示框,点击"打开定位"按钮,会打开系统的设置,提示打开定位服务- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error {    UIAlertController * alertVC = [UIAlertController alertControllerWithTitle:@"允许\"定位\"提示" message:@"请在设置中打开定位" preferredStyle:UIAlertControllerStyleAlert];    UIAlertAction * ok = [UIAlertAction actionWithTitle:@"打开定位" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {        //打开app定位设置

NSURL *settingsURL = [NSURL URLWithString:UIApplicationOpenSettingsURLString];

[[UIApplication sharedApplication] openURL:settingsURL];

}];    UIAlertAction * cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {

}];

[alertVC addAction:cancel];

[alertVC addAction:ok];

[self presentViewController:alertVC animated:YES completion:nil];

}//定位成功- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {    //

[locationManager stopUpdatingLocation];    CLLocation *currentLocation = [locations lastObject];    CLGeocoder * geoCoder = [[CLGeocoder alloc] init];    //反编码

[geoCoder reverseGeocodeLocation:currentLocation completionHandler:^(NSArray * _Nullable placemarks, NSError * _Nullable error) {        if (placemarks.count > 0) {            CLPlacemark *placeMark = placemarks[0];

currentCity = placeMark.locality;            if (!currentCity) {

currentCity = @"无法定位当前城市";

}            NSLog(@"%@",currentCity); //这就是当前的城市

NSLog(@"%@",placeMark.name);//具体地址:  xx市xx区xx街道

//administrativeArea   省

NSLog(@"%@",placeMark.administrativeArea);

}        else if (error == nil && placemarks.count == 0) {            NSLog(@"No location and error return");

}        else if (error) {            NSLog(@"location error: %@ ",error);

}

}];

}

3.、推送//3.=======检测是否允许消息推送======#import //====方法一+ (BOOL)isAllowedNotification {

if ([UIDevice isSystemVersioniOS8]) {  // >= ios8

// system is iOS8

UIUserNotificationSettings *setting = [[UIApplication sharedApplication  ] currentUserNotificationSettings];

if (UIUserNotificationTypeNone != setting.types) {

return YES;

}

} else {//iOS7

UIRemoteNotificationType type = [[UIApplication sharedApplication] enabledRemoteNotificationTypes];            if(UIRemoteNotificationTypeNone != type) {

return YES;

}else

}

return NO;

}

+ (BOOL)isSystemVersioniOS8 {

//check systemVerson of device

UIDevice *device = [UIDevice currentDevice];

float sysVersion = [device.systemVersion floatValue];         if (sysVersion >= 8.0f) {              return YES;

}         return NO;

}//====方法二if ([[UIDevice currentDevice].systemVersion floatValue]>=8.0f) {

UIUserNotificationSettings *setting = [[UIApplication sharedApplication] currentUserNotificationSettings];

if (UIUserNotificationTypeNone == setting.types) {

NSLog(@"推送关闭");

}else{

NSLog(@"推送打开");

}

}else{

UIRemoteNotificationType type = [[UIApplication sharedApplication] enabledRemoteNotificationTypes];

if(UIRemoteNotificationTypeNone == type){

NSLog(@"推送关闭");

}else{

NSLog(@"推送打开");

}

}// 去设置[[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];

//===方法三+ (void)isOpenMessageNotificationServiceWithBlock:(ReturnBlock)returnBlock

{

BOOL isOpen = NO;

#if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_8_0

UIUserNotificationSettings *setting = [[UIApplication sharedApplication] currentUserNotificationSettings];

if (setting.types != UIUserNotificationTypeNone) {

isOpen = YES;

}

#else

UIRemoteNotificationType type = [[UIApplication sharedApplication] enabledRemoteNotificationTypes];

if (type != UIRemoteNotificationTypeNone) {

isOpen = YES;

}

#endif

if (returnBlock) {

returnBlock(isOpen);

}

}//====方法四+ (void)isOpenMessageNotificationServiceWithBlock:(ReturnBlock)returnBlock

{

#if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_10_0

[[UNUserNotificationCenter currentNotificationCenter] getNotificationSettingsWithCompletionHandler:^(UNNotificationSettings *settings) {

if (returnBlock) {

returnBlock(settings.authorizationStatus == UNAuthorizationStatusAuthorized);

}

}];

#elif __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_8_0

returnBlock([[UIApplication sharedApplication] isRegisteredForRemoteNotifications]);

#else

UIRemoteNotificationType type = [[UIApplication sharedApplication] enabledRemoteNotificationTypes];

if (returnBlock) {

returnBlock(type != UIRemoteNotificationTypeNone);

}

#endif  }

4、通讯录

NSContactsUsageDescription

5、麦克风NSMicrophoneUsageDescription -> 麦克风

作者:子疯zp

链接:https://www.jianshu.com/p/3eb54cd92443

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值