Reputation: 10788
I am creating a button to let the user choose an image from their photo library, and I would like to hide this button if there are no images in the user’s photo library.
BOOL stillImagesAvailable = [[UIImagePickerController availableMediaTypesForSourceType:UIImagePickerControllerSourceTypePhotoLibrary] containsObject:(NSString *)kUTTypeImage];
if (!stillImagesAvailable) {
// Hide button
return;
}
stillImagesAvailable = [UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary];
if (!stillImagesAvailable) {
// Hide button
return;
}
// Show button
+[UIImagePickerController isSourceTypeAvailable:]
is documented to return NO
if there are no photos in the library, but I'm seeing it return YES
in this case on iOS 11 running on the simulator. Am I using this method wrong, or is the documentation incorrect, or am I running into a bug?
Is there another good way to detect whether there are any images in the user’s photo library?
Upvotes: 0
Views: 197
Reputation: 126137
UIImagePickerController
doesn't expose this information. That class generally doesn't provide information about the user's photo library, merely some user-selected contents thereof. (The documentation you cite appears to be incorrect — I'd recommend filing a bug against the documentation for them to change it.)
Aside: The "user-selected" part is important in iOS 11 and later: the image picker runs in a separate process, meaning your app gets access only to the picked assets, meaning you don't have to ask the user for blanket read/write access to the Photos library through privacy settings.) Keep that privacy stuff in mind for further down in this answer, though...
If you need to learn about the contents of the user's Photos library, use the Photos framework. If specifically you want to know whether the library is "empty", you'll need to define what "empty" means for your app. No assets saved in the local library through iOS? No assets synced onto the device through iTunes? What if I have no assets, but I do have some empty albums?
Assuming one possible answer to those questions (no local or synced assets, don't care about albums), here's some (untested) code that should get your answer:
- (BOOL)isPhotoLibraryEmpty {
PHFetchOptions *options = [PHFetchOptions new];
options.includeAssetSourceTypes = PHAssetSourceTypeUserLibrary | PHAssetSourceTypeiTunesSynced;
PHFetchResult *results = [PHAsset fetchAssetsWithOptions:options];
return results.count == 0;
}
However, if this would be your app's only use of the Photos framework, it might be wiser to think about whether it's worthwhile to preemptively check for an empty library. If you use the Photos framework at all, your app needs blanket read/write access to the Photos library through the iOS privacy settings (that is, you provide a NSPhotoLibraryUsageDescription
in your info.plist, and iOS prompts the user for permission the first time you call any Photos API).
For example, if all you're using the Photos framework for is to check for an empty library so you know whether to disable a "pick a photo" button in your UI... getting that deep into the privacy/permissions system probably isn't worth it. (Now you're actively interrupting them with a privacy prompt instead of passively disabling a button.) It's probably better to just let the user do whatever your UI does for invoking the image picker, and let UIImagePickerController
show an appropriate screen if the library is empty (which it does).
Upvotes: 1