WWDC 2015

WatchKit In-depth Part 1

Architecture

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 :

  • Application WKInterfaceController
  • Glance WKInterfaceController
  • Notification WKUserNotificationInterfaceController
  • Complication CLKComplicationDataSource

WKInterfaceController handles:

  • Interface properties
  • Menu Handling ( force touch menu)
  • Controller navigation and paging
  • Controller model presentation
  • Alert and action sheets
  • System UI - text input,video, audio

Related Session : Creating Complication With ClockKit

Resource And Data

Setting image

someImage.setImageNamed("image") is not available in watch OS 2

use following to set images

let image = UIImage(named: "image")
someImage.setImage(image)

Local data

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 }

Media

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")!
//....

Transferring data

  • NSURLSession
    • Background upload and downloads
    • extension may not running
    • downloaded files must by copied
  • WatchConnectivity
    • Watch <-> Phone communication
    • Share data
    • Transfer files
    • Talk to each side
    • Related Session:Introducing Watch Connectivity

Migration

  • WatchOS 1
    • iOS SDK and platform
    • Runs on iPhone
    • Share framework with iOS
    • image caching
    • openParentApplication()
  • WatchOS 2
    • dedicated OS and some specific frameworks
    • Runs on Watch
    • more options on caching
    • Watch Connectivity for two way communication
    • Independent Operation
    • more layout and animations available
    • digital crown

Related session: Layout and Animation Techniques for WatchKit

New on WatchOS 2

  • Extension delegate
  • complication datasource

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 when the app visually active
  • Activate timers
  • Update any states

* invoke before going to background
* prepare to be inactive
* save any states
* Disable running servers, timers and tasks.

### Hand off

```handleUserActivity
  • OS 1 on root WKInterfaceController
  • OS 2 on 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)
}
`

Open URL

WKExtension.sharedExtension().openSystemURL(systemURL) to open any system URL

  • Phone (tel://)
  • SMS (sms://)
  • PassKit

Notification

The notification will be routed to Apple Watch if :

  • iPhone is locked
  • Apple Watch is on wrist and unlocked

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

Enhancement

Alert

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