Privacy & Permissions

Privacy & Permissions

Declare usage descriptions in Info.plist and request authorization before accessing protected resources.


Request Camera Access

Request camera access in your app.

Syntax:

  • AVCaptureDevice.authorizationStatus(for:)
  • AVCaptureDevice.requestAccess(for:)

Example

Demo.swift

ContentView.swift

App.swift

import SwiftUI
import AVFoundation

struct CameraPermissionDemo: View {
  @State private var status = AVCaptureDevice.authorizationStatus(for: .video)
  var body: some View {
VStack(spacing: 12) {
  Text("Status: \(label(for: status))")
  Button("Request Access") {
    AVCaptureDevice.requestAccess(for: .video) { _ in
      DispatchQueue.main.async {
        status = AVCaptureDevice.authorizationStatus(for: .video)
      }
    }
  }
}
.padding()
} private func label(for s: AVAuthorizationStatus) -> String {
switch s { case .notDetermined: return "notDetermined"
case .denied: return "denied"
case .restricted: return "restricted"
case .authorized: return "authorized"
@unknown default: return "unknown" }
} }

The example above shows a simple camera permission request with a button to request access and a label to display the current status.


Open Settings

Open the app’s settings page from within the app.

Syntax:

  • UIApplication.openSettingsURLString
  • UIApplication.shared.canOpenURL(url)
  • UIApplication.shared.open(url)

Example

Demo.swift

ContentView.swift

App.swift

import SwiftUI
import UIKit

struct OpenSettingsDemo: View {
  var body: some View {
VStack(spacing: 12) {
  Text("Permission denied? Open Settings.")
  Button("Open Settings") {
    if let url = URL(string: UIApplication.openSettingsURLString),
       UIApplication.shared.canOpenURL(url) {
      UIApplication.shared.open(url)
    }
  }
}
.padding()
} }

The example above shows a simple settings page with a button to open the app’s settings page from within the app.


Info.plist Usage Descriptions

When accessing protected resources (camera, photos, location, microphone, etc.) you must include a usage description in Info.plist (e.g., NSCameraUsageDescription).

Syntax:

  • NSCameraUsageDescriptionNSMicrophoneUsageDescription
  • NSPhotoLibraryUsageDescriptionNSPhotoLibraryAddUsageDescription
  • NSLocationWhenInUseUsageDescriptionNSLocationAlwaysAndWhenInUseUsageDescription
  • NSContactsUsageDescriptionNSCalendarsUsageDescriptionNSRemindersUsageDescription
  • NSBluetoothAlwaysUsageDescriptionNSMotionUsageDescriptionNSFaceIDUsageDescription

Example

Info.plist (snippets)

NSCameraUsageDescription = "This app uses the camera to scan QR codes"
NSPhotoLibraryUsageDescription = "Select a profile picture"
NSLocationWhenInUseUsageDescription = "Find nearby stores"

These usage descriptions will be displayed in the app’s Settings page.


Requesting Authorization

Use the respective frameworks to request runtime permission before accessing data.

Syntax:

  • AVCaptureDevice.authorizationStatus(for:)
  • AVCaptureDevice.requestAccess(for:)

Example

Camera.swift

import AVFoundation

let status = AVCaptureDevice.authorizationStatus(for: .video)
switch status {
case .notDetermined:
  AVCaptureDevice.requestAccess(for: .video) { granted in /* handle */ }
case .denied, .restricted:
  // Show instructions to enable in Settings
  break
case .authorized:
  // Proceed to use camera
  break
@unknown default: break
}

This example checks current camera authorization, requests access if needed, and guides handling for denied or restricted states.

Tip: Always explain why you need access, and gracefully handle denial by offering alternative flows.

Handle Denied: Link to Settings

Example

Settings.swift

import UIKit

if let url = URL(string: UIApplication.openSettingsURLString),
   UIApplication.shared.canOpenURL(url) {
  UIApplication.shared.open(url)
}

Other Common Permissions

Example

Photos.swift

Location.swift

Microphone.swift

Contacts.swift

import Photos

PHPhotoLibrary.requestAuthorization { status in /* handle */ }

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *