iOS determines whether some permissions are forbidden

Source: Internet
Author: User

iOS often encounter access to the album, Camera, Mac, Bluetooth, and push permissions, so every time we want to use these permissions are to remember to check whether the user is allowed, if the user has forbidden your access, you still go to the album or camera, then you will first appear the following this prompt. And is in English, this time the user may be a bit confused, this is the best way for us to give a hint, the user click OK, we'd better jump to the app's permission out, let the user a key to allow.


Permissions are disabled
1. Check if album permissions are disabled

(1.) iOS7 prior to the Judgment method (including iOS7)

Import header File #import <AssetsLibrary/AssetsLibrary.h>

Here is the code that determines whether there is permission

Alauthorizationstatus author =[alassetslibrary authorizationstatus];

if (author = = Alauthorizationstatusrestricted | | author ==alauthorizationstatusdenied) {

No permissions this time the best to give a hint, the user click is to jump to the app's permission settings within the user dynamic small hands can allow permissions

}

The following is an enumeration of Alauthorizationstatus

typedef ns_enum (Nsinteger, Alauthorizationstatus) {

Alauthorizationstatusnotdetermined Ns_enum_deprecated_ios (6_0, 9_0) = 0,//user has not made a greeting to select this application

Alauthorizationstatusrestricted Ns_enum_deprecated_ios (6_0, 9_0),//This application has no authorized access to the photo data. may be Parental control permissions

Alauthorizationstatusdenied Ns_enum_deprecated_ios (6_0, 9_0),//user has explicitly denied access to the permission

Alauthorizationstatusauthorized Ns_enum_deprecated_ios (6_0, 9_0)//user has authorized app access to photo data

} ns_deprecated_ios (6_0, 9_0, "use Phauthorizationstatus in the Photos framework instead");

(2) method of judgment after iOS8 (including iOS8)

Import Header File #import<photos/photos.h>

Judging the Code

Phauthorizationstatus status = [Phphotolibrary authorizationstatus];

if (status = = Phauthorizationstatusrestricted | |

Status = = phauthorizationstatusdenied) {

No permissions this time the best to give a hint, the user click is to jump to the app's permission settings within the user dynamic small hands can allow permissions

}

typedef ns_enum (Nsinteger, Phauthorizationstatus) {

phauthorizationstatusnotdetermined = 0,//user has not made a greeting to select this application

phauthorizationstatusrestricted,//This application does not have permission to access the photo data. may be Parental control permissions

Phauthorizationstatusdenied,//user has explicitly denied access to the access

phauthorizationstatusauthorized//user has authorized app access to photo data

} photos_available_ios_tvos (8_0, 10_0);

2. Check if camera permissions are allowed to access

#import <AVFoundation/AVCaptureDevice.h>

Avauthorizationstatus authstatus = [Avcapturedevice authorizationstatusformediatype:avmediatypevideo];

if (authstatus = = avauthorizationstatusrestricted | | authstatus ==avauthorizationstatusdenied)

{

No permissions

}

Typedefns_enum (nsinteger,avauthorizationstatus) {avauthorizationstatusnotdetermined=0,//system is also unknown if access, avauthorizationstatusrestricted,//restricted avauthorizationstatusdenied,//does not allow avauthorizationstatusauthorized when opening the camera for the first time Allow status}ns_available_ios (7_0) __tvos_prohibited;

3. Check if microphone permissions are allowed to access

#import <AVFoundation/AVCaptureDevice.h>

Avauthorizationstatus authstatus = [Avcapturedevice Authorizationstatusformediatype:avmediatypeaudio];

if (authstatus = = avauthorizationstatusrestricted | | authstatus ==avauthorizationstatusdenied)

{

No permissions

}

How do I feel wrong? Why is it the same as viewing camera permissions? Careful little partner may have found that the difference between the two is not the same as a parameter avmediatypevideo,avmediatypeaudio, of course, the enumeration of the result is the same, here no longer repeat.

4. Determine if the user is allowed to push

There are some differences between iOS8 and iOS8, so the iOS version needs to be judged.

#define IOS8 ([[[Uidevice Currentdevice] systemversion] doublevalue] >=8.0? Yes:no)

If (IOS8) {//ios8 above contains iOS8

if ([[UIApplication sharedapplication] Currentusernotificationsetting s].types  ==uiusernotificationtypenone) {

NSLog (@ "not open");

}

}else{//iOS7

if ([[uiapplication sharedapplication] enabledremotenotificationtypes]  = = UIR Emotenotificationtypenone) {

NSLog (@ "not open");

}

}

typedef ns_options (Nsuinteger, Uiusernotificationtype) {

Uiusernotificationtypenone = 0,//user banned push

Uiusernotificationtypebadge = 1 << 0,//user turned on the push angle mark

Uiusernotificationtypesound = 1 << 1,//user turned on push beep

Uiusernotificationtypealert = 1 << 2,//user turned on notification bar reminder

} ns_enum_deprecated_ios (8_0, 10_0, "use usernotifications Framework ' s unauthorizationoptions") __tvos_prohibited;

5. Determine if Bluetooth is turned on and need to jump to settings to open

In fact, when we use Bluetooth is created when the need to follow cbcentralmanagerdelegate this agent, he has an agent method is constantly monitoring the change in Bluetooth status.

-(void) Centralmanagerdidupdatestate: (Cbcentralmanager *) central{

Switch (central.state) {

Case Cbmanagerstateunknown:

{

The initial time is unknown (just when it was created)

}

Break

Case cbmanagerstateresetting:

{

Resetting status

}

Break

Case cbmanagerstateunsupported:

{

Status not supported by the device

}

Break

Case cbmanagerstateunauthorized:

{

[Stringforcentral appendstring:@ "resetting\n"];

Device Not authorized status

}

Break

Case Cbmanagerstatepoweredoff:

{

Device off State

}

Break

Case Cbmanagerstatepoweredon:

{

Device on state-available status

}

Break

Default

{

}

Break

}

}

}

We can do something in a different state. Of course, we can also get the link state of Bluetooth through Cbcentralmanager, and also we create Cbcentralmanager system if it is found that Bluetooth does not turn on will automatically pop up a window can be set inside to open Bluetooth.


Of course if we want to give a user prompt and then jump to the Settings page is also possible, iOS10 later open the way some differences

NSString * urlstring = @ "App-prefs:root=bluetooth";

if ([[[UIApplication sharedapplication] Canopenurl:[nsurl urlwithstring:urlstring]) {

if (ios_version>10.0) {

[[UIApplication sharedapplication] Openurl:[nsurl urlwithstring:urlstring] options:@{} CompletionHandler:nil];

} else {

[[UIApplication sharedapplication] Openurl:[nsurl urlwithstring:urlstring]];

}

}

6. Determine if the location service is disabled

if ([Cllocationmanager locationservicesenabled] && [Cllocationmanager authorizationstatus] = = kclauthorizationstatusdenied) {

NSLog (@ "not open");

}

Locationservicesenabled whether the returned result is set to the location service, presumably this means that the first time we access the location is that the system will give the user a prompt, whether to allow the app to use location information. If you choose Yes or no, this value is yes,kclauthorizationstatusdenied explicitly denied access to location information on behalf of the user.

typedef ns_enum (int, clauthorizationstatus) {

kclauthorizationstatusnotdetermined = 0,//Location Service Authorization status is the user does not decide whether to use location services.

The kclauthorizationstatusrestricted,//location Service authorization status is restricted. It is possible that the user cannot change the location service due to activity restrictions. This state may not be a user-denied location service.

The kclauthorizationstatusdenied,//location Service authorization status has been explicitly banned by the user or closed in the location service in the settings.

Kclauthorizationstatusauthorizedalways ns_enum_available (10_12, 8_0),///Location Service authorization status has been enabled by the user to obtain position information in any state. This includes monitoring areas, accessing areas, or when there are significant changes in location.

Kclauthorizationstatusauthorizedwheninuse ns_enum_available (NA, 8_0),///Location Service authorization status is only allowed when using the application.

Kclauthorizationstatusauthorized ns_enum_deprecated (10_6, NA, 2_0, 8_0, "Use Kclauthorizationstatusauthorizedalways" __tvos_prohibited __watchos_prohibited = kclauthorizationstatusauthorizedalways//This enumeration value has been deprecated. He is the equivalent

Kclauthorizationstatusauthorizedalways this value.

};

Jump to the Settings page and let the user set permissions

If we need to jump to the set location let the user allow permission by the way

Nsurl * url = [Nsurl urlwithstring:uiapplicationopensettingsurlstring];

if ([[[UIApplication sharedapplication] Canopenurl:url]) {

if (ios_version>10.0) {

[[UIApplication sharedapplication] Openurl:url options:@{} Completionhandler:nil];

} else {

[[UIApplication sharedapplication] openurl:url];

}

}

When calling this method, be sure to have these permissions in order to invoke, such as itself your app does not involve any privacy rights issues, you directly call this interface he will not go to set, but to the home page.

iOS determines whether some permissions are forbidden

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.