Sharing sensor data between watch and phone

I'm new to Swift and iOS development and I'm looking for the best/most convenient way to make sensor data gathered from an Apple Watch available in its corresponding iPhone app.


I will gather data from the watch's accelerometer, gyroscope and magnetometer using Core Motion. When the measurement is finished, all data should be available from the iPhone app. Not sure what's the best way to go about this. Should I store the data locally on the watch, e.g. using Core Data, and then send it over to the phone using WatchConnectivity, or is it possible to have a shared database between the watchOS app and iOS app?


I'm using XCode Version 10.1 and Swift 4.2.

Replies

I wrote my app many WatchOs's ago. There may be a recently enabled way of sharing a variant of NSUserDefaults. I used updateApplicationContext in WatchConnectivity. It works nicely in the foreground and also in the background, if the iPhone is not connected. Here's the (one-way; symmetrize for 2-way) code:


//  on the Watch:  
#import <WatchConnectivity/WatchConnectivity.h>  
//  global variable
WCSession wcsession;

if ([WCSession isSupported]) {
    if(!wcsession){
        wcsession = [WCSession defaultSession];
        wcsession.delegate=self;
    }
    if([wcsession activationState]!=WCSessionActivationStateActivated)
        [wcsession activateSession];
    NSString *newKey=[NSString stringWithFormat:@"%f",[NSDate timeIntervalSinceReferenceDate]];          
    NSDictionary *theInfo=[NSDictionary dictionaryWithObjectsAndKeys:
             theEvents,@"TheEvents",
             @"dummy variable with unique newKey",newKey,nil];
              //  all transfers have a unique key
              //      - this forces WatchConnectivity to push the transfer immediately
    [wcsession updateApplicationContext:theInfo error:nil];
}



//on the iPhone:
#import <WatchConnectivity/WatchConnectivity.h>
// global variable 
WCSession theSession;
//in didFinishLaunchingWithOptions:
[self activateASession];


-(void)activateASession{
    if ([WCSession isSupported]) {
        theSession = [WCSession defaultSession];
        theSession.delegate = self;
        if([theSession activationState]!=WCSessionActivationStateActivated){
            [theSession activateSession];
        }
    }
}

-(void)sessionDidDeactivate:(WCSession *)session{
    [self activateASession];
}

//  need these for some reason:
-(void)sessionDidBecomeInactive:(WCSession *)session{
    }
-(void)session:(WCSession *)session activationDidCompleteWithState:(WCSessionActivationState)activationState error:(nullable NSError *)error{    
}

-(void)session:(WCSession *)session didReceiveApplicationContext:userInfo{
      dispatch_async(dispatch_get_main_queue(), ^{
        if([userInfo objectForKey:@"TheEvents"]){
            if([[userInfo objectForKey:@"TheEvents"] count]>0){//  new events were sent
                NSArray *theNewEvents=[userInfo objectForKey:@"TheEvents"];
                  //  process theNewEvents
            }
        }
     });
}


    

Thank you for sharing! 🙂 Seems like WatchConnectivity is the (only) way to go to share data between the watch and phone.