Ing*_*Ron 8 ios watchkit watchos-2
我发现了许多问题和许多答案,但没有最后的例子请求:
任何人都可以在Objective C中给出最后一个示例,将WCSession与IOS应用程序和带有多个ViewController的Watch应用程序(WatchOS2)一起使用的最佳做法是什么.
到目前为止我注意到的是以下事实:
1.)在AppDelegate的父(IOS)应用程序中激活WCSession:
- (BOOL)application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
//Any other code you might have
if ([WCSession isSupported]) {
self.session = [WCSession defaultSession];
self.session.delegate = self;
[self.session activateSession];
}
}
Run Code Online (Sandbox Code Playgroud)
2.)在WatchOS2侧使用<WCSessionDelegate>.但其余的对我来说完全不清楚!一些答案是通过在传递的字典中指定键来讨论,如:
[session updateApplicationContext:@{@"viewController1": @"item1"} error:&error];
[session updateApplicationContext:@{@"viewController2": @"item2"} error:&error];
Run Code Online (Sandbox Code Playgroud)
其他人正在讨论检索默认会话
WCSession* session = [WCSession defaultSession];
[session updateApplicationContext:applicationDict error:nil];
Run Code Online (Sandbox Code Playgroud)
其他人在谈论不同的队列?"如果有必要,客户有责任派遣到另一个队列.发送回主要队列."
我完全糊涂了.因此,请举例说明如何将WCSession与IOS应用程序和带有多个ViewController的WatchOS2应用程序一起使用.
我需要它用于以下情况(简化):在我的父应用程序中,我测量心率,锻炼时间和卡路里.在Watch应用程序1. ViewController我将在2显示心率和锻炼时间.ViewController我也将显示心率和燃烧的卡路里.
据我所知,您只需要在一个Phone -> Watch方向上进行同步,所以简而言之,最低配置为您:
电话:
我相信application:didFinishLaunchingWithOptions:处理程序是WCSession初始化的最佳位置,因此将以下代码放在那里:
if ([WCSession isSupported]) {
// You even don't need to set a delegate because you don't need to receive messages from Watch.
// Everything that you need is just activate a session.
[[WCSession defaultSession] activateSession];
}
Run Code Online (Sandbox Code Playgroud)
然后在代码中的某个位置测量心率,例如:
NSError *updateContextError;
BOOL isContextUpdated = [[WCSession defaultSession] updateApplicationContext:@{@"heartRate": @"90"} error:&updateContextError]
if (!isContextUpdated) {
NSLog(@"Update failed with error: %@", updateContextError);
}
Run Code Online (Sandbox Code Playgroud)
更新:
看:
ExtensionDelegate.h:
@import WatchConnectivity;
#import <WatchKit/WatchKit.h>
@interface ExtensionDelegate : NSObject <WKExtensionDelegate, WCSessionDelegate>
@end
Run Code Online (Sandbox Code Playgroud)
ExtensionDelegate.m:
#import "ExtensionDelegate.h"
@implementation ExtensionDelegate
- (void)applicationDidFinishLaunching {
// Session objects are always available on Apple Watch thus there is no use in calling +WCSession.isSupported method.
[WCSession defaultSession].delegate = self;
[[WCSession defaultSession] activateSession];
}
- (void)session:(nonnull WCSession *)session didReceiveApplicationContext:(nonnull NSDictionary<NSString *,id> *)applicationContext {
NSString *heartRate = [applicationContext objectForKey:@"heartRate"];
// Compose a userInfo to pass it using postNotificationName method.
NSDictionary *userInfo = [NSDictionary dictionaryWithObject:heartRate forKey:@"heartRate"];
// Broadcast data outside.
[[NSNotificationCenter defaultCenter] postNotificationName: @"heartRateDidUpdate" object:nil userInfo:userInfo];
}
@end
Run Code Online (Sandbox Code Playgroud)
在Controller的某处,我们将其命名为XYZController1.
XYZController1:
#import "XYZController1.h"
@implementation XYZController1
- (void)awakeWithContext:(id)context {
[super awakeWithContext:context];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleUpdatedHeartRate:) name:@"heartRateDidUpdate" object:nil];
}
-(void)handleUpdatedHeartRate:(NSNotification *)notification {
NSDictionary* userInfo = notification.userInfo;
NSString* heartRate = userInfo[@"heartRate"];
NSLog (@"Successfully received heartRate notification!");
}
@end
Run Code Online (Sandbox Code Playgroud)
代码尚未经过测试我只是按原样编写,因此可能存在一些拼写错误.
我认为现在的主要想法非常明确,剩下的数据类型的转移并不是那么艰巨的任务.
我目前的WatchConnectivity架构要复杂得多,但它基于这种逻辑.
如果您还有任何问题,我们可以进一步讨论聊天.
| 归档时间: |
|
| 查看次数: |
5039 次 |
| 最近记录: |