Replacing UIImagePickerController with PHPickerViewController
Almost every app requires photos and video access these days. Before iOS 14.0, the iOS developers used
UIImagePickerController to access the items from the user’s media library. From iOS 14.0, Apple is providing its developers a brand new way to select images or videos from their apps using
Features of PHPickerViewController
- No more permission alerts. The developer doesn’t have to specify the
Privacy - Photo Library Usage Descriptionkey in the .plist, and there won’t be any alert views asking for permission.
- Runs in a separate process. Although the PHPickerViewController might appear to be present inside the application, it runs in a separate process.
- Built-in privacy. Since PHPickerViewController runs in a separate process, the application can only access the items selected by the user via PHPickerViewControllerDelegate.
- Built-in search. Searching photos is a built-in feature in PHPickerViewController, and the UI is similar to that of the Photos app.
- Selection limit. PHPickerViewController also allows the developer to limit the number of photos a user can select by setting the selectionLimit property in PHPickerConfiguration.
Implementing PHPickerViewController in an application
PHPickerViewController is a breeze. The APIs are pretty straightforward.
- Create an instance of PHPickerConfiguration and pass it to the
PHPickerViewControllerinstance while creating it.
- It is the developer’s responsibility to present or dismiss the
Customizing the PHPickerViewController using the PHPickerConfiguration
The PHPickerConfiguration provides us three properties using which we can customize the PHPickerViewController.
selectionLimit- The selectionLimit property specifies the number of items a user can select. The value
0specifies unlimited selection. The below configuration limits the selection to 4 items.
filter- The filter property restricts the type of items that can be displayed. Setting the value to
nilwill display all the supported items. The below configuration displays only the live photos and images.
preferredAssetRepresentationMode- The preferredAssetRepresentationMode determines how an item provider should represent an asset. The default value is
automatic. The below configuration sets the asset’s representation mode to automatic.
Accessing items from PHPickerViewController using the PHPickerViewControllerDelegate
The PHPickerViewControllerDelegate acts differently when compared to UIImagePickerControllerDelegate. PHPickerViewControllerDelegate provides us an NSItemProvider using which objects can be loaded.
If you look closely, the API will be similar to UITableView’s or UICollectionView’s drag and drop APIs.
PHPickerViewController is supported only from iOS 14.0. If your app has a deployment target of iOS 13.0 and less, you still have to use UIImagePickerController.
Recently, FeedSpot added Ohmyswift to its “Top 30 Swift Programming Blogs & News Websites To Follow in 2020.” It is an honor to us, and it means a lot to us. Do check out their listings at
About the author
- Rizwan Ahmed - iOS Engineer. Twitter - https://twitter.com/rizwanasifahmed
Like our articles? Support us!