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

余生长醉 提交于 2021-01-27 17:19:48

问题


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


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/28846415/whats-the-equivalent-of-app-delegate-file-for-apple-watch-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!