6

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

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

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

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

目前,它使用户处于丑陋的空白模式视图中。如果用户再次打开图像选择器,他们会显示漂亮的苹果提供的消息,上面写着“这个应用程序无权访问您的照片等”

这是我使用图像选择器的方式:

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

3 回答 3

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];
    }];
}
于 2013-02-07T04:28:07.040 回答
2

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

在此处对该方法进行搜索将显示一些示例代码,用于正确处理各种授权状态。

于 2013-02-07T02:38:55.033 回答
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];
    }
}
于 2015-04-13T16:44:54.163 回答