美文网首页
iOS _初次访问相册弹窗授权,拒绝与允许监听

iOS _初次访问相册弹窗授权,拒绝与允许监听

作者: wahkim | 来源:发表于2017-12-08 17:47 被阅读856次

    问题

    今天遇到一个问题,就是初次访问相册的时候弹窗授权,点击允许后 界面上自定义的相册浏览器数据没有刷新。
    why?最后排查到push到该界面的时候加载完视图,并且appear的时候获取数据的数组为空,所以reload collectionView的时候界面时空的。

    当弹出授权窗口的时候,界面已经加载完并且appear了。这时候想到的自然时监听点击授权按钮的时候 重新 获取数据 并且 reload 视图。

    //判断相册授权状态
     PHAuthorizationStatus status = [PHPhotoLibrary authorizationStatus];
    
     status == PHAuthorizationStatusDenied;//用户拒绝当前应用访问相册,我们需要提醒用户打开访问开关
    
     status == PHAuthorizationStatusRestricted;//家长控制,不允许访问
    
     status == PHAuthorizationStatusNotDetermined;//用户还没有做出选择
    
     status == PHAuthorizationStatusAuthorized;//用户允许当前应用访问相册
    

    解决代码

    if ([PHPhotoLibrary authorizationStatus] == PHAuthorizationStatusAuthorized){//用户之前已经授权
                [self readSystemPhotos];
     }else if([PHPhotoLibrary authorizationStatus] == PHAuthorizationStatusDenied){//用户之前已经拒绝授权
                UIAlertController *alertC = [UIAlertController alertControllerWithTitle:@"提示" message:@"您之前拒绝了访问相册,请到手机隐私设置" preferredStyle:UIAlertControllerStyleAlert];
                UIAlertAction *sureAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                    [self dismissViewControllerAnimated:YES completion:nil];
                }];
                [alertC addAction:sureAction];
                [self presentViewController:alertC animated:YES completion:nil];
            }else{//弹窗授权时监听
                [PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) {
                    if (status == PHAuthorizationStatusAuthorized){//允许
                        [weakself readSystemPhotos];//获取数据 刷新视图
     }else{//拒绝
                        [self dismissViewControllerAnimated:YES completion:nil];
                    }
                }];
            }
    

    这时会出现一个问题,重新获取数据成功后,reload 的时候没有响应。
    这时候要把 刷新视图放到主线埕执行

    - (void)readSystemPhotos
    {
        LHQWeakSelf(self);
        if (self.photoAssetCollection) {
            self.dataArray = [[LHQAblumTool shareAblumTool] getAssetsInAssetCollection:self.photoAssetCollection ascending:YES];
        }else{
            self.dataArray = [[LHQAblumTool shareAblumTool] getAllAssetInPhotoAblumWithAscending:YES];
        }
        dispatch_async(dispatch_get_main_queue(), ^{//主线埕执行
            [weakself.photosCollection reloadData];
        });
        
    }
    

    解决。
    网上参考资料:http://www.jianshu.com/p/f9776e2e54b0 (这里面的排版我差点看晕,不过解决了我的问题,挺好 嘿嘿嘿!!!)

    相关文章

      网友评论

          本文标题:iOS _初次访问相册弹窗授权,拒绝与允许监听

          本文链接:https://www.haomeiwen.com/subject/xyxyixtx.html