iOS開發中有時候有這樣的需求:當用戶設置不允許訪問照片、麥克風和相機等系統權限的時候,這時需要直接跳轉到系統的隱私界面進行設置。
判斷是否開啟權限
前面已經說過,我們需要在用戶不允許訪問的時候跳轉,那么首先我們就要判斷一些是否已經開啟系統相機權限了。
照片權限檢測
需要:#import <AssetsLibrary/AssetsLibrary.h> //導入此類和AssetsLibrary.framework框架
代碼如下:
int author = [ALAssetsLibrary authorizationStatus]; NSLog(@"author type:%d",author); if(author == ALAuthorizationStatusRestricted || author == ALAuthorizationStatusDenied) { // The user has explicitly denied permission for media capture. UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"無法使用相冊" message:@"請在iPhone的\"設置-隱私-照片\"中允許訪問照片。" delegate:self cancelButtonTitle:@"確定" otherButtonTitles:nil]; [alert show]; return;
ALAssetsLibrary類
ALAssetsLibrary類是代表系統中整個資源庫,使用它可以訪問資源庫中的資源和保存照片,視頻等功能。
//判斷當前應用是否能訪問相冊資源 /* typedef NS_ENUM(NSInteger, ALAuthorizationStatus) { ALAuthorizationStatusNotDetermined = 0, 用戶尚未做出了選擇這個應用程序的問候 ALAuthorizationStatusRestricted, 此應用程序沒有被授權訪問的照片數據。可能是家長控制權限。 ALAuthorizationStatusDenied, 用戶已經明確否認了這一照片數據的應用程序訪問. ALAuthorizationStatusAuthorized 用戶已授權應用訪問照片數據. } */
訪問攝像頭
需要:#import <AVFoundation/AVFoundation.h>
代碼如下:
if(isIOS7AndLater) { NSString *mediaType = AVMediaTypeVideo;// Or AVMediaTypeAudio AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:mediaType]; NSLog(@"---cui--authStatus--------%d",authStatus); // This status is normally not visible—the AVCaptureDevice class methods for discovering devices do not return devices the user is restricted from accessing. if(authStatus ==AVAuthorizationStatusRestricted){ NSLog(@"Restricted"); }else if(authStatus == AVAuthorizationStatusDenied){ // The user has explicitly denied permission for media capture. NSLog(@"Denied"); //應該是這個,如果不允許的話 UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"請在設備的\"設置-隱私-相機\"中允許訪問相機。" delegate:self cancelButtonTitle:@"確定" otherButtonTitles:nil]; [alert show]; return; } else if(authStatus == AVAuthorizationStatusAuthorized){//允許訪問 // The user has explicitly granted permission for media capture, or explicit user permission is not necessary for the media type in question. NSLog(@"Authorized"); }else if(authStatus == AVAuthorizationStatusNotDetermined){ // Explicit user permission is required for media capture, but the user has not yet granted or denied such permission. [AVCaptureDevice requestAccessForMediaType:mediaType completionHandler:^(BOOL granted) { if(granted){//點擊允許訪問時調用 //用戶明確許可與否,媒體需要捕獲,但用戶尚未授予或拒絕許可。 NSLog(@"Granted access to %@", mediaType); } else { NSLog(@"Not granted access to %@", mediaType); } }]; }else { NSLog(@"Unknown authorization status"); } }
麥克風權限檢測
代碼如下:
//檢測麥克風功能是否打開 [[AVAudioSessionsharedInstance]requestRecordPermission:^(BOOL granted) { if (!granted) { [ViewUtilalertViewWithString:NSLocalizedString(@"麥克風功能未開啟",nil)]; } else { [selfrecord:sender]; } }];
如何跳轉到系統設置界面
判斷權限是否設置之后就可以在相應的代理方法進行界面跳轉了,那么如何進行跳轉呢?
首先要在項目中的info.plist中添加 URL types 並設置一項URL Schemes為prefs,如下圖:

實現代碼如下:
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:url]];//url為具體路徑
以下是跳轉到一些常用界面的代碼
隱私->照片界面
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"prefs:root=Privacy&path=PHOTOS"]];
隱私->相機界面
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"prefs:root=Privacy&path=CAMERA"]];
藍牙設置界面
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"prefs:root=Bluetooth"]];
其他界面參數配置
About — prefs:root=General&path=About
Accessibility — prefs:root=General&path=ACCESSIBILITY
Airplane Mode On — prefs:root=AIRPLANE_MODE
Auto-Lock — prefs:root=General&path=AUTOLOCK
Brightness — prefs:root=Brightness
Bluetooth — prefs:root=General&path=Bluetooth
Date & Time — prefs:root=General&path=DATE_AND_TIME
FaceTime — prefs:root=FACETIME
General — prefs:root=General
Keyboard — prefs:root=General&path=Keyboard
iCloud — prefs:root=CASTLE
iCloud Storage & Backup — prefs:root=CASTLE&path=STORAGE_AND_BACKUP
International — prefs:root=General&path=INTERNATIONAL
Location Services — prefs:root=LOCATION_SERVICES
Music — prefs:root=MUSIC
Music Equalizer — prefs:root=MUSIC&path=EQ
Music Volume Limit — prefs:root=MUSIC&path=VolumeLimit
Network — prefs:root=General&path=Network
Nike + iPod — prefs:root=NIKE_PLUS_IPOD
Notes — prefs:root=NOTES
Notification — prefs:root=NOTIFICATIONS_ID
Phone — prefs:root=Phone
Photos — prefs:root=Photos
Profile — prefs:root=General&path=ManagedConfigurationList
Reset — prefs:root=General&path=Reset
Safari — prefs:root=Safari
Siri — prefs:root=General&path=Assistant
Sounds — prefs:root=Sounds
Software Update — prefs:root=General&path=SOFTWARE_UPDATE_LINK
Store — prefs:root=STORE
Twitter — prefs:root=TWITTER
Usage — prefs:root=General&path=USAGE
VPN — prefs:root=General&path=Network/VPN
Wallpaper — prefs:root=Wallpaper
Wi-Fi — prefs:root=WIFI