Search code examples
iosswiftparenttoday-extension

How to call a method in the parent iPhone app from Today Extension in Swift?


I have just created an Apple Watch extension to my iPhone App and used the following method to update my app data.

// Call the parent application which launches a method to update the app data
WatchViewController.openParentApplication([:],
    reply: { (reply, error) -> Void in
        self.updateGui() // update the gui when done
    })

Is there anything similar for Apple Today Extensions (widgets)? It feels wrong to implement all the client-server communication again for the extension.

Or how do you suggest to update my data (stored in app group throughout iPhone app, WatchKit and Today Extension).


Solution

  • No need to write client-server communication code separate for container app and its extension.

    Apple recommends embedded frameworks for the same. You create an embedded framework which can be used across both targets. Place code that will need to be used by both the container app and extension in the framework to avoid code repetition.

    And for sharing data between container app and extension, you can use NSUserDefaults through AppGroup.

    Please check Apple documentation.