user1872384
user1872384

Reputation: 7127

What's the equivalent of app delegate file for apple watch app?

I want to set the initial view controller for the watch app base on different type of notification received on the watch. But I don't know where's to set it.

This is the answer for an iOS app Multiple entry points to a storyboard

Upvotes: 2

Views: 2907

Answers (2)

Aaron Brager
Aaron Brager

Reputation: 66242

watchOS 2

WKExtensionDelegate is the equivalent of the app delegate, where you can:

respond to actionable notifications and manage Handoff transitions. You also use the delegate to respond to life-cycle events, such as the activation and deactivation of your app

In applicationDidFinishLaunching(), you can call reloadRootControllersWithNames(_:contexts:) to specify the set of pages displayed when your app launches.

You can also call reloadRootControllersWithNames(_:contexts:) at runtime to switch view controllers.

watchOS 1

There's no app delegate equivalent.

To create a custom per-notification UI, create a new Notification Interface Controller with a distinct name for the Notification Category. If you need additional customization, see Managing a Custom Long Look Interface in the docs. Specifically:

When a notification of the correct type arrives, WatchKit chooses your static or dynamic interface based on several factors. WatchKit automatically displays the static interface when a dynamic interface is not available, there is not enough power to warrant displaying the dynamic interface, or you explicitly tell WatchKit not to display the dynamic interface. In all other cases, WatchKit displays your dynamic interface. After making the choice, WatchKit loads the appropriate storyboard resources and prepares the interface as shown in Figure 16-2. The loading process for the dynamic interface is mostly the same as for your app’s other interface controllers, with the exception of processing the notification payload, which is specific to notification interface controllers.

The category name should exactly match the category field in your push notification JSON, like INVITE_CATEGORY in this example:

{
    "aps" :  {
        "alert" : "You’re invited!",
        "category" : "INVITE_CATEGORY",
    }
}

(If you're using local notifications, not push notifications, just set the category property on your UILocalNotification.)

Apple Watch will display the notification interface controller corresponding to your category.

See also Notification Essentials.

Upvotes: 5

tidbeck
tidbeck

Reputation: 2398

In WatchKit 2 there is an extension delegate.

From watchOS 2 Transition Guide

In watchOS 2, your WatchKit extension has an extension object and corresponding delegate object to manage behavior that is central to your app. The WKExtension object is a shared object that is available whenever your Watch app runs. The extension object has an associated delegate object that conforms to the WKExtensionDelegate protocol.

Upvotes: 0

Related Questions