// The one place an app adds to or asks for the photo library. Left out of an app whose plist says nothing about // photos (`COTTON_NO_PHOTOS`, set by ios-spec-gate for Days): App Store processing refuses a binary that references // the Photos API without a photo library purpose string (ITMS-90683, 22 Nov 2026). #if !COTTON_NO_PHOTOS import CottonKit import Photos /// The phone's camera roll, write-only — the ONE place a shot is added to the /// photo library, in the phone app and nowhere in the package, so a test /// bundle cannot reach it. Installed at launch into `LiveStores`; the Camera /// tool's `ShotPipeline` calls it after the shot is already in Cotton and only /// once the capability gate has said yes to adding. `LiveStoresSweepTests` /// holds the rule. /// Not compiled into an app that may not write photos (`COTTON_NO_PHOTO_WRITES`: Cotton Albums' look-only build). #if !COTTON_NO_PHOTO_WRITES struct SystemCameraRoll: CameraRoll { func save(file: URL, kind: CaptureMediaKind) async throws { try await PHPhotoLibrary.shared().performChanges { PHAssetCreationRequest.forAsset() .addResource(with: kind == .video ? .video : .photo, fileURL: file, options: nil) } } } #endif /// Photo library permission, read and asked: the whole library (`add == false`) or adding one picture. struct SystemPhotoAccess: AccessAsking { let add: Bool func status() async -> CapabilityAccess { Self.access(PHPhotoLibrary.authorizationStatus(for: add ? .addOnly : .readWrite), add: add) } func ask() async -> CapabilityAccess { Self.access(await PHPhotoLibrary.requestAuthorization(for: add ? .addOnly : .readWrite), add: add) } /// **`limited` is a YES for adding one picture** (add-only access is not filtered by a selection) and a NO for /// reading the whole library, which is what a whole-library sync needs. static func access(_ status: PHAuthorizationStatus, add: Bool) -> CapabilityAccess { switch status { case .authorized: return .authorized case .limited: return add ? .authorized : .denied case .notDetermined: return .notDetermined case .denied: return .denied case .restricted: return .restricted @unknown default: return .denied } } } #endif