具有WatchConnectivity Context的多手表界面

时间:2015-10-27 14:10:21

标签: ios watchkit watchconnectivity

我创建一个iPhone应用程序(带有视图)/ Watch(带界面),显示速度,距离和带有播放/暂停,停止和清除的计时器。

我曾经在(发送和接收上下文)之间共享WatchConnectivity应用程序上下文数据。一切都工作到目前为止,但我会添加另一个页面/接口交换使用iPhone观察上下文,我根本不知道如何。

My Interface Storyboard

如果我打开Session 2接口,信息将丢失

这是我当前的代码,我想在第二个界面上切换标签TimeLabel,distanceLabel Label和speed的显示

//
//  InterfaceController.m
//  Watch Extension
//
//  Created by Arnaud Roy on 25/10/2015.
//  Copyright © 2015 Burotica. All rights reserved.
//

#import "InterfaceController.h"
#import <WatchConnectivity/WatchConnectivity.h>

@interface InterfaceController() <WCSessionDelegate>
    @property (strong, nonatomic) WCSession *session;
    @property (unsafe_unretained, nonatomic) IBOutlet WKInterfaceButton *startLabel;
    @property (unsafe_unretained, nonatomic) IBOutlet WKInterfaceLabel *timeLabel;
    @property (unsafe_unretained, nonatomic) IBOutlet WKInterfaceLabel *distanceLabel;
    @property (unsafe_unretained, nonatomic) IBOutlet WKInterfaceLabel *vitesseLabel;
    @property (nonatomic,assign) BOOL running;
@end


@implementation InterfaceController

- (void)awakeWithContext:(id)context {
    [super awakeWithContext:context];
    // Configure interface objects here.
}

- (void)willActivate {
    // This method is called when watch view controller is about to be visible to user
    [super willActivate];

    if ([WCSession isSupported]) {
        WCSession *session = [WCSession defaultSession];
        session.delegate = self;
        [session activateSession];
        //NSLog(@"SESSION AVAIBLE");
    }
    //Objective-C
    if ([[WCSession defaultSession] isReachable]) {
        //NSLog(@"SESSION REACHABLE");
    }
    self.running = false;
}

- (void)didDeactivate {
    // This method is called when watch view controller is no longer visible
    [super didDeactivate];
}

- (IBAction)startButton {
    if(self.running == false) {
        [self sendCmd:@"start"];
        [self.startLabel setTitle:[NSString stringWithFormat:@"PAUSE"]];
        self.running = true;
    }
    else
    {
        [self sendCmd:@"pause"];
        [self.startLabel setTitle:[NSString stringWithFormat:@"PLAY"]];
        self.running = false;
    }
}

- (IBAction)clearButton {
   [self sendCmd:@"clear"];
}

- (IBAction)plusmoinsButton {
   [self sendCmd:@"plusmoins"];
}

- (IBAction)stopButton {
    [self sendCmd:@"stop"];
}

-(void)sendCmd:(NSString*) cmdSendW{
    WCSession *session = [WCSession defaultSession];
    NSError *error;
    [session updateApplicationContext:@{@"cmdSendW":cmdSendW} error:&error];
}

- (void)session:(nonnull WCSession *)session didReceiveApplicationContext:(nonnull NSDictionary<NSString *,id> *)applicationContext {
    NSString *cmdSend = [applicationContext objectForKey:@"cmdSend"];
    NSString *timeSend = [applicationContext objectForKey:@"timeSend"];
    NSString *distanceSend = [applicationContext objectForKey:@"distanceSend"];
    NSString *partielSend = [applicationContext objectForKey:@"partielSend"];
    NSString *vitesseSend = [applicationContext objectForKey:@"vitesseSend"];

    dispatch_async(dispatch_get_main_queue(), ^{
        if([cmdSend isEqual: @"start"])
        {
            [self.startLabel setTitle:[NSString stringWithFormat:@"PAUSE"]];
            [self.timeLabel setText:[NSString stringWithFormat:@"Time : %@", timeSend]];
            [self.distanceLabel setText:[NSString stringWithFormat:@"Distance : %@", distanceSend]];
            [self.vitesseLabel setText:[NSString stringWithFormat:@"Vitesse : %@", vitesseSend]];
            self.running = true;
        }
        else if([cmdSend isEqual: @"pause"])
        {
            [self.startLabel setTitle:[NSString stringWithFormat:@"PLAY"]];
            [self.timeLabel setText:[NSString stringWithFormat:@"Time : %@", timeSend]];
            [self.distanceLabel setText:[NSString stringWithFormat:@"Distance : %@", distanceSend]];
            [self.vitesseLabel setText:[NSString stringWithFormat:@"Vitesse : %@", vitesseSend]];
            self.running = false;
        }
        else if([cmdSend isEqual: @"stop"])
        {
            [self.startLabel setTitle:[NSString stringWithFormat:@"PLAY"]];
            [self.timeLabel setText:[NSString stringWithFormat:@"Time : %@", timeSend]];
            [self.distanceLabel setText:[NSString stringWithFormat:@"Distance : %@", distanceSend]];
            [self.vitesseLabel setText:[NSString stringWithFormat:@"Vitesse : %@", vitesseSend]];
            self.running = false;
        }
    });

}

@end

感谢您的帮助

1 个答案:

答案 0 :(得分:0)

我建议您使用UIApplicationDelegate和ExtensionDelegate创建一个新对象的实例,即WCSessionDelegate。此对象将传入数据保留到磁盘,并发布已更新后备数据存储的通知。然后,每个UI组件都可以监视这些通知并根据需要更新其UI。