iOS:使用设置切换 PHPhotoLibrary 权限
Posted
技术标签:
【中文标题】iOS:使用设置切换 PHPhotoLibrary 权限【英文标题】:iOS: Switch PHPhotoLibrary permissions using settings 【发布时间】:2017-05-17 16:40:26 【问题描述】:我正在检查相机胶卷的权限:
-(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];
];
【讨论】:
以上是关于iOS:使用设置切换 PHPhotoLibrary 权限的主要内容,如果未能解决你的问题,请参考以下文章
如何从 IOS 中的 PHPhotoLibrary 获取 ref url?
如何在 iOS/Swift 2 上使用 PHPhotoLibrary 保存具有自定义文件名的照片?
“PHPhotoLibrary.requestAuthorization”不在 iOS 9 中询问权限