Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
303 views
in Technique[技术] by (71.8m points)

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

I've seen a similar question posted on how to send data back and forth in Swift. I'm asking the same question but in Objective-C. I've also viewed Apple's transition docs.

I work best with clear examples, rather than lecture material. So if someone has implemented this and wouldn't mind sharing, that would be much appreciated.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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


I will give you an example go ApplicationContext, there are 2 other messaging techniques with WatchConnectivity. Please watch WWDC2015 session video for those.

First you need to conform to the WCSessionDelegate protocol in the classes you want to send and receive data from/to. E.g both on watch and iPhone.

Basic checking before: (this is just an example, implement better than 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 the 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 the 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 to check it out.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...