美文网首页
iOS 相册授权笔记

iOS 相册授权笔记

作者: 黑羽肃霜 | 来源:发表于2019-11-14 10:18 被阅读0次

    相册授权流程为:

    判断相册授权状态:

    • 拒绝授权
      • 可以弹出提示告知用户去设置里授权
    • 已授权
      • 继续正常流程
    • 未授权
      • 申请授权,系统会弹出授权框
        • 允许授权 —— 继续正常流程
        • 拒绝授权 —— 不作处理(下次用户再点击相册或选图时会重复上面的流程,弹出提示告知用户去授权)

    检查授权

    [PHPhotoLibrary authorizationStatus];
    
    /*
    返回的一个枚举类型
    PHAuthorizationStatusNotDetermined = 0, // User has not yet made a choice with regards to this application
    PHAuthorizationStatusRestricted,        // This application is not authorized to access photo data.
                                            // The user cannot change this application’s status, possibly due to active restrictions
                                            //   such as parental controls being in place.
    PHAuthorizationStatusDenied,            // User has explicitly denied this application access to photos data.
    PHAuthorizationStatusAuthorized         // User has authorized this application to access photos data.
    
    */
    

    申请授权, 建议在主线程里做

    - (void)requestAuthorizationWithCompletion:(void(^)(PHAuthorizationStatus status))handler {
        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
            [PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) {
                dispatch_async(dispatch_get_main_queue(), ^{
                    if (handler) {
                        handler(status);
                    }
                });
            }];
        });
    }
    

    下面是例子:

    switch ([[TZImageManager manager] checkAlbumAuthorized]) {
        case PHAuthorizationStatusAuthorized: {
            NSLog(@"%@ %@", config, itemId);
            __strong __typeof(weakSelf) strongSelf = weakSelf;
            strongSelf.itemID = itemId;
            dispatch_async(dispatch_get_main_queue(), ^{
                [strongSelf openImagePicker:config];
            });
        }
            break;
        case PHAuthorizationStatusNotDetermined:{
            [[TZImageManager manager] requestAuthorizationWithCompletion:^(PHAuthorizationStatus status) {
                if (status != PHAuthorizationStatusAuthorized) {
                    return;
                }
                
                NSLog(@"%@ %@", config, itemId);
                __strong __typeof(weakSelf) strongSelf = weakSelf;
                strongSelf.itemID = itemId;
                dispatch_async(dispatch_get_main_queue(), ^{
                    [strongSelf openImagePicker:config];
                });
            }];
        }
            break;
        default:
            [weakSelf showAuthorizedAlbumAlert];
    }
    

    相关文章

      网友评论

          本文标题:iOS 相册授权笔记

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