首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >iOS:使用设置切换PHPhotoLibrary权限

iOS:使用设置切换PHPhotoLibrary权限
EN

Stack Overflow用户
提问于 2017-05-17 16:40:26
回答 1查看 602关注 0票数 0

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

代码语言:javascript
复制
-(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.
            }
        }];
    }
}

它运行良好,但问题是,如果用户选择“不允许”,并希望切换到“允许”。

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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-05-19 14:23:36

你可以要求你的用户打开权限,如果他们说“是的,我想打开这个权限”,那么你可以使用NSURL将它们直接传输到你的应用程序的首选项中,他们也可以通过单击状态栏左边的后退按钮返回到你的应用程序。

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

代码语言:javascript
复制
NSURL *settingsUrl = [[NSURL alloc] initWithString:UIApplicationOpenSettingsURLString];
[[UIApplication sharedApplication] openURL:settingsUrl];

这里是我在iOS10 iPhone6s:中测试的完整代码

代码语言:javascript
复制
- (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];
            }
        }];
    }
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44030505

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档