0

I create an iPhone application (with a view) / Watch (with interface) that displays speed, distance and a timer with a Play / Pause, Stop and Clear.

I used to share WatchConnectivity Application Context data between (Send and receipt of Context). Everything works so far, but I would add another page / interfaces on which exchanges Watch the Context with the iPhone and there I do not know at all how.

My Interface Storyboard

If I turn on the Session 2 interfaces, information is lost

Here is my current code, I want to toggle the display of labels TimeLabel, distanceLabel Label and speed on the second interface

//
//  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

Thanks for your help

Arno.R
  • 71
  • 1
  • 6

1 Answers1

0

I'd suggest having your UIApplicationDelegate and ExtensionDelegate create an instance of a new object you make that is the WCSessionDelegate. This object will persist incoming data to disk, and post notifications that the backing data store has been updated. Then each of the UI components can monitor for these notifications and update their UIs as appropriate.

ccjensen
  • 4,578
  • 2
  • 23
  • 25