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

Practical WatchKit

Practical WatchKit

Given at NSLondon on 23/04/2015.

20cd0627a5c82f8b3e4f30a04745f0cf?s=128

Neil Kimmett

April 23, 2015
Tweet

Transcript

  1. PRACTICAL WATCHKIT BY @NEILKIMMETT

  2. None
  3. None
  4. None
  5. mandsdigital.com

  6. None
  7. None
  8. None
  9. HOW?

  10. None
  11. None
  12. None
  13. None
  14. WKInterfaceButton

  15. class WKInterfaceButton : WKInterfaceObject { func setTitle(title: String?) func setAttributedTitle(attributedTitle:

    NSAttributedString?) func setBackgroundColor(color: UIColor?) func setBackgroundImage(image: UIImage?) func setBackgroundImageData(imageData: NSData?) func setBackgroundImageNamed(imageName: String?) func setEnabled(enabled: Bool) }
  16. button.setText("I'm a button")

  17. None
  18. None
  19. None
  20. None
  21. None
  22. None
  23. None
  24. SHARED APP GROUP

  25. NSUSERDEFAULTS let group = "group.me.kimmett.pushthebutton" let defaults = NSUserDefaults(suiteName: group)

    defaults?.setInteger(42, forKey: "IMPORTANT_NUMBER") defaults?.integerForKey("IMPORTANT_NUMBER") // 42
  26. NSFILEMANAGER let group = "group.me.kimmett.pushthebutton" let manager = NSFileManager.defaultManager() if

    let dir = manager.containerURLForSecurityApplicationGroupIdentifier(group) { // do something with dir }
  27. CORE DATA?

  28. CORE DATA if let dir = manager.containerURLForSecurityApplicationGroupIdentifier(group) { let url

    = dir.URLByAppendingPathComponent(“Database.sqlite”) let coordinator = … coordinator.migratePersistentStore(store, toURL: url, options: nil, withType: NSSQLiteStoreType, error: &error) }
  29. None
  30. “on Yosemite and iOS 8, the method discards many-to-many relationships”

    — http://mjtsai.com/blog/2014/11/22/core-data-relationships-data- loss-bug/
  31. FIXED IN IOS 8.2

  32. MOVE THESE FILES .sqlite .sqlite-wal .sqlite-shm

  33. REAL TIME SYNC?

  34. REAL TIME SYNC ▸ MMWormhole https://github.com/mutualmobile/MMWormhole ▸ WFNotificationCenter https://github.com/DeskConnect/WFNotificationCenter

  35. WKINTERFACECONTROLLER class func openParentApplication( userInfo: [NSObject : AnyObject], reply: (([NSObject

    : AnyObject]!, NSError!) -> Void)?) -> Bool UIAPPLICATIONDELEGATE func application(application: UIApplication, handleWatchKitExtensionRequest: [NSObject : AnyObject]?, reply: (([NSObject : AnyObject]!) -> Void)!)
  36. OPENPARENTAPPLICATION var task: UIBackgroundTaskIdentifier task = application.beginBackgroundTaskWithExpirationHandler { reply([:]) application.endBackgroundTask(task)

    task = UIBackgroundTaskInvalid } // do something with userInfo reply(…) application.endBackgroundTask(task) task = UIBackgroundTaskInvalid
  37. ONE WEIRD TRICK... “…you have to begin – and end,

    after two seconds – an empty background task right at the beginning of the delegate method.” — http://www.fiveminutewatchkit.com/blog/2015/3/11/one-weird- trick-to-fix-openparentapplicationreply
  38. ONE WEIRD TRICK... var bogusTask: UIBackgroundTaskIdentifier bogusTask = application.beginBackgroundTaskWithExpirationHandler {

    application.endBackgroundTask(bogusTask) } let time = dispatch_time(DISPATCH_TIME_NOW, Int64(2 * Double(NSEC_PER_SEC))) dispatch_after(time, dispatch_get_main_queue(), { UIApplication.sharedApplication().endBackgroundTask(bogusTask) }) // do your actual work
  39. HANDOFF

  40. WKINTERFACECONTROLLER func updateUserActivity(type: String, userInfo: [NSObject : AnyObject]?, webpageURL: NSURL?)

    UIAPPLICATIONDELEGATE func application(application: UIApplication, continueUserActivity: NSUserActivity, restorationHandler: ([AnyObject]!) -> Void) -> Bool
  41. DESIGN TIPS

  42. BLACK IS THE NEW WHITE

  43. BLACK IS THE NEW WHITE

  44. BLACK IS THE NEW WHITE

  45. None
  46. None
  47. REDUCE "TAP DISTANCE"

  48. REDUCE "TAP DISTANCE"

  49. None
  50. REDUCE PADDING

  51. REDUCE PADDING

  52. REDUCE PADDING

  53. REDUCE PADDING

  54. REFERENCES ▸ “Core Data Relationships Data Loss Bug” by Michael

    Tsai http://mjtsai.com/blog/2014/11/22/core-data-relationships-data- loss-bug/ ▸ “Tap Distance” by David Smith http://david-smith.org/blog/2015/02/03/ailw-tap-distance/
  55. REFERENCES ▸ “One weird trick to “fix” openParentApplication:reply:” by Brian

    Gilham http://www.fiveminutewatchkit.com/blog/2015/3/11/one-weird- trick-to-fix-openparentapplicationreply
  56. SLIDES http://kimmett.me/talks/practical-watchkit.html

  57. QUESTIONS?