0

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

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

它工作正常,但问题是如果用户选择“不允许”并且它想要切换到“允许”。

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

4

1 回答 1

2

您可以要求您的用户打开权限,如果他们说“是的,我想打开此权限”,那么您可以使用 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];
            }
        }];
    }
}
于 2017-05-19T14:23:36.773 回答