PHPhotoLibrary requestAuthorization, not requesting

后端 未结 2 1794
一个人的身影
一个人的身影 2021-01-22 16:58

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

Update:
Under iOS 11, after deleting the App, the system po

相关标签:
2条回答
  • 2021-01-22 17:43

    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.

    0 讨论(0)
  • 2021-01-22 17:51

    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!

    0 讨论(0)
提交回复
热议问题