您可以要求您的用户打开权限,如果他们说“是的,我想打开此权限”,那么您可以使用 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];
}
}];
}
}