Objective C - How to detect user has clicked Don't Allow access to photos button

前端 未结 3 1668
一个人的身影
一个人的身影 2020-12-31 23:30

I am using a UIImagePicker to present the users photos so that the user can choose an image to be used in my app.

My problem is that on the first time a user opens t

相关标签:
3条回答
  • 2021-01-01 00:08

    I had a similar problem. You can look at my code below if it can help anyone in future who may have similar problem.I have an IBAction which helps me load the images. I struggled with this method for 4-5 hours.

    (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 讨论(0)
  • 2021-01-01 00:11

    Got it!

    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];
        }];
    }
    
    0 讨论(0)
  • 2021-01-01 00:34

    Use ALAssetsLibrary authorizationStatus. There is a specific return value that indicates your app has been denied.

    Doing a search here on that method will reveal some sample code for properly handling the various authorization states.

    0 讨论(0)
提交回复
热议问题