以编程方式请求授权媒体库失败 [英] Request authorization to Media Library programmatically fails

查看:240
本文介绍了以编程方式请求授权媒体库失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

iOS 10现在需要用户的许可才能访问媒体库。我们在使用之前检查是否可以访问媒体库,如果没有,我们会使用 [MPMediaLibrary requestAuthorization:再次向用户请求授权。

iOS 10 now requires the user's permission to access the Media Library. We check if we have access to the Media Library before we use it, and if not we then use [MPMediaLibrary requestAuthorization: to request authorization again from the user.

我希望这会显示相同的弹出窗口请求,以访问我们在应用启动时获得的媒体库,但没有任何反应。它只是从之前返回 MPMediaLibraryAuthorizationStatusDenied 状态。

I'm expecting this to show the same popup request to access the Media Library that we get at app startup, but nothing happens. It is simply returning with the MPMediaLibraryAuthorizationStatusDenied status from before.

requestAuthorization的文档目前还不完整,所以我不知道我是不是只是错误地使用了这个,或者还有其他问题。

The docs for requestAuthorization are incomplete at this time, so I can't tell if I'm just using this incorrectly, or there is something else wrong.

    if ( MPMediaLibrary.authorizationStatus == MPMediaLibraryAuthorizationStatusAuthorized)
    {
        // we already have access to the Media Library - use it here...
    }
    else
    {
        // We expect this to show a popup so the user can grant access, but does not work
        [MPMediaLibrary requestAuthorization:^(MPMediaLibraryAuthorizationStatus authorizationStatus)
         {
             if ( authorizationStatus == MPMediaLibraryAuthorizationStatusAuthorized )
             {
                 // success: the user authorized - use it here...
             }
             else
             {
                 // user did not authorize - tell user why here...
             }
         }];
    }

更新

显然没有办法让原始对话框重新出现(见下面的评论)。我现在使用此代码至少将我带到设置中的正确位置,以便用户可以进行更改。 (适用于iOS8及更高版本)

Apparently there is no way to cause the original dialog to reappear (see comments below). I'm now using this code to at least take me to the right place in settings so the user can make the change. (for iOS8 and beyond)

NSURL *url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
[[UIApplication sharedApplication] openURL:url];


推荐答案

MPMediaLibrary只会自动提示用户一次。如果您在用户授予或拒绝之前要求,则状态为 MPMediaLibraryAuthorizationStatusNotDetermined 。如果他们之前拒绝访问,您需要将用户发送到系统设置,以便他们可以为您的应用手动启用它。

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];
}

这篇关于以编程方式请求授权媒体库失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆