11. WatchKit
The apple watch requires an iPhone paired with it to run the apps
without an iPhone the watch apps will not work. For creating watch app
we need two separate bundles
• WatchKit app
The watch kit app contains only storyboard and resource files
associated with our apps UI.
• WatchKit extension
The WatchKit extension contains the code for managing the WatchKit
app’s UI and for responding user interactions.
Since both of these bundle are packed in side the ios app so during the
installation of the app the system prompts the user to install the
watchkit app when a paired watch is present
18. A look at WatchKit framework
The whole apple watch is managed by the
few classes the most used are as
• WKinterfaceController
• WKUserNotificationInterfaceController
• WKInterfaceDevice
• WKInterfaceObject
19. A look at WatchKit framework
WKInterfaceController in WatchKit is the same as
UIViewController in UIKit.
WKInterfaceController is a lot more restricted and has a
different life cycle. There are different methods to work with
as following.
UIViewController WKInterfaceController
likeviewDidLoad Init
loadView awakeWithContext
ViewWillAppear WillActivate
ViewWillDisappear didDeactivate
20. A look at WatchKit framework
• WKUserNotificationInterfaceController
Its a WKInterfaceController subclass that is used to display
custom notification interfaces.
• WKInterfaceDevice
This class contains basic information related to the iWatch.
This includes screen bounds, scale and current locale. You
can also use it to manage your images cache. WatchKit
allows you to store up to 20 MB of cached images on the
iWatch.
21. A look at WatchKit framework
• WKInterfaceObject
This is the base class for all UI controls supported by
WatchKit. As we cannot use UI views from UIKit. All we can
do is use the ones that WatchKit provides.
• WKInterfaceButton
• WKInterfaceDate
• WKInterfaceGroup
• WKInterfaceImage
• WKInterfaceLabel
• And more
22. A look at WatchKit framework
• Watchkit layout
WatchKit does not use Auto layout to position it’s element.
This is because it might have been too complex for the
slow iWatch. Instead we have a layout system that
positions elements relative to each other in a horizontal or
vertical order.
23. A look at WatchKit framework
• Glances
The apple watch is designed so that user can have a quick
access to the information from the app on iPhone. This is
achieved by “Glance Interface controller” which is non-
scrollable view with the precise info and user can launch
the app by tapping in it or can close it.
Apple prohibits using any interfaceObject on this screen
because “Glances do not support interactivity—tapping
on a glance automatically launches your WatchKit app”
24. A look at WatchKit framework
• Static notification interfaces
It is a simplified version of notification appearance,
it contains static image and text. Only notification
text can be changed.
• Dynamic notification interfaces
It allow you to specify a custom controller to
display notification. Dynamic notification are not
interactive and should be designed to display the
info, tapping on it will launch the application
25. Sharing data with containing iOS
app
This can be done with by different methods which are as
following
• By NSUserDefault
we can create and init the object of user default by initiating
with initwithSuiteName
• By NSFilemanager
we can create a shared container using group id
[[NSFileManager defaultManager]
containerURLForSecurityApplicationGroupIdentifier:@"gr
oup.com.swenggco.TodayExtensionApp"];
26. Sharing data with containing iOS
app
• By openParentApplication:reply: method
we can communicate directly with containing app by calling
the openParentApplication:reply: method, which iOS
launches or wakes the containing iOS app in the
background and calls
the application:handleWatchKitExtensionRequest:reply: m
ethod of its app delegate. The app delegate performs the
request using the provided dictionary and then returns a
reply to the WatchKit extension.
27. Creating watch app
• First we need to add watch app target to the iOS project.
To do this, select your project in the explorer and click the +
button at the bottom of the project.
31. Now we have two new folders in
project directory
32. Setup an app group
App Groups are the scheme iOS uses to allow different
apps to share data. If the apps have the right entitlements
and proper provisioning, they can access a shared
directory outside of their normal iOS sandbox.
To create groups we have to turn on “app groups”
entitlement in Xcode for app and also for the extension.
When you flip that switch, Xcode will talk to the developer
center to configure your app ID for app groups. Then you
can select a group or create a new one.
35. Share a classes or framework
• We can also use a class a framework in watch extension
from the main project by selecting extension from “Target
membership” in “file inspector”.