iOS: 使用设置切换 PHPhotoLibrary 权限

iOS: Switch PHPhotoLibrary permissions using settings

我正在检查相机胶卷的权限:

-(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.
            }
        }];
    }
}

它工作正常,但问题是如果用户选择 "Not Allow" 并且它想切换到 "Allow"。

用户如何切换相机胶卷的权限?

你可以要求你的用户打开权限,如果他们说 "yes, I want to turn on this permission" 那么你可以直接使用 NSURL 将它们传输到你的应用程序的设置中的首选项,他们也可以 return单击状态栏左侧的后退按钮可返回您的应用程序。

这是将用户传送到您的应用程序首选项的代码:

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];
            }
        }];
    }
}