Slide 1

Slide 1 text

PRACTICAL WATCHKIT BY @NEILKIMMETT

Slide 2

Slide 2 text

No content

Slide 3

Slide 3 text

No content

Slide 4

Slide 4 text

No content

Slide 5

Slide 5 text

mandsdigital.com

Slide 6

Slide 6 text

No content

Slide 7

Slide 7 text

No content

Slide 8

Slide 8 text

No content

Slide 9

Slide 9 text

HOW?

Slide 10

Slide 10 text

No content

Slide 11

Slide 11 text

No content

Slide 12

Slide 12 text

No content

Slide 13

Slide 13 text

No content

Slide 14

Slide 14 text

WKInterfaceButton

Slide 15

Slide 15 text

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) }

Slide 16

Slide 16 text

button.setText("I'm a button")

Slide 17

Slide 17 text

No content

Slide 18

Slide 18 text

No content

Slide 19

Slide 19 text

No content

Slide 20

Slide 20 text

No content

Slide 21

Slide 21 text

No content

Slide 22

Slide 22 text

No content

Slide 23

Slide 23 text

No content

Slide 24

Slide 24 text

SHARED APP GROUP

Slide 25

Slide 25 text

NSUSERDEFAULTS let group = "group.me.kimmett.pushthebutton" let defaults = NSUserDefaults(suiteName: group) defaults?.setInteger(42, forKey: "IMPORTANT_NUMBER") defaults?.integerForKey("IMPORTANT_NUMBER") // 42

Slide 26

Slide 26 text

NSFILEMANAGER let group = "group.me.kimmett.pushthebutton" let manager = NSFileManager.defaultManager() if let dir = manager.containerURLForSecurityApplicationGroupIdentifier(group) { // do something with dir }

Slide 27

Slide 27 text

CORE DATA?

Slide 28

Slide 28 text

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) }

Slide 29

Slide 29 text

No content

Slide 30

Slide 30 text

“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/

Slide 31

Slide 31 text

FIXED IN IOS 8.2

Slide 32

Slide 32 text

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

Slide 33

Slide 33 text

REAL TIME SYNC?

Slide 34

Slide 34 text

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

Slide 35

Slide 35 text

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)!)

Slide 36

Slide 36 text

OPENPARENTAPPLICATION var task: UIBackgroundTaskIdentifier task = application.beginBackgroundTaskWithExpirationHandler { reply([:]) application.endBackgroundTask(task) task = UIBackgroundTaskInvalid } // do something with userInfo reply(…) application.endBackgroundTask(task) task = UIBackgroundTaskInvalid

Slide 37

Slide 37 text

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

Slide 38

Slide 38 text

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

Slide 39

Slide 39 text

HANDOFF

Slide 40

Slide 40 text

WKINTERFACECONTROLLER func updateUserActivity(type: String, userInfo: [NSObject : AnyObject]?, webpageURL: NSURL?) UIAPPLICATIONDELEGATE func application(application: UIApplication, continueUserActivity: NSUserActivity, restorationHandler: ([AnyObject]!) -> Void) -> Bool

Slide 41

Slide 41 text

DESIGN TIPS

Slide 42

Slide 42 text

BLACK IS THE NEW WHITE

Slide 43

Slide 43 text

BLACK IS THE NEW WHITE

Slide 44

Slide 44 text

BLACK IS THE NEW WHITE

Slide 45

Slide 45 text

No content

Slide 46

Slide 46 text

No content

Slide 47

Slide 47 text

REDUCE "TAP DISTANCE"

Slide 48

Slide 48 text

REDUCE "TAP DISTANCE"

Slide 49

Slide 49 text

No content

Slide 50

Slide 50 text

REDUCE PADDING

Slide 51

Slide 51 text

REDUCE PADDING

Slide 52

Slide 52 text

REDUCE PADDING

Slide 53

Slide 53 text

REDUCE PADDING

Slide 54

Slide 54 text

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/

Slide 55

Slide 55 text

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

Slide 56

Slide 56 text

SLIDES http://kimmett.me/talks/practical-watchkit.html

Slide 57

Slide 57 text

QUESTIONS?