iosphassetcollectionphassetslibrary

Exclude iCloud shared albums with fetching asset collections


I'm trying to fetch a list of all the albums in the user's photo library excluding any iCloud shared albums. Here is the code I am using so far:

PHFetchOptions *userAlbumsOptions = [PHFetchOptions new];
userAlbumsOptions.predicate = [NSPredicate predicateWithFormat:@"estimatedAssetCount > 0"];
userAlbumsOptions.includeAssetSourceTypes = PHAssetSourceTypeUserLibrary;

PHFetchResult *userAlbums = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeAlbum subtype:PHAssetCollectionSubtypeSmartAlbumUserLibrary options:userAlbumsOptions];

for (PHAssetCollection *album in userAlbums) {
    [self.albums addObject:album];
}

If I have understood correctly, the userAlbumsOptions.includeAssetSourceTypes property should filter out iCloud shared albums; however, they are still showing in the list. Can anyone suggest what I might be doing wrong?


Solution

  • One possible solution:

    PHFetchOptions *userAlbumsOptions = [PHFetchOptions new];
    userAlbumsOptions.predicate = [NSPredicate predicateWithFormat:@"estimatedAssetCount > 0"];
    
    PHFetchResult *userAlbums = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeAlbum subtype:PHAssetCollectionSubtypeAny options:userAlbumsOptions];
    
    for (PHAssetCollection *album in userAlbums) {
        if (album.assetCollectionSubtype != PHAssetCollectionSubtypeAlbumCloudShared) {
            [self.albums addObject:album];
        }
    }
    

    Basically, retrieve all asset collections, but only add them to the array of albums if the subtype is not PHAssetCollectionSubtypeAlbumCloudShared. This works, but I'm not happy with it. The PHAssetCollectionSubtypeAlbumCloudShared check should not be necessary.