PHPhotoLibrary requestAuthorization, not requesting
Asked Answered
W

2

3

For testing, I was trying to recreate the system 'Requesting Access' popup experience.

Update:
Under iOS 11, after deleting the App, the system popup will be displayed again.


(previous question)

First time the App run (and the only time), the system popup showed, requesting access. After that, not even deleting the App and restarting the device will trigger that popup again.

In other words, the device 'remembers' the user request and there's no way to reset it.

Here's the code:

[PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) {

    switch (status) {
        case PHAuthorizationStatusAuthorized:
            NSLog(@"PHAuthorizationStatusAuthorized");
            break;

        case PHAuthorizationStatusDenied:
            NSLog(@"PHAuthorizationStatusDenied");
            break;
        case PHAuthorizationStatusNotDetermined:
            NSLog(@"PHAuthorizationStatusNotDetermined");
            break;
        case PHAuthorizationStatusRestricted:
            NSLog(@"PHAuthorizationStatusRestricted");
            break;
    }

}];

When access is off in settings, it keeps printing "PHAuthorizationStatusDenied". But does not present any popup. Returns immediately.

It was suggested to add 'Bundle display name' to the plist. Tried it to no avail, with empty value, $(PRODUCT_NAME), and different strings.

Cleaned project, deleted DrivedData (and delete App from simulator every time). No luck.

More info:

The Apple sample code "SamplePhotosApp", is crashing once you turn off photo access in the Settings.

Wardieu answered 29/1, 2016 at 11:28 Comment(1)
The solution to the problem with cached authorizationStatus response (even after app remove) is described here: #27726854Montfort
W
6

After further reading, this seems to be by design.

From Apple:

This method always returns immediately. If the user has previously granted or denied photo library access permission, it executes the handler block when called; otherwise, it displays an alert and executes the block only after the user has responded to the alert.

Saying 'This method always returns immediately' if user was prompt once. After that it will not show the request again. Seems to be no way (but some custom message) to use the system message again.

Wardieu answered 29/1, 2016 at 12:13 Comment(2)
Please take a look at my answer here: #26595843Symonds
From the linked answer, the way around it is to change the bundle identifier. Seems the issue is still open; that is, there's no way to 'reset' an App's bundle ID once it is written, short of a complete device reset. Of course, on a simulator, deleting the device will do it.Wardieu
M
1

Important thing is to add this to your app Info.plist file. "Privacy - Photo Library Usage Description"

I used something like this: "Used for accessing photos in the photos library."

This description is to be shown in requested access alert box. Can be localized if you want.

+ (void)imageLibraryCheckAccess:(UIViewController *)presenting
                        handler:(void (^)(PHAuthorizationStatus status))handler
{
        PHAuthorizationStatus status = [PHPhotoLibrary authorizationStatus];
        if (status == PHAuthorizationStatusNotDetermined) {
                [PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) {
                        if (status != PHAuthorizationStatusAuthorized) {
                                if (handler != nil)
                                        handler(status);
                                NSString *title = NSLocalizedStringWithDefaultValue(@"photo.library.access.disabled.alert.title",
                                                                                    @"Localizable",  [NSBundle mainBundle],
                                                                                    @"Photos access", @"Alert title.");
                                NSString *text = NSLocalizedStringWithDefaultValue(@"photo.library.access.disabled.alert.text",
                                                                                   @"Localizable",  [NSBundle mainBundle],
                                                                                   @"You explicitly disabled photo library access. This results in inability to work with photos.",
                                                                                   @"Alert text.");
                                [self alertWithPresenting:presenting title:title text:text buttons:@[[L10n okButton]]
                                                  handler:nil];
                        } else if (status == PHAuthorizationStatusAuthorized) {
                                if (handler != nil)
                                        handler(status);
                        }
                }];
        } else if (status != PHAuthorizationStatusAuthorized) {
                if (handler != nil)
                        handler(status);
                NSString *title = NSLocalizedStringWithDefaultValue(@"photo.library.access.notauthorized.alert.title",
                                                                    @"Localizable",  [NSBundle mainBundle],
                                                                    @"Photos access", @"Alert title.");
                NSString *text = NSLocalizedStringWithDefaultValue(@"photo.library.access.notauthorized.alert.text",
                                                                   @"Localizable",  [NSBundle mainBundle],
                                                                   @"Photo library access is disabled. Please check the application permissions or parental control settings in order to work with photos.", @"Alert text.");
                [self alertWithPresenting:presenting title:title text:text buttons:@[[L10n okButton]]
                                  handler:nil];
        } else if (status == PHAuthorizationStatusAuthorized) {
                if (handler != nil)
                        handler(status);
        }
}

And here is how I use it:

[YourClassName imageLibraryCheckAccess:self handler:^(PHAuthorizationStatus status) {
        if (status == PHAuthorizationStatusAuthorized) {
        }
}];

Good luck!

Mechanic answered 8/2, 2018 at 8:31 Comment(3)
I have both Privacy - Photo Library Usage Description and Privacy - Photo Library Additions Usage Description (new, under iOS 11). The App will simply crash without them. The issue was to trigger the first system popup. It is now resolved under iOS 11. The system will request permission again after app deletion.Wardieu
@Wardieu Got it, seems I did not finish with reading your question and jumped to answering it. )) Should I remove the post or still useful?Mechanic
Up to you. I thought of removing the whole post (since it's no longer relevant) but will leave it for the record. Thanks!Wardieu

© 2022 - 2024 — McMap. All rights reserved.