LockedCameraCaptureExtension and Sharing User Preferences

I have the main app that saves preferences to UserDefaults.standard. So I have this one preference that the user is able to toggle - isRawOn

UserDefaults.standard.set(self.isRawOn, forKey: "isRawOn")

Now, I have LockedCameraCaptureExtension which is required know if that above setting on or off during launch. Also if it's toggled within the extension, the main app should know about it on the next launch.

The main app and the extension runs on separate containers and the preferences are not shared due to privacy reasons.

Apple mentions of using appContext of CameraCaptureIntent, but not sure how above scenario is possible through that....unless I am missing something.

Apple Reference

What I have for CameraCaptureIntent:

@available(iOS 18, *)
struct LaunchMyAppControlIntent: CameraCaptureIntent {
    
    typealias AppContext = MyAppContext
    static let title: LocalizedStringResource = "LaunchMyAppControlIntent"
    static let description = IntentDescription("Capture photos with MyApp.")
    
    @MainActor
    func perform() async throws -> some IntentResult {
        .result()
    }
}

The recommended way to share context between the capture experience in your app and your extension is using the AppContext to synchronize that information. The AppContext is a type of your choice, up to a certain size, persisted across both.

In your example MyAppContext can be a custom type that allows encoding the context you wish to share between sessions of the capture experience.

For more info on the AppContext, check out: https://developer.apple.com/documentation/AppIntents/CameraCaptureIntent

Its also discussed in the documentation here: https://developer.apple.com/documentation/lockedcameracapture/creating-a-camera-experience-for-the-lock-screen#Launch-your-app-extension

You can also find some material about AppContext in this WWDC 2024 video: https://developer.apple.com/videos/play/wwdc2024/10204

Let us know if you have any questions about it! 😊

LockedCameraCaptureExtension and Sharing User Preferences
 
 
Q