in Watch OS 2, iPhone is no longer execute the watch app and watchKit Extension. Instead the watch app runs on the watch
4 roles :
WKInterfaceController
WKInterfaceController
WKUserNotificationInterfaceController
CLKComplicationDataSource
WKInterfaceController
handles:
Related Session : Creating Complication With ClockKit
someImage.setImageNamed("image")
is not available in watch OS 2
use following to set images
let image = UIImage(named: "image")
someImage.setImage(image)
Similar to iOS app local storage structure, document directory and caches directory are available for storing data.
Beware caches directory can be wiped out by system for reclaiming spaces. Document directory is purgeable but it will not restored from backup.
guard let documentDir = fileManager.URLsForDirectory(.DocumentDirectory,
inDomains: .UserDomainMask).first else { return }
Application can play media files and records audio to a file.
Extension download media files and reads recorded audio files.
Must use a shared container
Enable app groups for extension and application
use following to access the app group
let fileManager = NSFileManager.defaultManager()
let container = fileManager.containerURLForSecurityApplicationGroupIdentifier("group.myapp")!
let fileName = name.stringByAppendingPathExtension("mp4")!
//....
Related session: Layout and Animation Techniques for WatchKit
New on WatchOS 2
Existing Project : Add watchOS Application target
New project : create iOS with WatchKit App
Related Session : Building Watch Apps
WKExtensionDelegate
similar to AppDelegate
of iOS,
App Lifecycle methods:
* called once on launch
* perform app init
* setup notification observers
* app is not active yet
```applicationDidBecomeActive
* invoke before going to background
* prepare to be inactive
* save any states
* Disable running servers, timers and tasks.
### Hand off
```handleUserActivity
WKInterfaceController
WKExtensionDelegate
in coming WatchOS 2
var rootInterfaceController: WKInterfaceController
will be available and can be called upon receiving handoff
//WKExtensionDelegate
func handleUserActivity(userInfo: [NSObject: AnyObject]) {
let rootController = WKExtension.sharedExtension.rootInterfaceController
rootController.popToRootController()
rootController.performActionsForUserActivity(userInfo)
}
`
WKExtension.sharedExtension().openSystemURL(systemURL)
to open any system URL
The notification will be routed to Apple Watch if :
Similar to AppDelegate on iOS, WKUserNotificationInterfaceController
has following methods to handle notifications :
didReceiveRemoteNotification
and didReceiveLocalNotification
use WatchConnectivity to tell iPhone counterpart to schedule local notifications.
Notification Actions
handleActionWithIdentifier(identifier: String, forRemoteNotification: [NSObject: AnyObject])
and handleActionWithIdentifier(identifier: String, forLocaNotification)
For text replies
func suggestionsForResponseToActionWithIdentifier(identifier: String, remoteNotification: [String: AnyObject] ) -> [String] {
return ["option 1", "option 2"]
}
launching from a notification with inline text input
``` func handleActionWithIndentifier(identifer: String, forReomteNotifications: [NSObject: AnyOBject], withResponseIfno: [NSObject: AnyObject])
and
``` func handleActionWithWithIdentifier(identifier: String, forLocalNotification: UILocalNotification, withResponseInfo: [NSObject: AnyObject])
and check UIUserNotificationActionResponseTypedTextKey
func presentAlertC ontrollerWithTitle(title: String?, message: String?, preferredStyle: WKAlertControllerStyle, action: [WKAlertAction])
.Alert
display one button
.SideBySideButtonsAlert
for showing two buttons side by side
.ActionSheet
for two buttons in two buttons
alert also includes destructive style