我创建了一个 iPhone 应用程序(带视图)/手表(带界面),它显示速度、距离和带有播放/暂停、停止和清除的计时器。
我曾经在(发送和接收上下文)之间共享 WatchConnectivity 应用程序上下文数据。到目前为止一切正常,但我会添加另一个页面/界面,在这些页面/界面上与 iPhone 交换 Watch the Context,我根本不知道如何。
如果我打开 Session 2 接口,信息会丢失
这是我当前的代码,我想在第二个界面上切换标签TimeLabel,distanceLabel标签和速度的显示
//
// 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
谢谢你的帮助