WWDC 20
Handle the Limited Photos Library in your app
Current full photo library access: your app has full access to all contents of user’s photo library, can read and write to the photo database
Users usually have thousands or tens of thousands of photos in their library, possibly from many years, but they almost never want an app to have access to all of them (unless this is an app used for photo library management)
New limited mode: you only have access to “user’s limited library selection” – a kind of filter for the photo library
You can only fetch assets and resources related to what the user has chosen to give you access to
When the user updates the selection, your app is notified so you can update your UI
This affects all apps using photo library, even apps that are already live
When the user is asked for access to photos, they can choose:
- Allow access to all
- Don’t allow
- Select photos…
If they choose "Select photos", they're presented with a picker UI in which they can choose the set of photos that your app will be able to see
The selected photo set can be changed in the Settings later
If an app hasn’t made any changes to support this new mode yet, the user will be asked if they want to change selection every time the app accesses the photo library after a restart
Other ways to access the library
How can you use the photo library without requesting access to it at all?
Photo picker:
If your app only needs to let you choose a photo for an avatar, or to post to a chat ⭢ use system photo picker (PHPickerViewController
)
- replacement for
UIImagePickerController
- improved with search and multi-select
- doesn’t require user to grant photo library access
See more in Meet the new Photos Picker
Add-only access:
If you only need to save photos and images to the library, you can ask for add-only access
This is useful for e.g. saving an image from a message or post to the phone, or for simple camera apps
See more in What's New in Photos APIs (2017)
Full access makes sense for apps like photo browsing and editing apps, camera or backup apps
New APIs
Querying for authorization status:
The PHAuthorizationStatus
enum that tells you if the app is authorized to use the photo library has a new possible option: .limited
There is also now a second "dimension" to the authorization, represented by a new enum PHAccessLevel
, which is either .readWrite
or .addOnly
When asking for and checking authorization, you need to specify if you want to check or request read-write access or add-only access by passing a PHAccessLevel
parameter
The limited library access only affects read-write access
So here's how you detect if the user has granted you limited access to the library:
let accessLevel: PHAccessLevel = .readWrite let authorizationStatus = PHPhotoLibrary.authorizationStatus(for: accessLevel) switch authorizationStatus { case .limited: // perform UI set up for limited access ... }
Requesting authorization:
When requesting access, it's recommended that you just send a request to the photo library when the user performs an action that requires it, and the popup will appear automatically
This way, it's more clear to the user why exactly the app is requesting access to photos
If you want to request access explicitly without making a request, do:
PHPhotoLibrary.requestAuthorization(for: accessLevel) { status in switch status { ... } }
Note that the popup will only appear if the authorization status isn't determined yet, i.e. if your app hasn't requested it before
Older authorizationStatus()
and requestAuthorization(_ handler:)
APIs will be deprecated and will just return .authorized
even if the access is limited
Other APIs should work the same regardless of access, they will just return fewer items if access is limited
A few exceptions:
- assets created by your app are automatically accessible to your app
- you can’t create or fetch user’s albums in limited mode
- no access to cloud shared assets or albums
UIs to update the limited library selection:
If your app has limited access to the photo library, it's recommended that you add a piece of UI that will let the user update the selection easily without having to go look for it in the Settings app
Here's how you do it:
- add some kind of button that the user can tap to access the selection screen
- when the button is pressed, call
PHPhotoLibrary.shared().presentLimitedLibraryPicker(from: viewController)
- monitor changes through
PHPhotoLibraryChangeObserver
The selection alert will also appear automatically after each relaunch of your app when the app tries to access a limited photo library
To prevent this automatic popup, add the key PHPhotoLibraryPreventAutomaticLimitedAccessAlert
to Info.plist
("Prevent limited photos access alert" in Xcode)
Steps to take when updating
- 1. Reconsider if your app needs photo library access at all ⭢ consider using system picker instead, or asking for write-only access
- 2. Adopt the new authorization APIs
- 3. Add a button which lets the user access the library selection UI if it makes sense
- 4. Add the
Info.plist
key to prevent the alert once you make the necessary UI changes