watchkit app on watch and watchkit extension on iphone.
choosing the scene on watchkit app, WatchKit tells the paired iPhone to launch your WatchKit extension and create the objects needed to manage that scene. When the scene is fully configured, it is displayed on Apple Watch. This transfer of information between the WatchKit app and WatchKit extension happens transparently behind the scenes.
Managing Scenes: The Interface Controller
Each scene is managed by a single interface controller object, which is an instance of the
WKInterfaceController
class which is same purpose as a view controller in iOS.
WatchKit apps typically contain multiple interface controllers, with each one displaying a different type of information.
Watchkit app life cycle:-
When user launches app on watch it launches watchkit and corresponding watchkit extension.
After it loads the scene, WatchKit asks the WatchKit extension to create the corresponding interface controller object, which you use to prepare the scene for display to the user.
interface controller’s
init
and awakeWithContext:
methods to load any required data, set the values for any interface objects, and prepare your interface to be displayed. Do not use the willActivate
to initialize your interface controller. The willActivate
method is called shortly before your interface is displayed onscreen, so you should use that method only to make last-minute changes.
user interactions are handled by your interface controller’s custom action methods.
Glance interfaces do not support action methods. Tapping your app’s glance interface always launches the app.
Your WatchKit extension remains running only while the user is interacting with your app on Apple Watch. so interface controllers should be lightweight and never perform long-running tasks.
user exits your app explicitly or stops interacting with Apple watch, iOS deactivates the current interface controller and suspends your extension,
Method
|
Tasks to perform
|
---|---|
This method is your first chance to initialize your interface controller.
| |
This method lets you configure the interface controller using any available context data. Use it to load data and update labels, images, tables, and other interface objects in your storyboard scene. The context data is data you provide to assist in the configuration of the new interface controller. For example, when pushing a new interface controller in a hierarchical interface, you specify a context object that contains the next level of data to display. Providing a context object is recommended but not required.
| |
This method lets you know that your interface will soon be visible to the user. Use this method only to make small changes to your interface. For example, you might use this method to update a label based on new data. The bulk of your interface initialization should still occur in the
init and awakeWithContext: methods. | |
Use the
didDeactivate method to clean up your interface and put it into a quiescent state. For example, use this method to invalidate timers and stop animations.
You cannot set values for any interface objects from this method. From the time this method is called to the time the
willActivate method is called again, any attempts to set values for interface objects are ignored. |
Sharing Data with Your Containing iOS App
iOS app and WatchKit extension rely on the same data, use a shared app group to store that data. An app group is a secure container that multiple processes can access. Because your WatchKit extension and iOS app run in separate sandbox environments, they normally do not share files or communicate directly with one another. An app group lets the two processes share files or user defaults.
set up a shared app group from the Capabilities tab of your iOS app and WatchKit extension. Enabling the App Groups capability adds an entitlements file (as needed) to each target and adds the
com.apple.security.application-groups
entitlement to that file. To share data, both targets must have the same app group selected.
At runtime, you share files between processes by reading and writing those files in the shared container directory. To access the container directory, use the
containerURLForSecurityApplicationGroupIdentifier:
method of NSFileManager
to retrieve the base URL for the directory. Use the provided URL to enumerate the directory contents, or create new URLs for files in the directory.
To share preferences data between apps, create an
NSUserDefaults
object using the identifier of the shared group. The initWithSuiteName:
method of NSUserDefaults
creates an object that allows access to the shared user defaults data. Both processes can access this data and write changes to it.Communicating Directly with Your Containing iOS App
Apps that work closely with their containing iOS app can use the
openParentApplication:reply:
method to send requests to that app and receive a response. WatchKit extensions do not support background execution modes; they run only while the user is interacting with the corresponding app on Apple Watch. Their containing iOS app has fewer restrictions and can be configured to run in the background or to gather information on behalf of the WatchKit extension. Activities that might require extra time to complete, such as fetching a user’s location, should therefore be performed by the iOS app and communicated back to the WatchKit extension.
When you call the
openParentApplication:reply:
method, iOS launches or wakes the containing iOS app in the background and calls the application:handleWatchKitExtensionRequest:reply:
method of its app delegate. The app delegate performs the request using the provided dictionary and then returns a reply to the WatchKit extension.
0 comments:
Post a Comment