There are times in iOS development where you need to jump directly to the system's privacy interface when the user sets the system permissions to not allow access to photos, microphones, and cameras.
Determine whether permissions are turned on
As we have already said, we need to jump when the user is not allowed to access, so first we have to determine whether the system camera permission has been turned on.
Photo Permission Detection
Required: #import <AssetsLibrary/AssetsLibrary.h>//import such and assetslibrary.framework frameworks
The code is as follows:
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: @ "cannot use album" Message:@ "Allow access to photos in iphone's \ settings-privacy-photos \". "Delegate:self cancelbuttontitle:@" OK "otherButtonTitles:< Span class= "hljs-literal" >nil]; [Alert show]; return;
Alassetslibrary class
The Alassetslibrary class is representative of the entire repository in the system, using it to access resources in the resource pool and to save features such as photos, videos, and more.
//判断当前应用是否能访问相册资源 /* NS_ENUM(NSInteger, ALAuthorizationStatus) { ALAuthorizationStatusNotDetermined = 0, 用户尚未做出了选择这个应用程序的问候 ALAuthorizationStatusRestricted, 此应用程序没有被授权访问的照片数据。可能是家长控制权限。 ALAuthorizationStatusDenied, 用户已经明确否认了这一照片数据的应用程序访问. ALAuthorizationStatusAuthorized 用户已授权应用访问照片数据. } */
Accessing the camera
Need: #import <AVFoundation/AVFoundation.h>
The code is as follows:
if (isios7andlater) {NSString *mediatype =Avmediatypevideo;Or AvmediatypeaudioAvauthorizationstatus Authstatus = [Avcapturedevice Authorizationstatusformediatype:mediatype];NSLog (@ "---cui--authstatus--------%d", authstatus);This status was normally not Visible-the Avcapturedevice class methods for discovering devices does not return devices the User is restricted from accessing.if (Authstatus = =avauthorizationstatusrestricted) {NSLog (@ "Restricted"); }Elseif (Authstatus = =avauthorizationstatusdenied) {The user has explicitly denied permission for media capture.NSLog (@ "Denied");It's supposed to be this, if not allowed.Uialertview *alert = [[Uialertview Alloc] Initwithtitle:@ "hint" message:@ "Please allow access to the camera in device's \" Settings-privacy-camera \ ". "Delegate:Self Cancelbuttontitle:@ "OK" otherbuttontitles:NIL]; [Alert show];Return }Elseif (Authstatus = =avauthorizationstatusauthorized) {Allow accessThe user has explicitly granted permission for media capture, or explicit user permission are not necessary for the Medi A type in question.NSLog (@ "Authorized"); }Elseif (Authstatus = =avauthorizationstatusnotdetermined) { //Explicit user permission is required to media capture, but the user have not yet granted or denied such Permissio N. [avcapturedevice requestaccessformediatype:mediatype completionhandler:^ (BOOL granted) { if (granted) { //Click Allow access when the call //user expressly permits or not, the media needs to be captured, but the user has not granted or denied the license. NSLog (@ "Granted access to%@", mediatype);} else { NSLog (@ "Not granted access to%@", mediatype);}];} else { NSLog (@ "Unknown authorization Status");}}
Microphone Permission Detection
The code is as follows:
//检测麦克风功能是否打开 [[AVAudioSessionsharedInstance]requestRecordPermission:^(BOOL granted) { if (!granted) { [ViewUtilalertViewWithString:NSLocalizedString(@"麦克风功能未开启",nil)]; } else { [selfrecord:sender]; } }];
How to jump to the system settings screen
Determine whether the permissions are set after the corresponding proxy method to the interface to jump, then how to jump it?
First, add the URL types to the Info.plist in the project and set a URL schemes to prefs, such as:
1.jpg
The implementation code is as follows:
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:url]];//url为具体路径
Here's the code to jump to some common interface
Privacy-Photo Interface
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"prefs:root=Privacy&path=PHOTOS"]];
Privacy-Camera Interface
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"prefs:root=Privacy&path=CAMERA"]];
Bluetooth Settings interface
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"prefs:root=Bluetooth"]];
Other interface parameter configuration
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
Source: http://www.jianshu.com/p/1fb3f60b689a Small ball why
IOS System Permissions