iOS 系统权限
iOS開發(fā)中有時(shí)候有這樣的需求:當(dāng)用戶設(shè)置不允許訪問照片、麥克風(fēng)和相機(jī)等系統(tǒng)權(quán)限的時(shí)候,這時(shí)需要直接跳轉(zhuǎn)到系統(tǒng)的隱私界面進(jìn)行設(shè)置。
判斷是否開啟權(quán)限
前面已經(jīng)說過,我們需要在用戶不允許訪問的時(shí)候跳轉(zhuǎn),那么首先我們就要判斷一些是否已經(jīng)開啟系統(tǒng)相機(jī)權(quán)限了。
照片權(quán)限檢測
需要:#import <AssetsLibrary/AssetsLibrary.h> //導(dǎo)入此類和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:@"請?jiān)趇Phone的\"設(shè)置-隱私-照片\"中允許訪問照片。" delegate:self cancelButtonTitle:@"確定" otherButtonTitles:nil]; [alert show]; return;ALAssetsLibrary類
ALAssetsLibrary類是代表系統(tǒng)中整個(gè)資源庫,使用它可以訪問資源庫中的資源和保存照片,視頻等功能。
//判斷當(dāng)前應(yīng)用是否能訪問相冊資源/*typedef NS_ENUM(NSInteger, ALAuthorizationStatus) {ALAuthorizationStatusNotDetermined = 0, 用戶尚未做出了選擇這個(gè)應(yīng)用程序的問候ALAuthorizationStatusRestricted, 此應(yīng)用程序沒有被授權(quán)訪問的照片數(shù)據(jù)。可能是家長控制權(quán)限。 ALAuthorizationStatusDenied, 用戶已經(jīng)明確否認(rèn)了這一照片數(shù)據(jù)的應(yīng)用程序訪問. ALAuthorizationStatusAuthorized 用戶已授權(quán)應(yīng)用訪問照片數(shù)據(jù). } */訪問攝像頭
需要:#import <AVFoundation/AVFoundation.h>
代碼如下:
if(isIOS7AndLater) {NSString *mediaType = AVMediaTypeVideo;// Or AVMediaTypeAudioAVAuthorizationStatus 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"); //應(yīng)該是這個(gè),如果不允許的話 UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"請?jiān)谠O(shè)備的\"設(shè)置-隱私-相機(jī)\"中允許訪問相機(jī)。" 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){//點(diǎn)擊允許訪問時(shí)調(diào)用 //用戶明確許可與否,媒體需要捕獲,但用戶尚未授予或拒絕許可。 NSLog(@"Granted access to %@", mediaType); } else { NSLog(@"Not granted access to %@", mediaType); } }]; }else { NSLog(@"Unknown authorization status"); } }麥克風(fēng)權(quán)限檢測
代碼如下:
//檢測麥克風(fēng)功能是否打開[[AVAudioSessionsharedInstance]requestRecordPermission:^(BOOL granted) {if (!granted){[ViewUtilalertViewWithString:NSLocalizedString(@"麥克風(fēng)功能未開啟",nil)]; } else { [selfrecord:sender]; } }];如何跳轉(zhuǎn)到系統(tǒng)設(shè)置界面
判斷權(quán)限是否設(shè)置之后就可以在相應(yīng)的代理方法進(jìn)行界面跳轉(zhuǎn)了,那么如何進(jìn)行跳轉(zhuǎn)呢?
首先要在項(xiàng)目中的info.plist中添加 URL types 并設(shè)置一項(xiàng)URL Schemes為prefs,如下圖:
1.jpg
實(shí)現(xiàn)代碼如下:
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:url]];//url為具體路徑以下是跳轉(zhuǎn)到一些常用界面的代碼
隱私->照片界面
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"prefs:root=Privacy&path=PHOTOS"]];隱私->相機(jī)界面
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"prefs:root=Privacy&path=CAMERA"]];藍(lán)牙設(shè)置界面
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"prefs:root=Bluetooth"]];其他界面參數(shù)配置
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
?
來源:http://www.jianshu.com/p/1fb3f60b689a ?簡書作者:小球why
轉(zhuǎn)載于:https://www.cnblogs.com/shifu/p/6495087.html
總結(jié)
- 上一篇: 06:图像相似度
- 下一篇: Mac系统的终端显示git当前分支