'How do I set a value on NSUserActivity from my Complication to open the correct view in my Watch app

In my ComplicationController, I'm creating my descriptor with an NSUserActivity object:

class ComplicationController: NSObject, CLKComplicationDataSource {

// MARK: - Complication Configuration    
let data = MyData.shared
var dataDict: Dictionary<AnyHashable, Any> = ["character": "nil"]

func getComplicationDescriptors(handler: @escaping ([CLKComplicationDescriptor]) -> Void) {            
    let userActivity = NSUserActivity(activityType: "com.myapp")
    userActivity.userInfo = dataDict
    let descriptors = [
        CLKComplicationDescriptor(
            identifier: "myapp.extralarge",
            displayName: "MyApp",
            supportedFamilies: [CLKComplicationFamily.extraLarge],
            userActivity: userActivity)
    ]

    // Call the handler with the currently supported complication descriptors
    handler(descriptors)
}

In my ExtensionDelegate, I can detect that the complication has been tapped and use Notification Center to send a message to my Watch App as follows:

    func handle(_ userActivity: NSUserActivity) {
    if let date = userActivity.userInfo?[CLKLaunchedTimelineEntryDateKey] as? Date {
        // Handoff from complication
        NotificationCenter.default.post(
            name: Notification.Name.complicationTapped,
            object: date
        )
    }

I can detect the timeline entry date using the CLKLaunchedTimelineEntryDateKey. How do I detect what data was being presented by the complication? I can't find where to set a key on the NSUserActivity object.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source