目标C - 如何检测用户点击了“不允许访问照片”按钮

时间:2013-02-07 02:33:00

标签: iphone objective-c ipad

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

我的问题是,当用户第一次打开图像选择器时,会向他们显示一条提示:'“我的应用程序”想要使用两个选项“访问您的照片”,不允许和确定。

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

有没有办法检测用户选择了不允许?

目前它让用户处于一个丑陋的空白模态视图中。如果用户再次打开图像选择器,他们会显示提供的苹果提供的消息“此应用程序无法访问您的照片等”

这是我如何使用图像选择器:

self.imagePickerController = [[UIImagePickerController alloc] init];
self.imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentModalViewController:self.imagePickerController animated:YES];

3 个答案:

答案 0 :(得分:13)

知道了!

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

答案 1 :(得分:2)

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

在此处搜索该方法将显示一些示例代码,以便正确处理各种授权状态。

答案 2 :(得分:0)

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

(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];
    }
}
相关问题