问题描述
我见过。我在Objective-C中问同样的问题。我还查看了Apple的。
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.
推荐答案
这是一个关于WatchConnectivity的Q / A的链接:
我会给你一个例如go ApplicationContext,WatchConnectivity还有其他两种消息传递技术。请观看WWDC2015会话视频。
I will give you an example go ApplicationContext, there are 2 other messaging techniques with WatchConnectivity. Please watch WWDC2015 session video for those.
首先,您需要在要发送和从中接收数据的类中符合WCSessionDelegate协议。例如,在手表和iPhone上。
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];
}
WatchConnectivity上的WWDC2015视频非常棒,我建议您查看。
The WWDC2015 video on WatchConnectivity is really great, I recommend to check it out.
这篇关于如何在Objective-C中将数据从Iphone发送到OS2中的Apple Watch的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!