iOS 各种权限判断及设置跳转

作者: Albin_iOS | 来源:发表于2016-12-27 16:27 被阅读1126次

    iOS8之后用一下方法跳转到设置界面

    //手机系统版本号
    #define SYSTEMVERSION [[[UIDevice currentDevice]systemVersion]floatValue]
    //跳转到设置界面,让用户开启权限        
    NSURL *url;        
    if(SYSTEMVERSION >= 8.0)      
     {            
    //iOS8 以后           
    url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];            if ([[UIApplication sharedApplication] canOpenURL:url])           
    [[UIApplication sharedApplication] openURL:url];      
     }
    

    补充资料:

    在iOS应用程序中打开设备设置界面及其中某指定的选项界面

    NSURL *url = [NSURL URLWithString:@“prefs:xxx"];
     if ([[UIApplication sharedApplication] canOpenURL:url])
     {     
        [[UIApplication sharedApplication] openURL:url]; 
     }
    

    一些其他可用的参数:
    隐私下常用字段

    @"prefs:root=Privacy&path=CAMERA"
    ,
    //设置相机使用权限
    @"prefs:root=Privacy&path=PHOTOS"
    //设置照片使用权限
    

    打开一级界面可将上面的字符串修改为以下对应的字段:

    @"prefs:root=WIFI"
    ,//打开WiFi
    
    @"prefs:root=Bluetooth"
    ,//打开蓝牙设置页
    
    @"prefs:root=NOTIFICATIONS_ID"
    ,//通知设置
    
    @"prefs:root=General"
    ,//通用
    @"prefs:root=DISPLAY&BRIGHTNESS",//显示与亮度
    @"prefs:root=Wallpaper",//墙纸
    @"prefs:root=Sounds",//声音
    @"prefs:root=Privacy",//隐私
    @"prefs:root=STORE",//存储
    @"prefs:root=NOTES",//备忘录
    @"prefs:root=SAFARI",//Safari
    @"prefs:root=MUSIC",//音乐
    @"prefs:root=Photos",//照片与相机
    @"prefs:root=CASTLE"//iCloud
    @"prefs:root=FACETIME",//FaceTime
    @"prefs:root=LOCATION_SERVICES",//定位服务
    @"prefs:root=Phone",//电话
    @"prefs:root=AIRPLANE_MODE"//飞行模式 
    @"prefs:root=General&path=Network"//网络
    @"prefs:root=VIDEO"//视频
    
     prefs:root=CASTLE&path=STORAGE_AND_BACKUP// 
    prefs:root=General&path=INTERNATIONAL//
    prefs:root=ACCOUNT_SETTINGS// 
    prefs:root=MUSIC&path=EQ// 
    prefs:root=MUSIC&path=VolumeLimit// 
    prefs:root=NIKE_PLUS_IPOD// 
    prefs:root=General&path=ManagedConfigurationList// 
    prefs:root=General&path=Reset// 
    prefs:root=Sounds&path=Ringtone// 
    prefs:root=General&path=Assistant// 
    prefs:root=General&path=SOFTWARE_UPDATE_LINK// 
    prefs:root=TWITTER// prefs:root=General&path=USAGE// 
    prefs:root=General&path=Network/VPN//
    prefs:root=INTERNET_TETHERING//
    

    通用下常用字段

    @"prefs:root=General&path=About",//关于本机 
    @"prefs:root=General&path=SOFTWARE_UPDATE_LINK",//软件更新 
    @"prefs:root=General&path=DATE_AND_TIME",//日期和时间
    @"prefs:root=General&path=ACCESSIBILITY",//辅助功能
    @"prefs:root=General&path=Keyboard",//键盘 
    @"prefs:root=General&path=VPN",//VPN设置 
    @"prefs:root=General&path=AUTOLOCK",//自动锁屏
    @"prefs:root=General&path=INTERNATIONAL",//语言与地区 
    @"prefs:root=General&path=ManagedConfigurationList",//描述文件
    prefs:root=General&path=ACCESSIBILITY//重力感应  
    prefs:root=General&path=USAGE/CELLULAR_USAGE//用量 
    

    .m 方发声明

    #import <Foundation/Foundation.h>
    
    typedef enum : NSUInteger {   
     systemAuthorityCamera,   
     systemAuthorityPhotoLibrary,  
     systemAuthorityNotifacation,   
     systemAuthorityNetwork,  
     systemAuthorityAudio,   
     systemAuthorityLocation,   
     systemAuthorityAddressBook,  
     systemAuthorityCalendar,    
    systemAuthorityReminder,} systemAuthorityType;
    
    @interface SystemAuthority : NSObject
    /**
    相机权限开关
    @return YES/NO
    */
    - (BOOL)CameraAuthority;
    /**
    相册权限开关
    @return YES/NO
    */
    - (BOOL)PhotoLibraryAuthority;
    /**
    推送权限开关
    @return YES/NO
    */
    - (BOOL)notificationAuthority;
    /**
    连网权限开关
    @return YES/NO
    */
    - (BOOL)netWorkAuthority;
    /**
    麦克风权限开关
    @return YES/NO
    */
    - (BOOL)audioAuthority;
    /**
    定位权限开关
    @return YES/NO
    */
    - (BOOL)locationAuthority;
    /**
    通讯录权限开关
    @return YES/NO
    */
    - (BOOL)addressBookAuthority;
    /**
    日历权限开关
    @return YES/NO
    */
    - (BOOL)calendarAuthority;
    /**
    备忘录权限开关
    @return YES/NO
    */
    - (BOOL)reminderAuthority;
    @end
    

    .h 方法实现

    #import "SystemAuthority.h"
    #import <AVFoundation/AVFoundation.h>
    #import <Photos/Photos.h>
    #import <AssetsLibrary/AssetsLibrary.h>
    @import CoreTelephony;
    #import <UserNotifications/UserNotifications.h>
    #import <CoreLocation/CoreLocation.h>
    #import <AddressBook/AddressBook.h>
    #import <Contacts/Contacts.h>
    #import <EventKit/EventKit.h>
    
    //手机系统版本号
    #define SYSTEMVERSION [[[UIDevice currentDevice]systemVersion]floatValue]
    
    
    @implementation SystemAuthority
    
    - (BOOL)CameraAuthority
    {
        AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
        if (authStatus == AVAuthorizationStatusDenied || authStatus == AVAuthorizationStatusRestricted) {
            [self showAlertWithType:systemAuthorityCamera];
            return NO;
        }
        return YES;
    }
    
    
    - (BOOL)PhotoLibraryAuthority
    {
        if (SYSTEMVERSION >= 8.0) {
            PHAuthorizationStatus authStatus = [PHPhotoLibrary authorizationStatus];
            if(authStatus == PHAuthorizationStatusDenied || authStatus == PHAuthorizationStatusRestricted) {
                // 未授权
                [self showAlertWithType:systemAuthorityPhotoLibrary];
                return NO;
            }
        }
        else if (SYSTEMVERSION >= 6.0 && SYSTEMVERSION < 8.0)
        {
            ALAuthorizationStatus authStatus = [ALAssetsLibrary authorizationStatus];
            if(authStatus == ALAuthorizationStatusDenied || authStatus == ALAuthorizationStatusRestricted) {
                // 未授权
                [self showAlertWithType:systemAuthorityPhotoLibrary];
                return NO;
            }
        }
        return YES;
    }
    
    - (BOOL)notificationAuthority
    {
        if (SYSTEMVERSION>=8.0f)
        {
            UIUserNotificationSettings *setting = [[UIApplication sharedApplication] currentUserNotificationSettings];
            if (UIUserNotificationTypeNone == setting.types)
            {
                [self showAlertWithType:systemAuthorityNotifacation];
                return NO;
            }
            
        }
        else
        {
            UIRemoteNotificationType type = [[UIApplication sharedApplication] enabledRemoteNotificationTypes];
            if(UIRemoteNotificationTypeNone == type){
                [self showAlertWithType:systemAuthorityNotifacation];
                return NO;
            }
        }
        return YES;
    }
    
    - (BOOL)netWorkAuthority
    {
        CTCellularData *cellularData = [[CTCellularData alloc]init];
        CTCellularDataRestrictedState state = cellularData.restrictedState;
        if (state == kCTCellularDataRestricted) {
            [self showAlertWithType:systemAuthorityNetwork];
            return NO;
        }
        return YES;
    }
    
    - (BOOL)audioAuthority
    {
        AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeAudio];
        if (authStatus == AVAuthorizationStatusDenied || authStatus == AVAuthorizationStatusRestricted) {
            [self showAlertWithType:systemAuthorityAudio];
            return NO;
        }
        return YES;
    }
    
    - (BOOL)locationAuthority
    {
        BOOL isLocation = [CLLocationManager locationServicesEnabled];
        if (!isLocation) {
            CLAuthorizationStatus CLstatus = [CLLocationManager authorizationStatus];
            if (CLstatus == kCLAuthorizationStatusDenied || CLstatus == kCLAuthorizationStatusDenied) {
                [self showAlertWithType:systemAuthorityLocation];
                return NO;
            }
        }
        return YES;
    }
    
    - (BOOL)addressBookAuthority
    {
        if (SYSTEMVERSION >= 9.0) {
            CNAuthorizationStatus status = [CNContactStore authorizationStatusForEntityType:CNEntityTypeContacts];
            if (status == CNAuthorizationStatusDenied || status == CNAuthorizationStatusRestricted)
            {
                [self showAlertWithType:systemAuthorityAddressBook];
                return NO;
                
            }
        }
        else
        {
            ABAuthorizationStatus ABstatus = ABAddressBookGetAuthorizationStatus();
            if (ABstatus == kABAuthorizationStatusDenied || ABstatus == kABAuthorizationStatusRestricted)
            {
                [self showAlertWithType:systemAuthorityAddressBook];
                return NO;
            }
        }
        return YES;
    }
    
    - (BOOL)calendarAuthority
    {
        EKAuthorizationStatus EKstatus = [EKEventStore authorizationStatusForEntityType:EKEntityTypeEvent];
        if (EKstatus == EKAuthorizationStatusDenied || EKstatus == EKAuthorizationStatusRestricted)
        {
            [self showAlertWithType:systemAuthorityCalendar];
            return NO;
        }
        return YES;
    }
    
    - (BOOL)reminderAuthority
    {
        EKAuthorizationStatus EKstatus = [EKEventStore authorizationStatusForEntityType:EKEntityTypeReminder];
        if (EKstatus == EKAuthorizationStatusDenied || EKstatus == EKAuthorizationStatusRestricted)
        {
            [self showAlertWithType:systemAuthorityReminder];
            return NO;
        }
        return YES;
    }
    
    - (void)showAlertWithType:(systemAuthorityType)type
    {
        NSString *title;
        NSString *msg;
        switch (type) {
            case systemAuthorityCamera:
                title = @"未获得授权使用相机";
                msg = @"请在设备的 设置-隐私-相机 中打开。";
                break;
            case systemAuthorityPhotoLibrary:
                title = @"未获得授权使用相册";
                msg = @"请在设备的 设置-隐私-照片 中打开。";
                break;
            case systemAuthorityNotifacation:
                title = @"未获得授权使用推送";
                msg = @"请在设备的 设置-隐私-推送 中打开。";
                break;
            case systemAuthorityNetwork:
                title = @"未获得授权使用网络";
                msg = @"请在设备的 设置-隐私-网络 中打开。";
                break;
            case systemAuthorityAudio:
                title = @"未获得授权使用麦克风";
                msg = @"请在设备的 设置-隐私-麦克风 中打开。";
                break;
            case systemAuthorityLocation:
                title = @"未获得授权使用定位";
                msg = @"请在设备的 设置-隐私-定位 中打开。";
                break;
            case systemAuthorityAddressBook:
                title = @"未获得授权使用通讯录";
                msg = @"请在设备的 设置-隐私-通讯录 中打开。";
                break;
            case systemAuthorityCalendar:
                title = @"未获得授权使用日历";
                msg = @"请在设备的 设置-隐私-日历 中打开。";
                break;
            case systemAuthorityReminder:
                title = @"未获得授权使用备忘录";
                msg = @"请在设备的 设置-隐私-备忘录 中打开。";
                break;
            default:
                break;
        }
        UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:title message:msg delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"立即前往", nil];
        alertView.delegate = self;
        [alertView show];
        alertView.tag = type;
    }
    
    #pragma mark -- AlertView Delegate
    - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
    {
        if (buttonIndex == 1)
        {
            //跳转到设置界面,让用户开启权限
            NSURL *url;
            if(SYSTEMVERSION >= 8.0)
            {
                //iOS8 以后
                url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
                if ([[UIApplication sharedApplication] canOpenURL:url])
                [[UIApplication sharedApplication] openURL:url];
            }
            else
            {
                //iOS8 之前
                //以下方法暂未测试
                switch (alertView.tag) {
                    case systemAuthorityCamera:
                        url = [NSURL URLWithString:@"prefs:root=Privacy&path=CAMERA"];
                        break;
                    case systemAuthorityPhotoLibrary:
                        url = [NSURL URLWithString:@"prefs:root=Privacy&path=PHOTOS"];
                        break;
                    case systemAuthorityNetwork:
                        url = [NSURL URLWithString:@"prefs:root=General&path=Network"];
                        break;
                    case systemAuthorityAudio:
                        url = [NSURL URLWithString:@"prefs:root=Privacy&path=Audio"];
                        break;
                    case systemAuthorityLocation:
                        url = [NSURL URLWithString:@"prefs:root=LOCATION_SERVICES"];
                        break;
                    case systemAuthorityAddressBook:
                        url = [NSURL URLWithString:@"prefs:root=Privacy&path=AddressBook"];
                        break;
                    case systemAuthorityCalendar:
                        url = [NSURL URLWithString:@"prefs:root=Privacy&path=Calendar"];
                        break;
                    case systemAuthorityReminder:
                        url = [NSURL URLWithString:@"prefs:root=Privacy&path=NOTES"];
                        break;
                    
                    default:
                        break;
                }
                if ([[UIApplication sharedApplication] canOpenURL:url])
                {
                    [[UIApplication sharedApplication] openURL:url];
                }
            }
        }
    }
    @end
    

    相关文章

      网友评论

      本文标题:iOS 各种权限判断及设置跳转

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