0

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.

The Hungry Dictator
  • 3,444
  • 5
  • 37
  • 53
user2924482
  • 8,380
  • 23
  • 89
  • 173
  • You don't manage this permission request yourself; iOS does it for you when you access the photo library. e.g. http://stackoverflow.com/questions/13572220/ask-permission-to-access-camera-roll – Paulw11 May 16 '17 at 23:56
  • @Paulw11, in the same page http://stackoverflow.com/questions/13572220/ask-permission-to-access-camera-roll/38398067#38398067 is sending users to settings – user2924482 May 17 '17 at 00:02
  • Yes, that will open your settings, but you can't put your own switch there; iOS will do it once you have asked for permissions. The code you linked to enables you to request the user to grant permissions if they have denied it (iOS will only ask the user once, after that it uses the permissions they have set) – Paulw11 May 17 '17 at 00:04

1 Answers1

1

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.
            }
        }];
    } 
Son Pham
  • 1,516
  • 1
  • 14
  • 22
  • This works great. But if the user wants to change the state from "Don't allow" to "Allow". It needs to be done thru settings. How can you link the settings to the implementation? – user2924482 May 17 '17 at 16:10