iOS 相册授权笔记

2019-11-14  本文已影响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];
}
上一篇下一篇

猜你喜欢

热点阅读