iOS: переключение разрешений PHPhotoLibrary с помощью настроек

Я проверяю разрешения на рулон камеры:

-(void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:YES];
    PHAuthorizationStatus status = [PHPhotoLibrary authorizationStatus];
    if (status != PHAuthorizationStatusNotDetermined) {
        // Access has not been determined.
        [PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) {
            if (status == PHAuthorizationStatusAuthorized) {
                // do something
            }else {
                // Access has been denied.
            }
        }];
    }
}

И это работает нормально, но проблема в том, что пользователь выбирает "Не разрешать" и хочет переключиться на "Разрешить".

Как пользователь может переключить разрешение на рулон камеры?

1 ответ

Решение

Вы можете попросить вашего пользователя включить разрешение, если он скажет "да, я хочу включить это разрешение", то вы можете перенести их в настройки вашего приложения в Настройках напрямую, используя NSURL, и они также могут вернуться в ваше приложение с помощью нажмите кнопку назад в строке состояния слева.

Вот код для переноса пользователя в настройки вашего приложения:

NSURL *settingsUrl = [[NSURL alloc] initWithString:UIApplicationOpenSettingsURLString];
[[UIApplication sharedApplication] openURL:settingsUrl];

Вот полный код, который я тестирую в iOS10 iPhone6s:

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:YES];
    PHAuthorizationStatus status = [PHPhotoLibrary authorizationStatus];
    if (status != PHAuthorizationStatusNotDetermined) {
        // Access has not been determined.
        [PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) {
            if (status == PHAuthorizationStatusAuthorized) {
                // do something
            }else {
                // Access has been denied.
                UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Need Photo Permission"
                                                                               message:@"Using this app need photo permission, do you want to turn on it?"
                                                                        preferredStyle:UIAlertControllerStyleAlert];
                UIAlertAction *yesAction = [UIAlertAction actionWithTitle:@"YES" style:UIAlertActionStyleDefault
                                                                      handler:^(UIAlertAction * action) {
                                                                          NSURL *settingsUrl = [[NSURL alloc] initWithString:UIApplicationOpenSettingsURLString];
                                                                          [[UIApplication sharedApplication] openURL:settingsUrl];
                                                                      }];
                UIAlertAction *noAction = [UIAlertAction actionWithTitle:@"NO" style:UIAlertActionStyleCancel
                                                                 handler:^(UIAlertAction * action) {
                                                                 }];
                [alert addAction:yesAction];
                [alert addAction:noAction];
                [self presentViewController:alert animated:YES completion:nil];
            }
        }];
    }
}
Другие вопросы по тегам