WWDC 2015

WatchKit In depth part 2

Digital Crown

in the native apps, digital crown can control lists, pickers and volume

in WatchOS 2, WKInterfacePicker can be used accompanying with digital crown. There are 3 styles :

list style:

Stack style:

Sequence style:

we can also put the text on the top of the focused UI

The scroll indicator can be hidden if the context is not fit

Use Coordinating images for any interesting UI via digital crown

use picker.setItems([WKPickerItem]) to set items in the picker

use IBAction func pickerAction(selectedIndex: Int) for getting selected index

Coordinating images

layout the UI as follows :

and config with :

let progressImages = UIImage.animatedImageWithImages( [WKImage], duration: 0.0)

// WKImage can be initialized with local asset, remote with imageData: or draw with Core Image 

progressInterfaceGroup.setBackgroundImage(progressImages)

picker.setCoordinatedAnimations( [progressInterfaceGroup, ...])

once it set up , picker will assign the background image according to current selected index.

make sure picker item generates very fast as the list can scroll though many items in a short time.

Media Playback

  1. load the asset from bundle and retrieve the url
  2. setup options like WKMediaPlayerControllerOptionsAutoplayKey:, WKMediaPlayerControllerOptionsStartTimeKey:,WKMediaPlayerControllerOptionsVideoGravityKey: (aspect ratio)
  3. and present the media player controller with url and options. provide handlers for checking if the media has played to end, the finished time and error if any

WKInterfaceMovie is also provided if the player required to embed in other controllers. Movie remote url, the poster(the thumnnail image to preview) and the video gravity ( the aspect ratio)

func setupMovie() {
    //movie pointer set up via storyboard
    movie.setMovieURL(url)
    movie.setVideoGravity(.ResizeAspectFill)
    movie.setPosterImage(poster)
}

Long-form audio

  • used for Podcases and music
  • routed to blue headphones if available
  • Glance integration with Now Playing
  • similar API usage with AVFoundation
  • enable watch app background mode - audio playback
  • Since watch app and extension bundle is not the same. requires App group to share the multimedia contents. Put all meida in the shared container.

Sharing Data with the containing app

Audio Recoding

call the following to get recording

presentAudioRecordingControllerWithOutputURL(url, preset: .NarrowBandSpeech, maximumDuration:60.0 , actionTitle:"Send") { didSave, error in
    // ... 
}

and different preset for different bitrates

Security

Store the data securely with locks/unlock mode

No iCloud Keychain

let secret = "somethingSecert"
if let secret = secret.dataUsingEncoding(NSUnicodeStringEncoding) {
    let attributes: [NString: NSObject] = [
        kSecClass: kSecClassGenericPassword,
        kSecAttraccessible: kSecAttrAccessibleWhenUnlocked,
        kSecAttrSErvice: "myService",
        kSecAttrAccount:"account name",
        kSecValueData : secretData
    ]
    SecItemAdd(attributes, nil)
}