首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >PHPhotoLibrary requestAuthorization,不是请求

PHPhotoLibrary requestAuthorization,不是请求
EN

Stack Overflow用户
提问于 2016-01-29 11:28:12
回答 2查看 10.9K关注 0票数 3

为了进行测试,我试图重新创建系统“请求访问”的弹出体验。

更新:

在iOS 11下,删除应用程序后,系统弹出将再次显示。

(上一个问题)

当应用程序第一次运行时(只运行时间),系统弹出显示,请求访问。在此之后,甚至不会删除应用程序并重新启动设备,这将再次触发弹出。

换句话说,设备“记住”用户的请求,没有办法重置它。

下面是代码:

代码语言:javascript
复制
[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;
    }

}];

当access在设置中关闭时,它会继续打印"PHAuthorizationStatusDenied“。但没有弹出。马上回来。

有人建议在plist中添加“Bundle显示名称”。使用空值、$(PRODUCT_NAME)和不同的字符串尝试它没有效果。

清理项目,删除DrivedData (每次从模拟器中删除应用程序)。不走运。

更多信息:

苹果示例代码"SamplePhotosApp“一旦关闭设置中的照片访问,就会崩溃。

EN

回答 2

Stack Overflow用户

发布于 2016-01-29 12:13:07

经过进一步的阅读,这似乎是故意的。

苹果

此方法总是立即返回。如果用户以前拥有授予或拒绝照片库访问权限,则在调用时执行处理程序块;否则,它将显示警报,并仅在用户响应警报后才执行该块。

如果用户是提示符一次,则说‘此方法总是立即返回’。在此之后,它将不再显示请求。似乎没有办法(只是一些自定义消息)再次使用系统消息。

票数 5
EN

Stack Overflow用户

发布于 2018-02-08 08:31:08

重要的是将其添加到应用程序Info.plist文件中。“私隐-照片图书馆使用情况说明”

我用了这样的东西:“用于访问照片库中的照片。”

此说明将显示在“请求访问警报”框中。如果你想的话可以本地化。

代码语言:javascript
复制
+ (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);
        }
}

以下是我如何使用它:

代码语言:javascript
复制
[YourClassName imageLibraryCheckAccess:self handler:^(PHAuthorizationStatus status) {
        if (status == PHAuthorizationStatusAuthorized) {
        }
}];

祝好运!

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35083401

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档