WWDC Quick Look 💓 By SwiftGGTeam
Handle the Limited Photos Library in your app

Handle the Limited Photos Library in your app

Watch original video

Highlight

iOS 14 adds Limited Photos Library to PhotoKit. Users can only authorize selected photos and videos. Apps need to adapt to the limited photo library through new authorization status, access level and management UI.

Core Content

In the past, many apps only wanted users to upload an avatar, send a photo, or insert a picture into a document, but a full photo library permission request would pop up. Once the user agrees, the app sees virtually the entire photo library through PhotoKit. Personal photo libraries often have tens or even hundreds of thousands of photos and videos, which is far larger than what many functions really require.

iOS 14 adds Limited Photos Library to this permission model. Users can select “Select Photos” and then select the photos and videos that an app is allowed to access in the system interface. For the app, this selection will act like a filter on the PhotoKit API:PHAsset.fetchAssetsOnly the resources selected by the user can be returned, and the relevant metadata is limited to these resources.

This change will affect all apps that use PhotoKit, including already released apps that have not yet adopted the new API. When an unadapted app grabs resources through PhotoKit for the first time, the system will prompt once in each app life cycle to allow the user to retain or modify the current selection. After adaptation, the App can decide when to display the management interface and place the entrance in the photo browsing, importing or setting page.

Apple’s direction for the migration is clear. For one-time selection processes such as avatar uploading, posting pictures in chat, and document illustrations, the new one should be used first.PHPickerViewController, as it does not require photo gallery access. Only apps with photo libraries as the core, such as photo browsing, editing, camera, and backup, need to carefully handle the UI, authorization query, and resource changes in the limited state.

Detailed Content

Query limited authorization status

(08:36) iOS 14 givesPHAuthorizationStatusadded.limited, and addPHAccessLevel. limited only affects read and write access, so it must be passed in when querying.readWrite, and then check whether the return value is.limited

import Photos

let accessLevel: PHAccessLevel = .readWrite
let authorizationStatus = PHPhotoLibrary.authorizationStatus(for: accessLevel)

switch authorizationStatus {
case .limited:
    print("limited authorization granted")
default:
    //FIXME: Implement handling for all authorizationStatus values
    print("Not implemented")
}

Key points:

  • PHAccessLeveldistinguish.addOnlyand.readWrite, corresponding to the two types of access in photo permissions: add and read and write.
  • Example usage when you need to check limited status.readWrite, because limited library does not affect add-only access. -authorizationStatus(for:)is a new query API that is access level aware.
  • The old authorization status API will not be returned for compatibility reasons.limited; When the user gives limited photo library access, the old API will still return authorized.

Request read and write access

(09:43) Requesting authorization also adds access level parameters. The speech suggested allowing user operations to drive permission requests, such as triggering them when the user clicks to import photos, read a photo album, or save to the camera roll, so that system pop-ups can be aligned with the user’s current tasks.

import Photos

let requiredAccessLevel: PHAccessLevel = .readWrite
PHPhotoLibrary.requestAuthorization(for: requiredAccessLevel) { authorizationStatus in
    switch authorizationStatus {
    case .limited:
        print("limited authorization granted")
    default:
        //FIXME: Implement handling for all authorizationStatus
        print("Unimplemented")

    }
}

Key points:

  • requestAuthorization(for:)The system permission request will only pop up when the authorization status has not yet been determined.
  • in the callbackauthorizationStatusWill reflect the choice the user just made. -.limitedIndicates that the user has granted limited photo library access, and the app should adjust the UI to let the user know that only selected photos and videos are currently visible.
  • In limited mode, most PhotoKit APIs behave the same as full authorization, but the visible resource range is filtered by user selection.

Handle resource boundaries in limited mode

(10:57) The talk lists several intentional exceptions. Assets newly created by an app are automatically added to the app’s limited selection; user albums cannot be captured or created; Cloud Shared Assets and shared albums are also inaccessible. Products that rely on app-specific photo albums need to adjust their behavior.

Key points:

  • The ability to save new photos is still available, and newly created assets will enter the visible selection of the current app.
  • If the existing process relies on creating user albums, this design cannot continue to be used in limited mode.
  • Shared photo resources are not visible, and backup, organization, and aggregation apps must explain the missing range in the UI. -PHPhotoLibraryChangeObserverStill important because users may change visible resources in system settings, admin interface, or iCloud sync scenarios.

Actively display the management interface

(12:04) After adapting to limited mode, the App can be usedpresentLimitedLibraryPicker(from:)Actively display the system management interface. The talk suggests tying it to a contextually clear button or action, such as the admin entry to a photo browsing page.

import PhotosUI

let library = PHPhotoLibrary.shared()
let viewController = self

library.presentLimitedLibraryPicker(from: viewController)

Key points:

  • PHPhotoLibrary.shared()Get the shared photo library object. -viewControllerIt is where the management interface is displayed. -presentLimitedLibraryPicker(from:)The system’s limited photo library management UI will be opened, allowing the user to add or remove photos and videos accessible to the current app.
  • After using this entrance, you can set the Prevent Limited Photos Access Alert in Info.plist to prevent the system from automatically popping up a management prompt when the App calls the PhotoKit API for the first time.

Core Takeaways

  • Make a limited photo library status bar: Display the current limited access at the top of the photo browsing page and provide management access. Why it’s worth it: When the user selects only a few photos, a blank grid can easily be misinterpreted as an app failure to read. How to start: UsePHPhotoLibrary.authorizationStatus(for: .readWrite)examine.limited, then usepresentLimitedLibraryPicker(from:)Open the system management interface.

  • Change avatar upload to PHPicker process: Single selection functions such as avatars, covers, and chat pictures no longer require PhotoKit read and write permissions. Why it’s worth doing: The session explicitly says that these functions can be accomplished with PHPicker, without the user having to grant full photo library access. How to start: Replace the old permission request entry withPHPickerViewController, select the result and enter it into the App, then save it to your own business data.

  • Make limited mode empty state for photo editor: When the user only authorizes a few photos, the editor prompts the user to add more editable photos instead of showing a generic error. Why it’s worth doing: The PhotoKit API will return results as if those are the only resources in the photo library, and the app needs to interpret the resource scope in the UI. How to start: Search before entering the resource list.limited, provide a management selection button when the list is empty or has a small number.

  • Adjust photo album dependency function: Change “Create App-exclusive photo album” to in-app favorites, project list or export record. Why it’s worth doing: Lecture Notes User photo albums cannot be captured or created in limited mode. How to get started: Save the album structure in the app’s own data layer. PhotoKit is only responsible for reading user-authorized assets and saving newly created resources.

  • Listen for selection changes and refresh cache: After the user modifies the selection in the settings or management interface, the photo list, thumbnail cache, and edit queue should be updated simultaneously. Why it’s worth doing: session description The app will receive notifications when the user modifies their selections. How to get started: Get startedPHPhotoLibraryChangeObserver, recalculate the currently visible resource collection in the change callback.

  • Meet the new Photos picker — Direct explanationPHPickerViewController, is the recommended permission-free photo selection solution for this session.
  • Build trust through better privacy — Explains the background to these permission changes in terms of privacy transparency and user control in iOS 14.
  • Support local network privacy in your app — Also around the iOS 14 permission prompts, it is suitable to design an authorization process that is triggered by users and has clear reasons.
  • What’s new in location — Displays the accuracy and authorization changes of location permissions in iOS 14, which can help understand the UI adaptation ideas for privacy permissions.

Comments

GitHub Issues · utterances