How to send data from iPhone to Apple Watch in OS2 in Objective-C

I saw a similar question on how to send data to Swift. I ask the same question, but in Objective-C. I also looked at Apple transition documents .

I work best with clear examples, not lecture material. Therefore, if someone did this and did not want to share them, it would be greatly appreciated.

+5
source share
1 answer

Here is a Q / A link about WatchConnectivity: Send messages between iOS and WatchOS with WatchConnectivity in watchOS2


I'll give you an example go applicationContext, there are 2 other messaging methods with WatchConnectivity. Please see the WWDC2015 session video recording for them.

First you need to comply with the WCSessionDelegate protocol in the classes you want to send and receive data from / to. For example, both on the watch and on the iPhone.

Basic check before: (this is just an example, it is better to implement this)

if ([WCSession isSupported]) { WCSession *session = [WCSession defaultSession]; session.delegate = self; [session activateSession]; NSLog(@"SESSION AVAIBLE"); } //Objective-C if ([[WCSession defaultSession] isReachable]) { NSLog(@"SESSION REACHABLE"); } 

This will send data from the phone to the watch.

 WCSession *session = [WCSession defaultSession]; NSError *error; [session updateApplicationContext:@{@"firstItem": @"item1", @"secondItem":[NSNumber numberWithInt:2]} error:&error]; 

This will receive data from the phone on the watch.

 - (void) session:(nonnull WCSession *)session didReceiveApplicationContext:(nonnull NSDictionary<NSString *,id> *)applicationContext { NSLog(@"%@", applicationContext); item1 = [applicationContext objectForKey:@"firstItem"]; item2 = [[applicationContext objectForKey:@"secondItem"] intValue]; } 

The WWDC2015 video on WatchConnectivity is really great, I recommend checking it out.

+21
source

All Articles