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

我发现了许多问题和许多答案,但没有最后的例子请求:

任何人都可以在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];
    }
}

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

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

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

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

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

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

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

解决方法

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

电话:

我相信应用程序:didFinishLaunchingWithOptions:handler是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];
}

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

NSError *updateContextError;
BOOL isContextUpdated = [[WCSession defaultSession] updateApplicationContext:@{@"heartRate": @"90"} error:&updateContextError]

if (!isContextUpdated) {
    NSLog(@"Update failed with error: %@",updateContextError);
}

更新:

看:

ExtensionDelegate.h:

@import WatchConnectivity;
#import <WatchKit/WatchKit.h>

@interface ExtensionDelegate : NSObject <WKExtensionDelegate,WCSessionDelegate>
@end

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

在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

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

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

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

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

以上是来客网为你收集整理的ios – 将WCSession与多个ViewController一起使用全部内容,希望文章能够帮你解决ios – 将WCSession与多个ViewController一起使用所遇到的程序开发问题。

如果觉得来客网网站内容还不错,欢迎将来客网网站推荐给程序员好友。