Search code examples
iosobjective-ciphoneuiimagepickercontrollerxcode8

iOS: how to link iPhone settings to PHPhotoLibrary permission


I'm trying to implement an app accessing the camera roll. Here is how I'm checking for permissions:

-(void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:YES];
    PHAuthorizationStatus status = [PHPhotoLibrary authorizationStatus];
    if (status != PHAuthorizationStatusAuthorized) {
        NSString *accessDescription = [[NSBundle mainBundle]
                                       objectForInfoDictionaryKey:@"NSPhotoLibraryUsageDescription"];
        UIAlertController * alertController = [UIAlertController alertControllerWithTitle:accessDescription
                                                                                  message:@"To give permissions tap on 'Change Settings' button"
                                                                           preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"Cancel"
                                                               style:UIAlertActionStyleCancel
                                                             handler:nil];
        [alertController addAction:cancelAction];
        UIAlertAction *settingsAction = [UIAlertAction
                                         actionWithTitle:@"Change Settings"
                                         style:UIAlertActionStyleDefault
                                         handler:^(UIAlertAction * _Nonnull action) {
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]
                                               options:@{}
                                     completionHandler:nil];

        }];
        [alertController addAction:settingsAction];
        [[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:alertController
                                                                                     animated:YES
                                                                                   completion:nil];

    }
}

I also add it Settings.bundle:

enter image description here

But I haven't figure it out how can link the toggle switch to the permissions. Any of you knows how can I link the toggle switch to the camera roll permissions ?

I'll really appreciate your help.


Solution

  • You can try this code :

    if (status == PHAuthorizationStatusNotDetermined) {
            // Access has not been determined.
            [PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) {
                if (status == PHAuthorizationStatusAuthorized) {
                    // do something
                }else {
                    // Access has been denied.
                }
            }];
        }