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
}
}
});
}