首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >目标C-如何检测用户已单击不允许访问照片按钮

目标C-如何检测用户已单击不允许访问照片按钮
EN

Stack Overflow用户
提问于 2013-02-07 10:33:00
回答 3查看 5K关注 0票数 6

我正在使用UIImagePicker向用户呈现照片,以便用户可以选择要在我的应用程序中使用的图像。

我的问题是,当用户第一次打开图片选择器时,他们会看到一个提示:“我的应用程序想要访问你的照片”,其中有两个选项,“不允许”和“确定”。

我的要求是,当用户单击“不允许”时,图像选择器将被关闭。

有没有一种方法可以检测到用户选择了不允许?

目前,它给用户留下了一个丑陋的空白模式视图。如果用户再次打开图像选择器,他们会显示很好的苹果提供的消息,上面写着“此应用程序无法访问您的照片等。”

下面是我使用图像选择器的方法:

代码语言:javascript
复制
self.imagePickerController = [[UIImagePickerController alloc] init];
self.imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentModalViewController:self.imagePickerController animated:YES];
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2013-02-07 12:28:07

明白了!

代码语言:javascript
复制
if ([ALAssetsLibrary authorizationStatus] == ALAuthorizationStatusNotDetermined) {
    ALAssetsLibrary *assetsLibrary = [[ALAssetsLibrary alloc] init];
    [assetsLibrary enumerateGroupsWithTypes:ALAssetsGroupAll usingBlock:^(ALAssetsGroup *group, BOOL *stop) {
        if (*stop) {
            // INSERT CODE TO PERFORM WHEN USER TAPS OK eg. :
            return;
        }
        *stop = TRUE;
    } failureBlock:^(NSError *error) {
        // INSERT CODE TO PERFORM WHEN USER TAPS DONT ALLOW, eg. :
        self.imagePickerController dismissViewControllerAnimated:YES completion:nil];
    }];
}
票数 13
EN

Stack Overflow用户

发布于 2013-02-07 10:38:55

使用ALAssetsLibrary authorizationStatus。有一个特定的返回值指示您的应用程序已被拒绝。

在这里搜索该方法将显示一些用于正确处理各种授权状态的示例代码。

票数 2
EN

Stack Overflow用户

发布于 2015-04-14 00:44:54

我也遇到过类似的问题。你可以看看我下面的代码,如果它可以帮助任何将来可能有类似问题的人。我有一个IBAction,它可以帮助我加载图像。我用这个方法挣扎了4-5个小时。

代码语言:javascript
复制
(IBAction)loadImages:(id)sender {
    ALAuthorizationStatus status = [ALAssetsLibrary authorizationStatus];

    if (status == AVAuthorizationStatusAuthorized || status == AVAuthorizationStatusNotDetermined) {

        ALAssetsLibrary *assetsLibrary = [[ALAssetsLibrary alloc]init];
        [assetsLibrary enumerateGroupsWithTypes:ALAssetsGroupAll usingBlock:^(ALAssetsGroup *group, BOOL *stop) {
            if (*stop) {
                self.imagePicker = [[UIImagePickerController alloc] init];
                self.imagePicker.delegate = self;
                self.imagePicker.allowsEditing = NO;
                self.imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
                imagePicker.mediaTypes =[UIImagePickerController availableMediaTypesForSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
                [self presentViewController:imagePicker animated:YES completion:^{
                    //TODO
                }];
                return;
            }
            *stop = TRUE;
        } failureBlock:^(NSError *error) {
            [imagePicker dismissViewControllerAnimated:YES completion:nil];
        }];
    }

    if (status == AVAuthorizationStatusDenied || status == AVAuthorizationStatusRestricted) {
        UIAlertView *cameraAlert = [[UIAlertView alloc]initWithTitle:NSLocalizedString(@"Camera Access Required", nil) message:NSLocalizedString(@"Please allow us to access your camera roll in your device Settings.", nil) delegate:self cancelButtonTitle:@"Okay" otherButtonTitles:nil, nil];
        [cameraAlert show];
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14742578

复制
相关文章

相似问题

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