Slide 1

Slide 1 text

Curtis Herbert (.com) @parrots WatchKit Communication

Slide 2

Slide 2 text

What can we do in early 2015? Glance Notification App https://developer.apple.com/watchkit/

Slide 3

Slide 3 text

What can we do in early 2015? Glance Notification App https://developer.apple.com/watchkit/ You can provide users with timely read-only information that they care about with a Glance — a quick and lightweight view of your app.

Slide 4

Slide 4 text

What can we do in early 2015? Glance Notification App https://developer.apple.com/watchkit/ Actionable notifications built and designed with WatchKit let users take action right from their wrists.

Slide 5

Slide 5 text

What can we do in early 2015? Glance Notification App https://developer.apple.com/watchkit/ Your app on Apple Watch contains a full user interface. Users can launch, control, and interact with your app in ways unique to Apple Watch.

Slide 6

Slide 6 text

What Makes Up a WatchKit App?

Slide 7

Slide 7 text

Parent App Delivered with your app

Slide 8

Slide 8 text

Parent App E Watch Extension Delivered with your app

Slide 9

Slide 9 text

Parent App E Watch Extension Just the UI Watch “App” Delivered with your app

Slide 10

Slide 10 text

Parent App E Watch Extension Just the UI Watch “App” Delivered with your app

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

Parent App E Watch Extension Separate processes

Slide 15

Slide 15 text

Parent App E Watch Extension Separate processes

Slide 16

Slide 16 text

Parent App E Watch Extension setters getters ui events Bluetooth

Slide 17

Slide 17 text

Parent App E Watch Extension App can’t wake extension

Slide 18

Slide 18 text

Watchkit app made of 2 parts - “App” and extension Extension <-> watch app is a lightweight channel Extension <-> parent app can only be started by the extension Architectural Takeaways

Slide 19

Slide 19 text

A WatchKit App Lifecycle

Slide 20

Slide 20 text

E Watch Extension Initial Launch Watch “App” User launches app Load storyboard init awakeWithContext: Show UI willActivate

Slide 21

Slide 21 text

E Watch Extension Interaction willActivate didDeactivate …various UI-triggered actions…

Slide 22

Slide 22 text

E Watch Extension Interaction willActivate didDeactivate …various UI-triggered actions…

Slide 23

Slide 23 text

E Watch Extension Interaction willActivate didDeactivate …various UI-triggered actions…

Slide 24

Slide 24 text

Stay lightweight in watch extension, especially init and awake UI updates when screen isn’t active are dropped on the floor Lifecycle Takeaways

Slide 25

Slide 25 text

Communication

Slide 26

Slide 26 text

openParentApplication:reply:

Slide 27

Slide 27 text

Polo! Marco?

Slide 28

Slide 28 text

[WKInterfaceController openParentApplication:@{...} reply:^(NSDictionary *replyInfo, NSError *error) { if (error) { NSLog(@"Error from parent: %@", error); } else { //do something with the reply info.... } }]; E Any WKInterfaceController

Slide 29

Slide 29 text

- (void)application:(UIApplication *)application handleWatchKitExtensionRequest:(NSDictionary *)userInfo reply:(void(^)(NSDictionary *replyInfo))reply { reply(@{...}); } AppDelegate

Slide 30

Slide 30 text

openParentApplication:reply: • Will launch your parent app! • Great way to send actions / triggers Pros • Can’t do too many back-to-back (serialized queue) • Launches parent in background mode-only • Have fun with your app lifecycle Cons

Slide 31

Slide 31 text

App Group & NSUserDefaults

Slide 32

Slide 32 text

Parent App E iOS 8 Extension Extension data sharing ex: “com.consumedbycode.slopes.group” App groups x

Slide 33

Slide 33 text

Parent App E iOS 8 Extension Extension data sharing ex: “com.consumedbycode.slopes.group” App groups x

Slide 34

Slide 34 text

Parent App E iOS 8 Extension Extension data sharing ex: “com.consumedbycode.slopes.group” App groups x

Slide 35

Slide 35 text

Anywhere NSUserDefaults *defaults = [NSUserDefaults initWithSuite:@“my.group.name”]; [defaults setInteger:4 forKey@"myKey"]; [defaults synchronize];

Slide 36

Slide 36 text

NSUserDefaults *defaults = [NSUserDefaults initWithSuite:@“my.group.name"]; NSInteger myInt = [defaults integerForKey@"myKey"]; E Anywhere

Slide 37

Slide 37 text

initWithSuite is separate from standardUserDefaults

Slide 38

Slide 38 text

NSUserDefaults • Simple API to serialize and read data Pros • No KVO Cons

Slide 39

Slide 39 text

App Groups & Core Data

Slide 40

Slide 40 text

NSURL *directory = [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject]; NSURL *storeURL = [directory URLByAppendingPathComponent:@“myApp.sqlite"]; //init persistent store, etc..

Slide 41

Slide 41 text

Anywhere NSURL *directory = [[NSFileManager defaultManager] containerURLForSecurityApplicationGroupIdentifier:@“ my.group.name”]; NSURL *storeURL = [directory URLByAppendingPathComponent:@“myApp.sqlite"]; //init persistent store, etc.. E

Slide 42

Slide 42 text

Core Data • Reuse existing model • Set up for multi-threading • KVO Pros • Core Data (I kid, I kid) • Bad place to store one-off values to pass around • Need to move DB to App Group container Cons

Slide 43

Slide 43 text

App Groups & Darwin Notification Center

Slide 44

Slide 44 text

pod ‘MMWormhole’

Slide 45

Slide 45 text

Anywhere MMWormhole *wormhole = [[MMWormhole alloc] initWithApplicationGroupIdentifier:@"my.group.name" optionalDirectory:@"wormhole"]; //whenever the data changes… [wormhole passMessageObject:@(5.5) identifier:@"currentSpeed"]; E

Slide 46

Slide 46 text

Anywhere //get the current value NSNumber *currentSpeed = [wormhole messageWithIdentifier:@"currentSpeed"]; //...or subscribe for updates [wormhole listenForMessageWithIdentifier:@"currentSpeed" listener:^(id messageObject) { //do something with the speed }]; E

Slide 47

Slide 47 text

Anywhere [wormhole stopListeningForMessageWithIdentifier:@"currentSpeed"]; E

Slide 48

Slide 48 text

WKInterfaceController - (void)willActivate { [super willActivate]; [self.wormhole listenForMessageWithIdentifier:@"currentSpeed" listener:^(id messageObject) { //do something with the speed, update a label, etc }]; } - (void)didDeactivate { [super didDeactivate]; [self.wormhole stopListeningForMessageWithIdentifier:@"currentSpeed"]; } E

Slide 49

Slide 49 text

MMWormhole • KVO • Great for one-off values Pros • Separate data you have to keep in sync from your model layer Cons

Slide 50

Slide 50 text

openParentApplication:reply: great for actions, bad for lots of data fetching NSUserDefaults: great for data, but no notification of changes CoreData: great for sharing existing model data, bad for one-off messages Darwin Notification Center: consider in place of NSUserDefaults Communication Takeaways

Slide 51

Slide 51 text

Demo time

Slide 52

Slide 52 text

https://developer.apple.com/watchkit/ https://github.com/mutualmobile/MMWormhole http://infinitapps.com/bezel/ Resources