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
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" }
}
}
import SwiftUI
struct ContentView: View { var body: some View { CameraPermissionDemo() } }
import SwiftUI
@main
struct MyApp: App { var body: some Scene { WindowGroup { ContentView() } } }
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.openSettingsURLStringUIApplication.shared.canOpenURL(url)UIApplication.shared.open(url)
Example
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()
}
}
import SwiftUI
struct ContentView: View { var body: some View { OpenSettingsDemo() } }
import SwiftUI
@main
struct MyApp: App { var body: some Scene { WindowGroup { ContentView() } } }
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:
NSCameraUsageDescription,NSMicrophoneUsageDescriptionNSPhotoLibraryUsageDescription,NSPhotoLibraryAddUsageDescriptionNSLocationWhenInUseUsageDescription,NSLocationAlwaysAndWhenInUseUsageDescriptionNSContactsUsageDescription,NSCalendarsUsageDescription,NSRemindersUsageDescriptionNSBluetoothAlwaysUsageDescription,NSMotionUsageDescription,NSFaceIDUsageDescription
Example
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
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
import UIKit
if let url = URL(string: UIApplication.openSettingsURLString),
UIApplication.shared.canOpenURL(url) {
UIApplication.shared.open(url)
}
Other Common Permissions
Example
import Photos
PHPhotoLibrary.requestAuthorization { status in /* handle */ }
import CoreLocation
class L: NSObject, CLLocationManagerDelegate {
let manager = CLLocationManager()
func ask() {
manager.requestWhenInUseAuthorization()
}
}
import AVFoundation
AVAudioSession.sharedInstance().requestRecordPermission { granted in /* handle */ }
import Contacts
let store = CNContactStore()
store.requestAccess(for: .contacts) { granted, error in /* handle */ }
Testing tips: In Simulator, reset privacy permissions (Device → Erase All Content and Settings) or use test devices. Ensure your flow handles every status: .notDetermined, .denied, .authorized, and framework-specific cases.