iOS development--camera and album Permissions get logical Optimization

Source: Internet
Author: User
Tags blank page

In the actual project, we often need to access the device's camera or album, when the first installation of an app, the system will pop up the authorization dialog box, asking the user to make a decision whether to authorize. The overall logic is relatively simple, but the user experience needs to be optimized during use, or a bug will occur. The sample code for this blog has been uploaded to Https://github.com/chenyufeng1991/AuthorityOfCameraAndPhoto.

First, let me describe the problems that arise. As an example of accessing a photo album, I implemented the following code:

-(void) photobtnpressed: (ID) sender{    //First check if the current device supports album    if ([Uiimagepickercontroller issourcetypeavailable: Uiimagepickercontrollersourcetypephotolibrary])    {        [self Presenttoimagepickercontroller: Uiimagepickercontrollersourcetypephotolibrary];    }    else    {        [self showalertcontroller:@ prompt "message:@" current device does not support album "];    }}


-(void) Presenttoimagepickercontroller: (uiimagepickercontrollersourcetype) type{    Uiimagepickercontroller * Picker = [[Uiimagepickercontroller alloc] init];    Picker.delegate = self;    picker.allowsediting = YES;    Picker.sourcetype = type;    [Self Presentviewcontroller:picker animated:yes completion:nil];} -(void) Showalertcontroller: (NSString *) title message: (NSString *) message{    uialertcontroller *ac = [ Uialertcontroller alertcontrollerwithtitle:title message:message Preferredstyle:uialertcontrollerstylealert];    [AC addaction:[uialertaction actionwithtitle:@ "I Know" Style:uialertactionstyledefault handler:^ (UIAlertAction * _ Nonnull action) {    }]];    [Self Presentviewcontroller:ac animated:yes completion:nil];}


To explain, it is safe to judge whether the current device supports albums. If you can use Uiimagepickercontroller access directly, otherwise pop-up alert prompt box. The first run results are as follows:


This dialog box is the system requests the user to get access to the album Permissions dialog box, if you click "OK", then you can pop up the album Interface. If you click "Don't allow", the user will not be able to access the album because I am here to demonstrate the interaction problem, so I clicked "don't allow". The following blank interface appears:


This will cause interaction problems, jump to a completely blank page, and without any hint, exactly, this is a bug. And we are unable to customize this blank page. If you take a closer look at the process of obtaining this permission, you will see that the interface pops up the blank page first, then the popup selection dialog box. That's the problem, and the same is true for getting camera permissions, so let's deal with this sort of problem.

My goal is to first pop up the authorization dialog box, if I allow authorization, then jump to the camera Interface or album interface, if I refuse authorization, then jump to a prompt with a custom page. First, take the photo album as an example to achieve:

(1) First explain the status of authorization, a total of three kinds:

Authorized: ***authorized;

Not determined: ***notdetrmined;

Rejected: ***denied,***restricted;

For these permission states of the current device, which we can read directly, I implemented the following methods:

+ (BOOL) isphotoalbumdenied{    alauthorizationstatus author = [Alassetslibrary authorizationstatus];    if (author = = Alauthorizationstatusrestricted | | author = = alauthorizationstatusdenied)    {        return YES;    }    return NO;} + (BOOL) isphotoalbumnotdetermined{    alauthorizationstatus author = [Alassetslibrary authorizationstatus];    if (author = = alauthorizationstatusnotdetermined)    {        return YES;    }    return NO;}

The Isphotoalbumdenied method determines whether the album permission has been rejected, and whether the Isphotoalbumnotdetermined method determines if it is not. The method interface is written in the Yfkit class.


(2) The authorization method is implemented as follows:

-(void) optimalphotobtnpressed: (ID) sender{if ([Uiimagepickercontroller issourcetypeavailable: Uiimagepickercontrollersourcetypephotolibrary]) {//install the app for the first time, not yet determined permission, call here if ([Yfkit isphotoalbumnotdeter                Mined]) {if ([[[Uidevice Currentdevice] systemversion] floatvalue] >= 8.0) { The API starts with iOS8.0 support//System Popup Authorization dialog box [phphotolibrary requestauthorization:^ (Phauthorizationsta Tus status) {Dispatch_async (Dispatch_get_main_queue (), ^{if (status = = Phautho rizationstatusrestricted | |                            Status = = phauthorizationstatusdenied) {//user denied, jump to custom Prompt page                            Deniedauthviewcontroller *VC = [[Deniedauthviewcontroller alloc] init];                        [Self PRESENTVIEWCONTROLLER:VC animated:yes completion:nil]; } else if (status = = Phauthorizationstatusauthorized) {//user authorization, pop-up Photo album dialog box [self Presenttoim                        Agepickercontroller:uiimagepickercontrollersourcetypephotolibrary];                }                    });            }];                The else {//above Requestauthorization interface only supports more than 8.0, if the app supports 7.0 and below, it can only be called here.            [Self presenttoimagepickercontroller:uiimagepickercontrollersourcetypephotolibrary]; }} else if ([Yfkit isphotoalbumdenied]) {//If rejected, the dialog box pops up [self showalertcont        roller:@ "Prompt" message:@ "Deny access to the album, you can set privacy to open"]; } else {//has been authorized to jump to the album page [self presenttoimagepickercontroller:uiimagepickercontrollers        Ourcetypephotolibrary];    }} else {//the current device does not support opening album [Self showalertcontroller:@ "prompt" message:@ "current device does not support album"]; }}


(3) The operation effect is as follows:

Request for authorization:


You can see that this is the first time a dialog box is confirmed, instead of jumping to the blank page of the album for pop-up confirmation.


Deny authorization:


This null-state interface can be customized.


Allow Authorization:


Jump directly to the album page.


(3) The camera request authorization logic is similar to the album, but the API is different, but it is simpler, because the API can support 7.0 and above, and the current app is basically support 7.0 and above. The interface used is avcapturedevice. The implementation method is as follows:

-(void) optimalcamerabtnpressed: (ID) sender{if ([Uiimagepickercontroller issourcetypeavailable:            Uiimagepickercontrollersourcetypecamera]) {//Apply the first request permission call here if ([Yfkit iscameranotdetermined]) { [Avcapturedevice Requestaccessformediatype:avmediatypevideo completionhandler:^ (BOOL granted)                        {Dispatch_async (Dispatch_get_main_queue (), ^{if (granted) { User authorization [Self presenttoimagepickercontroller:uiimagepickercontrollersourcetypecamer                    A]; } else {//user denied authorization Deniedauthviewcontr                        Oller *VC = [[Deniedauthviewcontroller alloc] init];                    [Self PRESENTVIEWCONTROLLER:VC animated:yes completion:nil];            }                });        }]; }//user has denied access to camera else if ([Yfkit iscameradenied]) {[SELF showalertcontroller:@ "Prompt" message:@ "Deny access to the camera, can go to set privacy to open"]; }//user allowed access to camera else {[Self presenttoimagepickercontroller:uiimagepickercontrollersourcety        Pecamera];    }} else {//the current device does not support camera, such as emulator [self showalertcontroller:@ "hint" message:@ "current device does not support photography"]; }}
The test camera needs to be tested under a real machine because the simulator does not support the camera.

Through the above code, you can effectively and controllable the camera and album permissions to apply for the process control, optimize the user experience. Some development tips are given below:

(1) for the simulator, if you want to reset the app's permissions and privacy settings, you can reset the emulator directly, select Simulator-->reset Content and setting. The next time you reinstall the app, all permissions will have to be re-applied.

(2) Reset permissions on the real machine can be entered: set--Universal---Reset location and privacy. This reset method is safe and does not cause the loss of other data on the phone, just to record some permissions to delete. When permission is required, the system will re-request it.

(3) When you just want to switch a permission, enter the settings--privacy inside the switch.


iOS development--camera and album Permissions get logical Optimization

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.