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

WatchKit

 WatchKit

Developing apps for WatchKit for Swift belling talk

Konstantin

January 26, 2015
Tweet

More Decks by Konstantin

Other Decks in Programming

Transcript

  1. Me - ¯\_(ツ)_/¯ Kostiantyn Koval Work:  + + I

    ❤ : Kostiantyn Koval @KostiaKoval
  2. Watch & Apps ❖ Watch is worn not used ❖

    Quick Interaction ❖ New use scenarios
  3. Watch & Apps ❖ Watch is worn not used ❖

    Quick Interaction ❖ New use scenarios Small
  4. 1. Create WatchKit App iOS Project WatchKit
 Project ❖ An

    Extension to iOS app ❖ Most important feature
  5. 1. Create WatchKit App iOS Project WatchKit
 Project ❖ An

    Extension to iOS app ❖ Most important feature ❖ Require iOS app and iPhone
  6. 2. WatchKit Target Content WatchKit App WatchKit Extension ❖ UI

    - Storyboard and Images ❖ Lives on  Watch ❖ Static UI ❖ Code - Swift and Objc ❖ Lives on iPhone ❖ Extension Limitations
  7. UI - Watch App ❖ Interface Builder (Drag & Drop)

    ❖ No X, Y, Z coordinate system (0,0) Y X
  8. UI - Watch App ❖ Interface Builder (Drag & Drop)

    ❖ No X, Y, Z coordinate system ❖ Stack layout
  9. UI - Watch App ❖ Interface Builder (Drag & Drop)

    ❖ No X, Y, Z coordinate system ❖ Stack layout ❖ Static Interface
  10. Elements ❖ Layout ❖ Group, Table, Separator ❖ Controls ❖

    Button, Switch, Slider ❖ Content ❖ Image, Map, Label ❖ Date and Time ❖ Menus
  11. Element Attributes ❖ Element Specific (Button) ❖ View ❖ Alpha,

    Hidden ❖ Position ❖ Horizontal, Vertical ❖ Size ❖ Width , Height
  12. Size Fit Content Relative to Container Fixed ❖ Percent: 0,3

    ❖ Adjustment : 2 or -2 ❖ Point : 
 38mm 134 - 151
 42mm 155 - 175
  13. Layout Order 1. By Position rule 2. By Order Top

    Center Bottom Keep elements order the same as Layout
  14. Code - WatchKit Extension ❖ Delegate hard work to parent

    iPhone app, iPhone app is a brain ❖ Cocoa and Foundation
  15. Code - WatchKit Extension ❖ Delegate hard work to parent

    iPhone app, iPhone app is a brain ❖ Cocoa and Foundation ❖ Extension Limitation: ❖ No background tasks ❖ Avoid long running tasks ❖ No UIApplication
  16. Controlling UI ❖ Create UI in Storyboard ❖ Create WKInterfaceController

    subclass ❖ Set Custom class in Storyboard ❖ Set @IBOutlet and @IBAction
  17. UI - Code WatchKit App WatchKit Extension Proxy Actions Set

    values Actions Set values ❖ Run loop batch update ❖ No animation
  18. Code … class ButtonsInterfaceController: WKInterfaceController { @IBOutlet weak var button1:

    WKInterfaceButton! @IBOutlet weak var button2: WKInterfaceButton! @IBOutlet weak var button3: WKInterfaceButton! @IBAction func action1() { println("Action 1") } @IBAction func action2() { println("Action 2") } @ IBAction func action3() { println("Action 3") } override func awakeWithContext(context: AnyObject?) { super.awakeWithContext(context) button1.setTitle("Title 1") button2.setTitle("Title 2") button3.setTitle("Title 3") } override func willActivate() { // This method is called when watch view controller is about to be visible to user super.willActivate() NSLog("%@ will activate", self) } override func didDeactivate() { // This method is called when watch view controller is no longer visible NSLog("%@ did deactivate", self) super.didDeactivate() } }
  19. Lifecycle Create WKInterfaceController subclass
 ❖ init ❖ awakeWithContext(context: AnyObject!) ❖

    willActivate ❖ didDeactivate
 Initialize UI Configure Display Hide
  20. Share Data ❖ Setup ❖ Enable App Groups ❖ Set

    group name “group.com.me.myapp” ❖ Share Data ❖ NSUserDefaults ❖ Files ❖ MMWormhole ❖ CoreData
  21. Share Data // NSUserDefaults let defaults = NSUserDefaults(suiteName: "group.com.kkoval.DataSharing") defaults?.objectForKey(itemsKey)

    // File let containerURL = NSFileManager.defaultManager().containerURLForSecurityApplicationGroupIdentifier(groupName) // CoreData … let url = containerURL.URLByAppendingPathComponent(“db.sqlite”) coordinator.addPersistentStoreWithType(NSSQLiteStoreType, configuration: nil, URL: url, options: nil, error: &error) … Or use Seru var persistance = PersistenceLayer(name: "Database", location: .SharedGroup("group.com.myapp"))
  22. Share Code ❖ Model ❖ Data Access ❖ Business Logic

    ❖ Add to both Targets ❖ Make a Framework ( ✓App Extension Api only) ❖ !! NSBundle.mainBundle wont work
  23. Communication // iOS App func application(application: UIApplication!, handleWatchKitExtensionRequest userInfo: [NSObject

    : AnyObject]!, reply: (([NSObject : AnyObject]!) -> Void)?) { if let info = userInfo as? [String: String] { personViewController.showPerson(info["personName"]!) reply.map { $0(["response" : "success"]) } } else { reply.map { $0(["response" : "fail"]) } } } // WatchKit Extension App var userInfo = ["personName" : person] WKInterfaceController.openParentApplication(userInfo, reply: { (data, error) in if let error = error { println(error) } if let data = data { println(data) } })