When to call PHPhotoLibrary.register in iOS apps

When to call PHPhotoLibrary.register in iOS apps

Avoid issues introduced by Apple's PhotoKit behavior change in iOS 15.2


2 min read

Apple's PhotoKit framework allows app developers like you to access the photo and video assets managed by the Photos app.


Using PhotoKit, you can fetch and cache assets for display and playback, edit image and video content, or manage collections of assets such as albums, Moments, and Shared Albums.

PHPhotoLibrary is the main API that allows you to

  • retrieve or verify the user’s permission for your app to access Photos content
  • make changes to assets and collections
  • register for update messages the system sends when the library changes

The end-user has to grant permission to the app to access the photo library. This can be requested with static function requestAuthorization(for:handler:).

You can observe changes in the photo library and receive messages by registering an observer with the instance function register(_:).

Does it matter when to register for changes? πŸ€”

It does since iOS 15.2 😊

In December 2021 an issue was observed when

  • using Xcode 13.2 and building on iOS 15.2 devices and
  • installing the app for the 1st time on a device or simulator.

For more information about the reported issue, please check Apple's Developer Forum can be found here.

Before iOS 15.2 version there were no known issues when calling PHPhotoLibrary.shared().register(self) without authorization.

On devices running on iOS 15.2 you will get an error.

[GatekeeperXPC] Failed to open photo library Error Domain=com.apple.photos.error Code=41011 "Unauthorized access: client does not have valid TCC authorization" UserInfo={NSLocalizedDescription=Unauthorized access: client does not have valid TCC authorization}

The user needs to grant permission, and only then you shall call PHPhotoLibrary.shared().register(self).

In the following video I verify this issue and the solution proposed.

Did you find this article valuable?

Support Marco Eidinger by becoming a sponsor. Any amount is appreciated!