Upgrade to Pro — share decks privately, control downloads, hide ads and more …

WatchKit

 WatchKit

An overview of the first WatchKit SDK, and details on writing WathKit app extensions.

Jonathan Blocksom

December 05, 2014
Tweet

More Decks by Jonathan Blocksom

Other Decks in Programming

Transcript

  1. Agenda • WatchKit Overview • Hello, Wrist • Glances and

    Notifications • GUI Overview • Apps • “Advanced” WatchKit
  2. The SDK So Far • No native code • Glances


    Notifications
 Watch Apps • Extension Model • Limited Functionality
  3. Host App WatchKit Extension Watch iPhone Watch App Bluetooth Outlet

    setters Actions Shared Container • Extension model for app • Data, remote messages over bluetooth • No code on device • Watch App bundle contains only assets
  4. Watch App Limitations • Code is running on phone •

    Watch - Phone comms takes power • No access to Watch sensor data • No access to Phone sensor data • No background jobs
  5. Looking Forward • Apple Sept 9 2014 Press Release:
 ”With

    Apple Watch, developers can create WatchKit apps with actionable notifications and Glances that provide timely information. Starting later next year, developers will be able to create fully native apps for Apple Watch.”
 
 (emphasis added)
  6. Hackathon Crime Tracker Get out of meeting Love tap Health

    Data Heart Rate Audio Player Real Time Money
  7. Glances • Quick look at some data • Non-interactive •

    Code to prep runs ahead of display • Invoked when user swipes from bottom
 User can select which glances to display
  8. Programming Model • Implement WKInterfaceController • Use outlets to customize

    interface - (instancetype)initWithContext:(id)context; - (void)willActivate; - (void)didDeactivate; @interface GlanceController() @property (weak, nonatomic) IBOutlet WKInterfaceLabel *topLabel; @property (weak, nonatomic) IBOutlet WKInterfaceLabel *bottomLabel; @property (weak, nonatomic) IBOutlet WKInterfaceTimer *bottomTimer; @end
  9. Set up glance • Run whatever operations you need —

    query server, check shared container, etc. • Could be long time from initWithContext: to willActivate - (void)willActivate { NSLog(@"%@ glance will activate", self); self.bottomLabel.text = @"Glancing!"; self.bottomTimer.date = [NSDate date]; [self.bottomTimer start]; }
  10. Long-Look Interface • Tap sash/title to launch app • Use

    UIUserNotificationAction to register buttons and actions • Foreground Actions launch watch app, sending button ID • Background Actions launch containing iOS app
  11. GUI Elements • Group • Table • Label, Image •

    Button, Switch, Slider • Date, Timer • Map • Menu
  12. Tables • Not quite a data source func setRowTypes(rowTypes: [AnyObject])

    // row names. size of array is number of rows func setNumberOfRows(numberOfRows: Int, withRowType rowType: String) // repeating row name var numberOfRows: Int { get } func rowControllerAtIndex(index: Int) -> AnyObject? func insertRowsAtIndexes(rows: NSIndexSet, withRowType rowType: String) func removeRowsAtIndexes(rows: NSIndexSet) func scrollToRowAtIndex(index: Int)
  13. Menu • Show a menu with up to four buttons

    typedef NS_ENUM(NSInteger, WKMenuItemIcon) { WKMenuItemIconAccept, // checkmark WKMenuItemIconAdd, // '+' WKMenuItemIconBlock, // circle w/ slash WKMenuItemIconDecline, // 'x' WKMenuItemIconInfo, // 'i' WKMenuItemIconMaybe, // '?' WKMenuItemIconMore, // '...' WKMenuItemIconMute, // speaker w/ slash WKMenuItemIconPause, // pause button WKMenuItemIconPlay, // play button WKMenuItemIconRepeat, // looping arrows WKMenuItemIconResume, // circular arrow WKMenuItemIconShare, // share icon WKMenuItemIconShuffle, // swapped arrows WKMenuItemIconSpeaker, // speaker icon WKMenuItemIconTrash, // trash icon } NS_ENUM_AVAILABLE_IOS(8_2);
  14. App Model • Container/Host App
 iOS app • Watch App


    Assets for app, sent to watch by iOS • WatchKit Extension
 Code running on phone for watch app Host App WatchKit Extension Watch iPhone Watch App Bluetooth Outlet setters Actions Shared Container Container App
  15. Container App • iOS App • Includes WatchKit extension and

    Watch App • System finds Watch stuff and uses as needed Host App WatchKit Extension Watch iPhone Watch App Bluetooth Outlet setters Actions Shared Container
  16. Watch App • App bundle installed on watch • Storyboards

    • Image Assets • Other info Host App WatchKit Extension Watch iPhone Watch App Bluetooth Outlet setters Actions Shared Container
  17. WatchKit
 Extensions • Standard iOS Extension architecture • Run by

    system when Watch App is active • Can communicate with host app via shared container directory • Limited functionality Host App WatchKit Extension Watch iPhone Watch App Bluetooth Outlet setters Actions Shared Container
  18. “Advanced Tips” • Glance to App • Server side images

    • Communicating between phone and watch (app and extension) • Misc From “Getting Started With WatchKit”
  19. Launch App from Glance • Tap glance and app will

    launch • Use Handoff to set up // Use Handoff to route the wearer to the image detail controller when the Glance is tapped. NSDictionary *userInfo = @{ @"controllerName": @"mainController", @"detailInfo": @"Some information" }; [self updateUserActivity:@"com.capitalone.WatchThis.watchkitextension" userInfo:userInfo];
  20. Launch App from Glance • Tap glance and app will

    launch • Use Handoff to set up // Use Handoff to route the wearer to the image detail controller when the Glance is tapped. NSDictionary *userInfo = @{ @"controllerName": @"mainController", @"detailInfo": @"Some information" }; [self updateUserActivity:@"com.capitalone.WatchThis.watchkitextension" userInfo:userInfo]; Storyboard ID
  21. Watch App Images • JPEGs, PNGs in App • Access

    by name:
 WKInterfaceImage -setImageNamed:(NSString *)name • Animated images:
 img0.png, img1.png, img2.png • - startAnimating
 - startAnimatingWithImagesInRange:duration:repeat:
  22. Image Cache • 20MB Dynamic cache • WKInterfaceDevice -addCachedImage:name: •

    After adding, set via setImageNamed: • Automatically managed, will discard old images
  23. Dynamically Generating Images • Standard iOS dynamic image generation: •

    Then send to watch: UIGraphicsBeginImageContext(CGSizeMake(50.0, 50.0)); UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(0.0, 0.0, 50.0, 50.0)]; [[UIColor greenColor] setStroke]; path.lineWidth = 3.0; [path stroke]; UIImage *img = UIGraphicsGetImageFromCurrentImageContext(); NSData *pngData = UIImagePNGRepresentation(img); [device addCachedImageWithData:pngData name:@"circle"]; [self.image setImageNamed:@"circle"];
  24. App Groups • Requires developer account • Can set up

    automagically • Use group string in code: self.sharedDefaults = [[NSUserDefaults alloc] initWithSuiteName:@"group.com.capitalone.Watch1"];
  25. Shared Container • File system directory • Accessible by host

    and extension Entity Host App WatchKit Extension Shared Container Document Document Document User Defaults
  26. NSUserDefaults to share data • Set up using group suite:

    • Get and set objects:
 [self.sharedDefaults setObject:newMessage
 forKey:@"message"];
 NSString *msg = [self.sharedDefaults 
 objectForKey:@"message"]; self.sharedDefaults = [[NSUserDefaults alloc] initWithSuiteName:@"group.com.capitalone.Watch1"];
  27. What can I call from an extension? • Look in

    headers for NS_EXTENSION_UNAVAILABLE • Cannot:
 Access sharedApplication
 Access camera, microphone
 Long running background tasks
 HealthKit • https://developer.apple.com/library/ios/documentation/ General/Conceptual/ExtensibilityPG/ExtensionOverview.html
  28. Resources • WatchKit Reference Guide • Header Files • Apple

    Discussion Forum — FAQ
 Apple WatchKit Forum FAQ:
 https://devforums.apple.com/thread/254675 • Twitter search: WatchKit