How to share data between iPhone and applewatch?

I have an issue with sharing data from iPhone app to apple watch. I tried below code to split NSMutablearray on apple watch but it doesn't work.

NSData *encodedObject = [NSKeyedArchiver archivedDataWithRootObject:arrStartScore];
NSUserDefaults *defaults = [[NSUserDefaults alloc] initWithSuiteName:@"group.com.test.StartScore"];
[defaults setObject:encodedObject forKey:@"WatchHomeViewTableList"];
[defaults synchronize];

      

To get data for viewing in apple

NSUserDefaults *myDefaults = [[NSUserDefaults alloc]                                 initWithSuiteName:@"group.com.test.StartScoreCheck"];

arrStartScore = [myDefaults objectForKey:@"WatchHomeViewTableList"];
NSLog(@"dict....%@",arrStartScore);

      

+3


source to share


3 answers


To send data from your phone for viewing, use this code.

WCSession *session = [WCSession defaultSession];
NSError *error;

[session updateApplicationContext:@{@"message": yourArray} error:&error];

      

Get data from phone on watch:



 - (void) session:(nonnull WCSession *)session didReceiveApplicationContext:(nonnull NSDictionary<NSString *,NSMutableArray *> *)applicationContext {   
 }

      

you can access your array in doReceiveApplicationContext file using

  [applicationContext objectForKey:@"message"];

      

+5


source


watchOS3 has separated UserDefaults on watches and phones. AppGroups can share data among the same developer apps on the same device. To transfer data between devices, use the WatchConnectivity Framework.



Documentation

+1


source


You can exchange data using the WatchKit Connectivity Framework. There are various ways to use background or interactive.

Check out this video and it explains all the different ways to communicate between iPhone and Watch.

In addition, you can create a singleton class for connecting watches and use it in both iOS and WatchOS. Set up a session on both device and delegate method integration and you're done. You can now send and receive messages back and forth from iOS and WatchOS.

+1


source







All Articles