问题描述
我看过一个关于如何在 Swift 中来回发送数据的类似问题.我在问同样的问题,但在 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 的问答链接:在 watchOS2 中使用 WatchConnectivity 在 iOS 和 WatchOS 之间发送消息
我会给你一个例子 go ApplicationContext,还有另外 2 种使用 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的OS2中将数据从Iphone发送到Apple Watch的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!