Tuesday, July 14, 2015

WatchKit App Architecture / Apple watchkit app development started [4 ]

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.

Figure 3-1Communication between a WatchKit app and WatchKit extension

image: ../Art/app_communication_2x.png

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. 
Figure 3-2Launching a WatchKit appimage: ../Art/launch_cycle_2x.png
 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,

Figure 3-3The life cycle of an interface controllerimage: ../Art/watch_app_lifecycle_simple_2x.png

Table 3-1Key methods of WKInterfaceController
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 willActivatemethod 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 thecom.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.



Apple watchkit app development started [3 ] / Configuring Your Xcode Project for watchkit

Watchkit is extension to you iOS app.
To add watchkit to app follow below steps(1.File new target watchkit app which will add wathkitapp and  WatchKit extension to your app.
 add a new WatchKit app target, which configures the bundles and initial resources for your WatchKit app and WatchKit extension. 
Those bundles are then delivered as part of your iOS app on the App Store.):-

1. In Xcode, open the project for your iOS app.
2. Select File > New > Target, and navigate to the Apple Watch section.
3. Select WatchKit App, then click Next.
4. If you plan to implement a glance or custom notification interface, select the appropriate checkboxes.
For notification interfaces, it is recommended that you select the Include Notification Scene checkbox, even if you do not plan on implementing that interface right away. Selecting that checkbox adds an additional file to your project for debugging your notification interfaces. If you do not select that option, later you must create the file manually.
5. Click Finish.

WatchKit development requires the iOS 8.2 SDK or later. 

The bundle IDs for both new targets are configured automatically, based on the bundle ID of your iOS app. The base IDs for all three bundles must match; if you change your iOS app’s bundle ID, you must update the other bundle IDs accordingly.

Adding a WatchKit App target to your Xcode project creates two new executables and updates your project’s build dependencies. Building your iOS app builds all three executables (the iOS app, WatchKit extension, and WatchKit app) and packages them together. Xcode also creates a build scheme specifically for building and debugging only your WatchKit app.



The Build, Run, and Debug Process select watchkitapp as as target and run 
If you are using notification then follow below steps

To configure custom build schemes for glances and notifications

  1. Select your existing WatchKit app scheme.
  2. From the scheme menu, select Edit Scheme. image: ../Art/edit_scheme_menu_2x.png
  1. The Build, Run, and Debug Process select watchkitapp as as target and run 
    If you are using notification then follow below steps

    To configure custom build schemes for glances and notifications
    1. Select your existing WatchKit app scheme.
    2. From the scheme menu, select Edit Scheme.


    image: ../Art/duplicate_scheme_2x.pngClose the scheme editor to save your changes.
  2. Specifying a Notification Payload for Testing
    The payload itself is a file that you create with a .apns filename extension.
    The PushNotificationPayload.apns file contains most of the keys you need to simulate a remote notification, and you can add more keys as needed. Figure 2-2 shows the default JSON file that comes with your project.

    Fgure 2-2A simulated remote notification payloadimage: ../Art/PushNotificationPayload.apns_2x.png




WatchKit App / Glance Interfaces / Local and Remote Notifications / Apple watchkit app development started [2] /

1.WatchKit App:- ( contains only the storyboards and resource files associated with your app’s user interface.)
It exists on  the Apple Watch home screen. 
A WatchKit app acts as the public face of your app but it works in tandem with your WatchKit extension, which is the brains of the operation. 

The WatchKit extension contains the code for managing content, responding to user interactions, and updating your user interface. And because the extension runs on the user’s iPhone, it can coordinate with your iOS app as needed to perform more sophisticated tasks.

2. Glance Interfaces:-  Glance are  used to display your app’s most important information. Glances are nonscrolling; the entire glance interface must fit on a single screen.
 Glances are read-only and cannot contain buttons, switches, or other interactive controls. Tapping a glance launches your WatchKit app. To create a glance,  you  have to create a specialized set of objects inside your existing WatchKit app and WatchKit extension. 

2.Local and Remote Notifications :
Apple Watch works with its paired iPhone to display local and remote notifications.Initially, Apple Watch uses a minimal interface to display incoming notifications. When the user’s movement indicates a desire to see more information, the minimal interface changes to a more detailed interface displaying the contents of the notification.You can customize this detailed interface and add custom graphics or arrange the notification data differently from the default interface provided by the system.

Apple Watch support for the actionable notifications introduced in iOS 8. Actionable notifications are a way to add buttons to your notification interface that reflect actions the user might take.

example, a notification for a meeting invite might include buttons to accept or reject the invitation.

Apple watchkit app development started [1]

Apple Watch must have the presence of an iPhone to run third-party .

To create a third-party app, you need two separate bundles: 
1.a WatchKit app (that runs on Apple Watch)  :- contain storyboard and resources only. Code will be in WatchKit extension running on phone.
2. WatchKit extension (that runs on the user’s iPhone) :- code for managing the WatchKit app’s user interface and for responding to user interactions. 

glance:- for displaying static screens
 you can optionally offer users a read-only interface, known as a glance, which displays timely and relevant information from your app.

notification interfaces:- for local and remote notification
 you can change the way local and remote notifications are displayed to your users by providing custom notification interfaces.

WatchKit app extends the behavior of your existing iOS app, the WatchKit app and WatchKit extension are bundled together and packaged inside your iOS app bundle. During installation of your iOS app, the system prompts the user to install the WatchKit app when a paired Apple Watch is present.


About

Powered by Blogger.