ios – 将WCSession与多个ViewController一起使用

前端之家收集整理的这篇文章主要介绍了ios – 将WCSession与多个ViewController一起使用前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我发现了许多问题和许多答案,但没有最后的例子请求:

任何人都可以在Objective C中给出最后的示例,将WCSession与IOS应用程序和带有多个ViewController的Watch应用程序(WatchOS2)一起使用的最佳做法是什么.

到目前为止我注意到的是以下事实:

1.)在AppDelegate的父(IOS)应用程序中激活WCSession:

  1. - (BOOL)application:(UIApplication *)application
  2. didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
  3. {
  4. //Any other code you might have
  5.  
  6. if ([WCSession isSupported]) {
  7. self.session = [WCSession defaultSession];
  8. self.session.delegate = self;
  9. [self.session activateSession];
  10. }
  11. }

2.)在WatchOS2端使用< WCSessionDelegate>.但其余的对我来说完全不清楚!一些答案是通过在传递的字典中指定键来讨论,如:

  1. [session updateApplicationContext:@{@"viewController1": @"item1"} error:&error];
  2. [session updateApplicationContext:@{@"viewController2": @"item2"} error:&error];

其他人正在讨论检索默认会话

  1. WCSession* session = [WCSession defaultSession];
  2. [session updateApplicationContext:applicationDict error:nil];

其他人在谈论不同的队列? “如果有必要,客户有责任派遣到另一个队列.发送回主要队列.”

我完全糊涂了.因此,请举例说明如何将WCSession与IOS应用程序和带有多个ViewController的WatchOS2应用程序一起使用.

我需要它用于以下情况(简化):
在我的父应用程序中,我正在测量心率,锻炼时间和卡路里.在Watch应用程序1. ViewController我会在2点显示心率和锻炼时间.ViewController我也会显示心率和燃烧的卡路里.

解决方法

据我所知,你只需要在手机中同步任务 – >观察方向,简而言之,为您提供最低配置:

电话:

我相信应用程序:didFinishLaunchingWithOptions:handler是WCSession初始化的最佳位置,因此将以下代码放在那里:

  1. if ([WCSession isSupported]) {
  2. // You even don't need to set a delegate because you don't need to receive messages from Watch.
  3. // Everything that you need is just activate a session.
  4. [[WCSession defaultSession] activateSession];
  5. }

然后在代码中的某个位置测量心率,例如:

  1. NSError *updateContextError;
  2. BOOL isContextUpdated = [[WCSession defaultSession] updateApplicationContext:@{@"heartRate": @"90"} error:&updateContextError]
  3.  
  4. if (!isContextUpdated) {
  5. NSLog(@"Update Failed with error: %@",updateContextError);
  6. }

更新:

看:

ExtensionDelegate.h:

  1. @import WatchConnectivity;
  2. #import <WatchKit/WatchKit.h>
  3.  
  4. @interface ExtensionDelegate : NSObject <WKExtensionDelegate,WCSessionDelegate>
  5. @end

ExtensionDelegate.m:

  1. #import "ExtensionDelegate.h"
  2.  
  3. @implementation ExtensionDelegate
  4.  
  5. - (void)applicationDidFinishLaunching {
  6. // Session objects are always available on Apple Watch thus there is no use in calling +WCSession.isSupported method.
  7. [WCSession defaultSession].delegate = self;
  8. [[WCSession defaultSession] activateSession];
  9. }
  10.  
  11. - (void)session:(nonnull WCSession *)session didReceiveApplicationContext:(nonnull NSDictionary<NSString *,id> *)applicationContext {
  12. NSString *heartRate = [applicationContext objectForKey:@"heartRate"];
  13.  
  14. // Compose a userInfo to pass it using postNotificationName method.
  15. NSDictionary *userInfo = [NSDictionary dictionaryWithObject:heartRate forKey:@"heartRate"];
  16.  
  17. // Broadcast data outside.
  18. [[NSNotificationCenter defaultCenter] postNotificationName: @"heartRateDidUpdate" object:nil userInfo:userInfo];
  19. }
  20.  
  21. @end

在Controller的某个地方,我们将它命名为XYZController1.

XYZController1:

  1. #import "XYZController1.h"
  2.  
  3. @implementation XYZController1
  4.  
  5. - (void)awakeWithContext:(id)context {
  6. [super awakeWithContext:context];
  7.  
  8. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleUpdatedHeartRate:) name:@"heartRateDidUpdate" object:nil];
  9. }
  10.  
  11. -(void)handleUpdatedHeartRate:(NSNotification *)notification {
  12. NSDictionary* userInfo = notification.userInfo;
  13. NSString* heartRate = userInfo[@"heartRate"];
  14. NSLog (@"Successfully received heartRate notification!");
  15. }
  16.  
  17. @end

代码尚未经过测试我只是按原样编写,因此可能存在一些拼写错误.

我认为现在的主要想法非常明确,剩下的数据类型的转移并不是那么艰巨的任务.

我目前的WatchConnectivity架构要复杂得多,但它基于这种逻辑.

如果您还有任何问题,我们可以进一步讨论聊天.

猜你在找的iOS相关文章