Request authorization to Media Library programmatically fails

一笑奈何 提交于 2019-11-30 09:37:20

The MPMediaLibrary will only automatically prompt the user once. The state is MPMediaLibraryAuthorizationStatusNotDetermined if you ask for it before it has been granted or denied by the user. If they have denied access previously, you need to send the user to the System Settings so they can manually turn it on for your app.

The following code is how we are doing it.

+ (void) validateMediaLibraryForMinimumIosAndAboveWithViewController:(UIViewController *)viewController
                                                        ifAuthorized:(void(^)())authorizedBlock
                                                     ifNotAuthorized:(void(^)())notAuthorizedBlock
{
    MPMediaLibraryAuthorizationStatus authorizationStatus = MPMediaLibrary.authorizationStatus;

    switch (authorizationStatus)
    {
        case MPMediaLibraryAuthorizationStatusAuthorized:
        {
            // We are already authorized - proceed
            if( authorizedBlock )
            {
                authorizedBlock();
            }
            break;
        }
        case MPMediaLibraryAuthorizationStatusNotDetermined:
        {
            // Not yet authorized - request it from the system
            [MPMediaLibrary requestAuthorization:^(MPMediaLibraryAuthorizationStatus authorizationStatus)
             {
                 if ( authorizationStatus == MPMediaLibraryAuthorizationStatusAuthorized )
                 {
                     if( authorizedBlock )
                     {
                         authorizedBlock();
                     }
                 }
                 else
                 {
                     PLog(@"The Media Library was not authorized by the user");
                     if( notAuthorizedBlock )
                     {
                         notAuthorizedBlock();
                     }
                 }
             }];
            break;
        }

        case MPMediaLibraryAuthorizationStatusRestricted:
        case MPMediaLibraryAuthorizationStatusDenied:
        {
            // user has previously denied access. Ask again with our own alert that is similar to the system alert
            // then take them to the System Settings so they can turn it on for the app
            NSString *titleString  = NSLocalizedStringWithDefaultValue(@"Media Library Privacy Alert Title",
                                                                       @"Localizable",
                                                                       [NSBundle mainBundle],
                                                                       @"Would Like to Access Apple Music And Your Media Library",
                                                                       @"Title for dialog requesting media library access");

            [self displayPermissionAlertFromViewController:viewController
                                                 withTitle:titleString];
            if( notAuthorizedBlock )
            {
                notAuthorizedBlock();
            }
            break;
        }
    }
}

+ (void)displayPermissionAlertFromViewController:(UIViewController *)viewController withTitle:(NSString *)title
{
    NSString* appName = [[NSProcessInfo processInfo] processName];

    NSString *titleString = [NSString stringWithFormat:@"\"%@\" %@",appName, title];

    NSString *cancelString = NSLocalizedStringWithDefaultValue(@"Don't Allow",
                                                               @"Localizable",
                                                               [NSBundle mainBundle],
                                                               @"Don't Allow",
                                                               @"Don't allow button");

    NSString *settingsString = NSLocalizedStringWithDefaultValue( @"Settings",
                                                                 @"Localizable",
                                                                 [NSBundle mainBundle],
                                                                 @"Settings",
                                                                 @"Settings button label");

    UIAlertController *alertController = [UIAlertController
                                          alertControllerWithTitle:titleString
                                          message:nil
                                          preferredStyle:UIAlertControllerStyleAlert];

    [alertController addAction:[UIAlertAction actionWithTitle:cancelString
                                                        style:UIAlertActionStyleDefault
                                                      handler:nil]];

    [alertController addAction:[UIAlertAction actionWithTitle:settingsString
                                                        style:UIAlertActionStyleDefault
                                                      handler:
                                ^(UIAlertAction * _Nonnull action)
                                {
                                    NSURL *url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
                                    [[UIApplication sharedApplication] openURL:url];
                                }]];

    [viewController presentViewController:alertController animated:true completion:nil];
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!