Chapters

Hide chapters

SwiftUI Cookbook

Live Edition · iOS 16.4 · Swift 5.8.1 · Xcode 14.3.1

Managing App Permissions
Written by Team Kodeco

When developing applications, it’s common to need access to various user data or services, such as location data, camera or the photo library. However, accessing such data often requires the user’s explicit permission. In this entry, you’ll learn how to handle permissions in your SwiftUI application, specifically for the photo library.

In SwiftUI, permissions are typically managed using Apple’s provided frameworks. For example, to access the user’s photo library, you use the Photos framework.

Here’s an example of how you might request access to the user’s photo library:

import SwiftUI
import Photos

struct ContentView: View {
  var body: some View {
    Button("Request Photo Library Permission") {
      requestPhotoLibraryAccess()
    }
  }

  private func requestPhotoLibraryAccess() {
    let status = PHPhotoLibrary.authorizationStatus()

    switch status {
    case .notDetermined:
      PHPhotoLibrary.requestAuthorization { newStatus in
        if newStatus == .authorized {
          print("Access granted.")
        } else {
          print("Access denied.")
        }
      }
    case .restricted, .denied:
      print("Access denied or restricted.")
    case .authorized:
      print("Access already granted.")
    case .limited:
      print("Access limited.")
    @unknown default:
      print("Unknown authorization status.")
    }
  }
}

Before you can request access, you’ll need to add a usage description to your app’s Info.plist file. For the photo library, this is the NSPhotoLibraryUsageDescription key, shown within Xcode as Privacy — Photo Library Usage Description. This is a string that describes why your app needs the photo library, which is then shown to the user when your app requests access. For example, “This app needs access to your photo library to share photos with your friends”. See Entry 6, “Use Custom Fonts in SwiftUI” in Section 3, “Text & Fonts in SwiftUI” for details on how to edit Info.plist.

Permissions don’t work in the preview canvas, so you’ll need to run the app on a simulator or device. Once you run and tap Request Photo Library Permission, you’ll see the following:

Permissions dialog in the iOS Simulator.
Permissions dialog in the iOS Simulator.

When the button is tapped, the requestPhotoLibraryAccess method is invoked, which checks the current authorization status for the photo library. If the status is notDetermined (the user has not yet granted or denied permission), it requests access. If access is granted, it prints “Access granted”; otherwise, it prints “Access denied”. If the status is restricted or denied, it means the user has already denied access or the device itself is restricting access. If the status is authorized, it means the user has already granted access. If the status is limited, it means the user has granted limited access to select photos.

Always remember, your app should remain functional, albeit with reduced capabilities, even if the user denies or later revokes access to the photo library. This will ensure a better user experience and respect the user’s privacy decisions.

Have a technical question? Want to report a bug? You can ask questions and report bugs to the book authors in our official book forum here.
© 2024 Kodeco Inc.