iOS 各種權限判斷及設置跳轉

iOS8之后用一下方法跳轉到設置界面

//手機系統(tǒng)版本號
#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"http://iCloud
@"prefs:root=FACETIME",//FaceTime
@"prefs:root=LOCATION_SERVICES",//定位服務
@"prefs:root=Phone",//電話
@"prefs:root=AIRPLANE_MODE"http://飛行模式 
@"prefs:root=General&path=Network"http://網絡
@"prefs:root=VIDEO"http://視頻

 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",//語言與地區(qū) 
@"prefs:root=General&path=ManagedConfigurationList",//描述文件
prefs:root=General&path=ACCESSIBILITY//重力感應  
prefs:root=General&path=USAGE/CELLULAR_USAGE//用量 

.m 方發(fā)聲明

#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 方法實現(xiàn)

#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>

//手機系統(tǒng)版本號
#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
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
  • 序言:七十年代末懦冰,一起剝皮案震驚了整個濱河市灶轰,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌刷钢,老刑警劉巖笋颤,帶你破解...
    沈念sama閱讀 218,858評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異,居然都是意外死亡伴澄,警方通過查閱死者的電腦和手機赋除,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,372評論 3 395
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來非凌,“玉大人举农,你說我怎么就攤上這事〕ㄎ耍” “怎么了颁糟?”我有些...
    開封第一講書人閱讀 165,282評論 0 356
  • 文/不壞的土叔 我叫張陵,是天一觀的道長喉悴。 經常有香客問我棱貌,道長,這世上最難降的妖魔是什么箕肃? 我笑而不...
    開封第一講書人閱讀 58,842評論 1 295
  • 正文 為了忘掉前任婚脱,我火速辦了婚禮,結果婚禮上勺像,老公的妹妹穿的比我還像新娘障贸。我一直安慰自己,他們只是感情好咏删,可當我...
    茶點故事閱讀 67,857評論 6 392
  • 文/花漫 我一把揭開白布惹想。 她就那樣靜靜地躺著问词,像睡著了一般督函。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上激挪,一...
    開封第一講書人閱讀 51,679評論 1 305
  • 那天辰狡,我揣著相機與錄音,去河邊找鬼垄分。 笑死宛篇,一個胖子當著我的面吹牛,可吹牛的內容都是我干的薄湿。 我是一名探鬼主播叫倍,決...
    沈念sama閱讀 40,406評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼豺瘤!你這毒婦竟也來了吆倦?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 39,311評論 0 276
  • 序言:老撾萬榮一對情侶失蹤坐求,失蹤者是張志新(化名)和其女友劉穎蚕泽,沒想到半個月后,有當地人在樹林里發(fā)現(xiàn)了一具尸體桥嗤,經...
    沈念sama閱讀 45,767評論 1 315
  • 正文 獨居荒郊野嶺守林人離奇死亡须妻,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 37,945評論 3 336
  • 正文 我和宋清朗相戀三年仔蝌,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片荒吏。...
    茶點故事閱讀 40,090評論 1 350
  • 序言:一個原本活蹦亂跳的男人離奇死亡敛惊,死狀恐怖,靈堂內的尸體忽然破棺而出司倚,到底是詐尸還是另有隱情豆混,我是刑警寧澤,帶...
    沈念sama閱讀 35,785評論 5 346
  • 正文 年R本政府宣布动知,位于F島的核電站皿伺,受9級特大地震影響,放射性物質發(fā)生泄漏盒粮。R本人自食惡果不足惜鸵鸥,卻給世界環(huán)境...
    茶點故事閱讀 41,420評論 3 331
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望丹皱。 院中可真熱鬧妒穴,春花似錦、人聲如沸摊崭。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,988評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽呢簸。三九已至矮台,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間根时,已是汗流浹背瘦赫。 一陣腳步聲響...
    開封第一講書人閱讀 33,101評論 1 271
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留蛤迎,地道東北人确虱。 一個月前我還...
    沈念sama閱讀 48,298評論 3 372
  • 正文 我出身青樓,卻偏偏與公主長得像替裆,于是被迫代替她去往敵國和親校辩。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 45,033評論 2 355

推薦閱讀更多精彩內容