iOS路上

项目收尾疑难问题解决一:打开相册不提示用户权限

2017-10-31  本文已影响13人  海0_0滨

一:打开相册不提示用户权限

问题描述:iOS11已经在plist文件中写了相关权限设置,但是在使用UIImagePickerController打开相册的时候却不提示用户选择权限,有以下几条情况:

  1. UIImagePickerController同样的设置使用相机会有权限选择提示,设置中也没有关于相册的设置;
  2. 项目中有用到TZImagePickerController的第三方库多选照片,他在进入图库的时候就会有权限提示,设置中关于相册的权限是:读与写;
  3. 将图片保存进手机相册会有权限提醒,设置中关于相册的权限是:写入;

带着以上疑问我看了TZImagePickerController的源码,发现他其实是在发现PHAuthorizationStatus为用户未作出明确选择的情况下自己主动请求了一次权限设置,代码如下:

/// Return YES if Authorized 返回YES如果得到了授权
- (BOOL)authorizationStatusAuthorized {
    NSInteger status = [self.class authorizationStatus];
    if (status == 0) {
        /**
         * 当某些情况下AuthorizationStatus == AuthorizationStatusNotDetermined时,无法弹出系统首次使用的授权alertView,系统应用设置里亦没有相册的设置,此时将无法使用,故作以下操作,弹出系统首次使用的授权alertView
         */
        [self requestAuthorizationWithCompletion:nil];
    }
    
    return status == 3;
}

+ (NSInteger)authorizationStatus {
    if (iOS8Later) {
        return [PHPhotoLibrary authorizationStatus];
    } else {
        return [ALAssetsLibrary authorizationStatus];
    }
    return NO;
}

- (void)requestAuthorizationWithCompletion:(void (^)())completion {
    void (^callCompletionBlock)() = ^(){
        dispatch_async(dispatch_get_main_queue(), ^{
            if (completion) {
                completion();
            }
        });
    };
    
    if (iOS8Later) {
        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
            [PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) {
                callCompletionBlock();
            }];
        });
    } else {
        [self.assetLibrary enumerateGroupsWithTypes:ALAssetsGroupAll usingBlock:^(ALAssetsGroup *group, BOOL *stop) {
            callCompletionBlock();
        } failureBlock:^(NSError *error) {
            callCompletionBlock();
        }];
    }
}

注:这里主动请求代码权限获得允许后不会跟系统的提示一样直接跳到相册,需要自己通过判断确定,TZImagePickerController的处理办法如下,当然也可以自己在权限设置的block里面设置

//下判断权限,如果没有权限就会先展示一个自定义的页面,然后开始定时器来判断是否已经获取权限,如果获取到权限则跳转到选择图片页面
if (![[TZImageManager manager] authorizationStatusAuthorized]) {
            _tipLabel = [[UILabel alloc] init];
            _tipLabel.frame = CGRectMake(8, 120, self.view.tz_width - 16, 60);
            _tipLabel.textAlignment = NSTextAlignmentCenter;
            _tipLabel.numberOfLines = 0;
            _tipLabel.font = [UIFont systemFontOfSize:16];
            _tipLabel.textColor = [UIColor blackColor];
            NSDictionary *infoDict = [NSBundle mainBundle].localizedInfoDictionary;
            if (!infoDict) {
                infoDict = [NSBundle mainBundle].infoDictionary;
            }
            NSString *appName = [infoDict valueForKey:@"CFBundleDisplayName"];
            if (!appName) appName = [infoDict valueForKey:@"CFBundleName"];
            NSString *tipText = [NSString stringWithFormat:[NSBundle tz_localizedStringForKey:@"Allow %@ to access your album in \"Settings -> Privacy -> Photos\""],appName];
            _tipLabel.text = tipText;
            [self.view addSubview:_tipLabel];
            
            if (iOS8Later) {
                _settingBtn = [UIButton buttonWithType:UIButtonTypeSystem];
                [_settingBtn setTitle:self.settingBtnTitleStr forState:UIControlStateNormal];
                _settingBtn.frame = CGRectMake(0, 180, self.view.tz_width, 44);
                _settingBtn.titleLabel.font = [UIFont systemFontOfSize:18];
                [_settingBtn addTarget:self action:@selector(settingBtnClick) forControlEvents:UIControlEventTouchUpInside];
                [self.view addSubview:_settingBtn];
            }
            //开启定时器
            _timer = [NSTimer scheduledTimerWithTimeInterval:0.2 target:self selector:@selector(observeAuthrizationStatusChange) userInfo:nil repeats:YES];
        } else {
            [self pushPhotoPickerVc];
        }

- (void)observeAuthrizationStatusChange {
    if ([[TZImageManager manager] authorizationStatusAuthorized]) {
        [_tipLabel removeFromSuperview];
        [_settingBtn removeFromSuperview];
        [_timer invalidate];
        _timer = nil;
        [self pushPhotoPickerVc];
    }
}

最后总结:打开相册不在需要用户授权,也就是说,默认对手机相册拥有读权限,因此在这里本人也就没有做处理,检查了手机上新更新的其他软件,如果只需要访问手机相册也都没有设置。等上线的时候看一下会不会被拒再来更新,如果已经有做过实验的小伙伴谢谢告诉我一声,不胜感激。

上一篇下一篇

猜你喜欢

热点阅读