如何在 Objective-C 的 OS2 中将数据从 Iphone 发送到 Apple Watch
How to send data from Iphone to Apple Watch in OS2 in Objective-C
我看过 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。
我最擅长使用清晰的示例,而不是讲课 material。因此,如果有人已经实现了这个并且不介意分享,那将不胜感激。
这是关于 WatchConnectivity 的 link 到 Q/A:Send messages between iOS and WatchOS with WatchConnectivity in watchOS2
我会给你一个例子去 ApplicationContext,还有 2 个其他消息技术与 WatchConnectivity。请观看 WWDC2015 session 视频。
首先你需要在类你想要发送和接收数据from/to中遵守WCSessionDelegate协议。例如手表和 iPhone.
之前的基本检查:(这只是一个例子,比这个实现得更好)
if ([WCSession isSupported]) {
WCSession *session = [WCSession defaultSession];
session.delegate = self;
[session activateSession];
NSLog(@"SESSION AVAIBLE");
}
//Objective-C
if ([[WCSession defaultSession] isReachable]) {
NSLog(@"SESSION REACHABLE");
}
这会将数据从 phone 发送到手表。
WCSession *session = [WCSession defaultSession];
NSError *error;
[session updateApplicationContext:@{@"firstItem": @"item1", @"secondItem":[NSNumber numberWithInt:2]} error:&error];
这将从手表上的 phone 接收数据。
- (void) session:(nonnull WCSession *)session didReceiveApplicationContext:(nonnull NSDictionary<NSString *,id> *)applicationContext {
NSLog(@"%@", applicationContext);
item1 = [applicationContext objectForKey:@"firstItem"];
item2 = [[applicationContext objectForKey:@"secondItem"] intValue];
}
WatchConnectivity 上的 WWDC2015 视频非常棒,推荐观看。
我看过
我最擅长使用清晰的示例,而不是讲课 material。因此,如果有人已经实现了这个并且不介意分享,那将不胜感激。
这是关于 WatchConnectivity 的 link 到 Q/A:Send messages between iOS and WatchOS with WatchConnectivity in watchOS2
我会给你一个例子去 ApplicationContext,还有 2 个其他消息技术与 WatchConnectivity。请观看 WWDC2015 session 视频。
首先你需要在类你想要发送和接收数据from/to中遵守WCSessionDelegate协议。例如手表和 iPhone.
之前的基本检查:(这只是一个例子,比这个实现得更好)
if ([WCSession isSupported]) {
WCSession *session = [WCSession defaultSession];
session.delegate = self;
[session activateSession];
NSLog(@"SESSION AVAIBLE");
}
//Objective-C
if ([[WCSession defaultSession] isReachable]) {
NSLog(@"SESSION REACHABLE");
}
这会将数据从 phone 发送到手表。
WCSession *session = [WCSession defaultSession];
NSError *error;
[session updateApplicationContext:@{@"firstItem": @"item1", @"secondItem":[NSNumber numberWithInt:2]} error:&error];
这将从手表上的 phone 接收数据。
- (void) session:(nonnull WCSession *)session didReceiveApplicationContext:(nonnull NSDictionary<NSString *,id> *)applicationContext {
NSLog(@"%@", applicationContext);
item1 = [applicationContext objectForKey:@"firstItem"];
item2 = [[applicationContext objectForKey:@"secondItem"] intValue];
}
WatchConnectivity 上的 WWDC2015 视频非常棒,推荐观看。