What’s New With Privacy?

Learn about the new privacy features introduced in iOS 14.5, including accessing the new privacy-focused image picker, handling location permissions and more. By Renan Benatti Dias.

Leave a rating/review
Download materials
Save for later
Share

Since the introduction of the first iPhone, the capabilities of smartphones have grown rapidly. The cellphone is no longer a tool for just calling or texting. Apps can now provide directions to your favorite destinations, access contacts to create meetings, provide professional photo editing tools and much more. The smartphone is a whole computer in your pocket.

But along with more capabilities, privacy concerns have arrived. How can users trust that apps are not maliciously using their data? Apple prides itself on being a privacy-focused company, creating and building features to better improve user privacy.

In this tutorial, you’ll learn about:

  • The new Photos library and how it’s safer and privacy-focused.
  • How to handle location permissions.
  • iOS 14’s new Clipboard prompts.
  • The new camera and microphone indicators.

Additionally, you’ll learn about iOS 14.5’s App Tracking Transparency feature and how to ask permission to track.

Getting Started

Download the project materials by clicking the Download Materials button at the top or bottom of the tutorial. Open YourPrivacy.xcodeproj in the starter folder. Build and run.

An empty view with a placeholder text "Add a picture by tapping the photo icon button" and a photo icon navigation button in the top right corner.

YourPrivacy is a simple app to add a caption and location to an image. The app stores those images and information in a list inside the app the user can later open.

Currently, the app is empty and tells the user to tap the button at the top of the screen to add a picture. Tapping it takes you to another screen with a button to select a picture, a field to add a caption and a location section. Currently, the location section toggle is disabled and tapping the Select Image button opens a blank screen with a placeholder text. You’ll add these functionalities during this tutorial.

Understanding Library Permissions and the New Restricted API

Before iOS 14, users could pick pictures from their Photos library using UIImagePickerController. This view controller has been a part of iOS since iOS 2, being the default way of fetching pictures. You also could do this using PHCollectionList, which allows you to fetch images from the Photos library and use them in your app.

But users could only allow or deny full access to their Photos library, even when they wanted to work with only a single picture or album, giving access to apps to do whatever they wanted with the user’s pictures.

With iOS 14, Apple introduced new ways of accessing photos that solved this problem: PHPickerViewController and presentLimitedLibraryPicker(from:). These APIs are safer and more privacy-focused, giving access only to pictures the user allows.

Working with Limited Libraries

iOS 14 introduced the concept of limited libraries. Instead of giving full access to the user’s library, iOS creates an intermediary library for each app, with only the pictures the user picks. That way, an app has access only to pictures and albums the user chooses, not the full library.

Users can choose to share a whole album or just some pictures with each app. With the new presentLimitedLibraryPicker(from:), users select which pictures are available to the app, and when they later change this selection, the app gets a notification to update the UI. Under Settings ▸ Privacy ▸ Photos, users can modify the level of Photos library access, as well as the selected photos available to each app.

iOS acts as an intermediary between your app and what the user wants to share.

Designing for Limited Libraries

When designing your app, it’s important to determine what kind of access your app requires. YourPrivacy saves only one picture at a time. Instead of requesting access to the entire library, letting the user pick one picture is simpler and easier.

The new PHPickerViewController works great for use cases like this. It’s built with privacy features that provide only the images the user wants to share. You’ll use this to fetch a photo from the user’s library and save it.

Adding the New PHPickerViewController

PHPickerViewController is a UIKit view controller. To use it with SwiftUI, you’ll have to create a UIViewControllerRepresentable to encapsulate it.

On Xcode, create a new Swift file inside the View group and name it ImagePickerView.swift. Add the following code to the file:

import SwiftUI
import PhotosUI

// 1
struct ImagePickerView: UIViewControllerRepresentable {
  // 2
  @Binding var image: UIImage?
  @Binding var isPresented: Bool

  // 3
  func makeUIViewController(context: Context) -> some PHPickerViewController {
    var configuration = PHPickerConfiguration()
    configuration.filter = .images
    configuration.selectionLimit = 1
    let pickerView = PHPickerViewController(configuration: configuration)
    pickerView.delegate = context.coordinator
    return pickerView
  }

  // 4
  func updateUIViewController(
    _ uiViewController: UIViewControllerType, 
    context: Context
  ) { }

  // 5
  func makeCoordinator() -> Coordinator {
    Coordinator(parent: self)
  }

  // 6
  class Coordinator: PHPickerViewControllerDelegate {
    private let parent: ImagePickerView

    init(parent: ImagePickerView) {
      self.parent = parent
    }

    // 7
    func picker(
      _ picker: PHPickerViewController, 
      didFinishPicking results: [PHPickerResult]
    ) {
      parent.isPresented.toggle()
      guard
        let result = results.first,
        result.itemProvider.canLoadObject(ofClass: UIImage.self)
      else { 
        return 
      }
      result.itemProvider.loadObject(
        ofClass: UIImage.self
      ) { [weak self] image, error in
        guard error == nil else { return }
        DispatchQueue.main.async {
          self?.parent.image = image as? UIImage
        }
      }
    }
  }
}

Here’s a breakdown of the code:

  1. Declare a new UIViewControllerRepresentable to bridge PHPickerViewController with SwiftUI.
  2. Declare Binding properties to set the picked photo and to dismiss the picker view.
  3. Implement makeUIViewController(context:) to return a view controller used in SwiftUI. Further, you create a new instance of PHPickerConfiguration, which sets the filter to show only images and limit the selection to one picture. Then, you create an instance of PHPickerViewController with your configuration and set its delegate.
  4. Declare updateUIViewController(_:context:). This method is empty because you won’t update PHPickerViewController, but UIViewControllerRepresentable requires ImagePickerView to have this method.
  5. Create and returns a Coordinator as declared below.
  6. Declare a Coordinator to conform to PHPickerViewControllerDelegate. This class acts as the delegate and will receive the picked photo from PHPickerViewController.
  7. Implement picker(_:didFinishPicking:) to receive the image the user picked and save it to the property.

Now, open NewPictureView.swift and replace the following code:

Text("To Do Open Image Picker View")

With:

ImagePickerView(image: $store.image, isPresented: $imagePickerIsPresented)

Here, you start using ImagePickerView as the presented content when the user taps the select image button.

Build and run. Tap the Select Image button to open the new picker view and select a picture from the library.

A grid view with pictures from the user's library.

How cool is that? PHPickerViewController follows the layout of iOS’s Photos library, so you don’t have to worry about creating a custom UI for picking photos. It already filters the content to show only pictures and has built-in albums, multi-selection and even a search bar.

Not only that, but the user didn’t have to allow access to their library. That’s because PHPickerViewController runs on a different process than your app, allowing access only to the picture the user selects. This allows users to pick images they want without giving access to their full library.