ios 判断用户是否开启权限---并跳转设置

ios 判断用户是否开启权限---并跳转“系统设置”

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

2.检测是否开启定位

后面将持续更新

只有在应用请求过位置权限 或者 通知权限的时候,才会跳进自己app里面的设置呢。不然直接跳到系统设置界面

//打开app定位设置

NSURL *settingsURL = [NSURL URLWithString:UIApplicationOpenSettingsURLString];

if([[UIApplication sharedApplication] canOpenURL:settingsURL]) {

[[UIApplication sharedApplication] openURL:settingsURL];

}

工具/原料

 
  • Mac / Mac mini
  • Xcode

方法/步骤

 
  1.  

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

    //在info.plist 里面设置

    NSCameraUsageDescription

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

    NSPhotoLibraryUsageDescription

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

    科普:

    //=================相册=======================

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

    #import <Photos/PHPhotoLibrary.h>  //相册

    ===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 <AVFoundation/AVCaptureDevice.h>#import <AVFoundation/AVMediaFormat.h>

    //=======AVAuthorizationStatus====

    ==判断是否开启相机权限 的4中状态

    typedef NS_ENUM(NSInteger, AVAuthorizationStatus) {   

      

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

        AVAuthorizationStatusNotDetermined = 0,  

      

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

       AVAuthorizationStatusRestricted,  

       

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

      AVAuthorizationStatusDenied, 

        

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

      AVAuthorizationStatusAuthorized 

    } NS_AVAILABLE_IOS(7_0) __TVOS_PROHIBITED;

    =================使用====================

    //选择从相册获取图片

    //判断状态  如果已经授权 则从相册选取相片  

                     如果没有授权  则跳转到授权设置界面

    //选择从相机获取图片

    //判断状态  如果已经授权 则打开摄像头  

                     如果没有授权  则跳转到授权设置界面

    //引入下面的框架

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

    #import <Photos/PHPhotoLibrary.h>  //相册

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

    #import <AVFoundation/AVCaptureDevice.h>#import <AVFoundation/AVMediaFormat.h>

    【注意】  控制器要遵循的协议

    相册     <UIImagePickerControllerDelegate>

               <UINavigationControllerDelegate>

    //自定义的枚举

    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.  

    //2.=======检测是否开启定位======

    //在info.plist 里面配置

    NSLocationWhenInUseUsageDescription

    Privacy - Location When In Use Usage Description    App需要使用定位功能

    NSLocationAlwaysUsageDescription

    Privacy - Location Always Usage Description     App需要使用定位功能

    引入框架 

    #import <CoreLocation/CoreLocation.h>  //定位

    遵循协议 <CLLocationManagerDelegate>

    //=========CLAuthorizationStatus=========

    typedef NS_ENUM(int, CLAuthorizationStatus) {

    //定位服务授权状态是用户没有决定是否使用定位服务

    kCLAuthorizationStatusNotDetermined = 0,

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

    kCLAuthorizationStatusRestricted,

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

    kCLAuthorizationStatusDenied,

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

    kCLAuthorizationStatusAuthorizedAlways NS_ENUM_AVAILABLE(10_12, 8_0),

    //定位服务授权状态仅被允许在使用应用程序的时候

    kCLAuthorizationStatusAuthorizedWhenInUse NS_ENUM_AVAILABLE(NA, 8_0),

      //已被废弃

    kCLAuthorizationStatusAuthorized NS_ENUM_DEPRECATED(10_6, NA, 2_0, 8_0, "Use kCLAuthorizationStatusAuthorizedAlways") __TVOS_PROHIBITED __WATCHOS_PROHIBITED = kCLAuthorizationStatusAuthorizedAlways

    };

    【注意】

    //1.

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

    //[CLLocationManager locationServicesEnabled]

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

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

    //2.

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

    NSURL *settingsURL = [NSURL URLWithString:UIApplicationOpenSettingsURLString];

    [[UIApplication sharedApplication] openURL:settingsURL];

    //================使用=================

    引入框架

    #import <CoreLocation/CoreLocation.h>  //定位

    遵循协议 <CLLocationManagerDelegate>

    //当前状态

    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<CLLocation *> *)locations {

    //

    [locationManager stopUpdatingLocation];

    CLLocation *currentLocation = [locations lastObject];

    CLGeocoder * geoCoder = [[CLGeocoder alloc] init];

    //反编码

    [geoCoder reverseGeocodeLocation:currentLocation completionHandler:^(NSArray<CLPlacemark *> * _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 <UserNotifications/UserNotifications.h>

    //====方法一

    + (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. 4

    //4.

    NSContactsUsageDescription -> 通讯录

    NSMicrophoneUsageDescription -> 麦克风

ios 判断用户是否开启权限---并跳转设置的更多相关文章

  1. iOS开发 判断用户是否开启了热点

    - (BOOL)achiveUserHotspotOpening { return [UIApplication sharedApplication].statusBarFrame.size.heig ...

  2. iOS开发 判断用户是否开启了定位

    - (BOOL)achiveUserLocationStart { CLAuthorizationStatus status = [CLLocationManager authorizationSta ...

  3. php如何判断用户是从指定页面跳转进来的

    $_SERVER['HTTP_REFERER']下'HTTP_REFERER' 引导用户代理到当前页的前一页的地址(如果存在).由 user agent 设置决定.并不是所有的用户代理都会设置该项,有 ...

  4. IOS判断用户的网络类型(2/3/4G、wifi)

    直接贴代码吧,ios7之后是获取的较为准确,7以下我拿iphone5测试的是无法区分3g/2g.连iphone4都能升到7.1.4,而且目前主流的设备7以下的系统已经很少了,这个方案尽管不太完美,但影 ...

  5. iOS 判断是否有权限访问相机,相册

    1.判断用户是否有权限访问相册 #import <AssetsLibrary/AssetsLibrary.h> ALAuthorizationStatus author =[ALAsset ...

  6. 使用uView UI+UniApp开发微信小程序--判断用户是否登录并跳转

    在<使用uView UI+UniApp开发微信小程序>的随笔中,介绍了基于uView UI+UniApp开发微信小程序的一些基础知识和准备工作,其中也大概介绍了一下基本的登录过程,本篇随笔 ...

  7. iOS之访问权限以及跳转到系统界面

    iOS开发中有时候有这样的需求:当用户设置不允许访问照片.麦克风和相机等系统权限的时候,这时需要直接跳转到系统的隐私界面进行设置. 判断是否开启权限 前面已经说过,我们需要在用户不允许访问的时候跳转, ...

  8. iOS判断一些权限是否被禁止

    iOS中经常会遇到访问相册.相机.麦克疯.蓝牙.以及推送等权限,所以每次我们要使用这些权限是都要记得查看用户是否允许了,如果用户禁止了你的访问权限,你仍然去调取相册或者相机等,那么就会先出现下面的这个 ...

  9. iOS 判断相机权限是否被限制,判断相机是否可以使用

    判断相机权限是否被限制 需要导入   AVFoundation 类 [objc] view plain copy #import <AVFoundation/AVFoundation.h> ...

随机推荐

  1. BlackLowKey主题CSS

    /* Minification failed. Returning unminified contents. (151,61): run-time error CSS1062: Expected se ...

  2. Google android开发者 中国官方文档开放了呀

    Google官方开发文档地址 包括 android , android TV

  3. js之可迭代对象

    遍历Array可以采用下标循环,遍历Map和Set就无法使用下标.为了统一集合类型,ES6标准引入了新的iterable类型,Array.Map和Set都属于iterable类型. 具有iterabl ...

  4. Python中and和or的运算法则

    1. 在纯and语句中,如果每一个表达式都不是假的话,那么返回最后一个,因为需要一直匹配直到最后一个.如果有一个是假,那么返回假2. 在纯or语句中,只要有一个表达式不是假的话,那么就返回这个表达式的 ...

  5. 【数据库】4.0 MySQL入门学习(四)——linux系统环境下MySQL安装

    1.0 我的操作系统是CentOS Linux release 7.6.1810  (Core) 系统详细信息如下: Linux version 3.10.0-957.1.3.el7.x86_64 ( ...

  6. ie浏览器 vuejs axios Promise 未定义

    随着前端技术的发现,es6语法在被更大范围的使用,而很多的浏览器并不支持ES6,比如IE…… 这里我们介绍几个解决方法. 一.使浏览器兼容ES6基本语法 1.在引入其他脚本前先引入browser.mi ...

  7. vuejs的双向数据绑定实现原理——object.defineproperty()

    视图和数据变化绑定 而vue.js主要利用了accessor descriptors的set和get来更新视图,这里看到的这个例子挺好,是一个简单的绑定.对于一个html页面 <div> ...

  8. Android 自定义View实现SegmentControlView(自定义多样式tablayout)

    偷懒一下,不做过多阐述 参考资源: Android 自定义View实现SegmentControlView : https://blog.csdn.net/a512337862/article/det ...

  9. git 无法忽略Android Studio 生成的 .idea目录解决办法

    在Android Studio中导入了别的人Gradle项目,产生了 .idea文件夹, 然后git 发现了这个变动,修改了 .gitignore不起作用,仍然不能忽略这个文件夹 在项目目录里面 右键 ...

  10. jdk是什么?jdk1.8安装配置方法

    jdk是什么呢?jdk的是java development kit的缩写,意思是java程序开发的工具包.也可以说jdk是java的sdk. 目前的JDK大致分三个大版本:Java SE:Java P ...