i have to develop an apple watch application in which i have to show some tabular view in the apple watch . For this operation i have already Core data in my iPhone from which i get retrieved in to a NSArray object. But now i want to pass it to the watch kit extension so how its possible? is any one have idea ?
Below is the function which returns the Core date fetched records in the form of array objects.
-(NSMutableArray *) getWatchHomeView
{
NSMutableArray *resultTracks = [[NSMutableArray alloc] init];
self.ongoingMapArray = [NSArray arrayWithArray:[[self fetchResultsForCompletedExpeditions:NO] fetchedObjects]];
NSLog(@"ongoingMapArray-- %lu",(unsigned long)[self.ongoingMapArray count]);
self.completedMapArray = [NSArray arrayWithArray:[[self fetchResultsForCompletedExpeditions:YES] fetchedObjects]];
NSLog(@"completedMapArray-- %lu",(unsigned long)[self.completedMapArray count]);
for (int i=0; i < self.completedMapArray.count; i++)
{
WatchTable *watchTableRow = [[WatchTable alloc] init];
Map *mapObject = [self.completedMapArray objectAtIndex:i];
watchTableRow.trackName = [[NSString stringWithFormat:@"%@", [mapObject name]] uppercaseString];
NSArray *arrPolylines = [NSArray arrayWithArray:[[self fetchPloylineForMaps:[mapObject name]] fetchedObjects]];
if ([arrPolylines count] > 0) {
double totalDis = [self getTotalDistanceFromPolylines:arrPolylines];
watchTableRow.trackedDistance = [NSString stringWithFormat:@"%.2f km", totalDis];
Polyline *firstPolyline = [arrPolylines lastObject];
NSMutableArray *arrTimeData = (NSMutableArray*)firstPolyline.time;
if ([arrTimeData count] > 0) {
watchTableRow.trackedTime = [NSString stringWithFormat:@"%@ ago", [self getPausedTimeWithCreationDate:[arrTimeData lastObject]]];
}else{
watchTableRow.trackedTime = [NSString stringWithFormat:@"%@ ago", [self getPausedTimeWithCreationDate:firstPolyline.creationDate]];
}
}else{
watchTableRow.trackedTime = [NSString stringWithFormat:@"%@ ago", [self getPausedTimeWithCreationDate:mapObject.creationDate]];
watchTableRow.trackedDistance = [NSString stringWithFormat:@"0.00 Km"];
}
NSLog(@"watchTableRow = %@",watchTableRow);
[resultTracks addObject:watchTableRow];
}
[[NSUserDefaults standardUserDefaults] setObject:resultTracks forKey:@"WatchHomeViewTableList"];
return resultTracks;
}
If you use watchOS 1, you can share data between your watch and iOS App with App Groups.
ref.
EDIT:
On the iphone side, serialize your data.
NSData *encodedObject = [NSKeyedArchiver archivedDataWithRootObject:resultTracks];
NSUserDefaults *defaults = [[NSUserDefaults alloc]
initWithSuiteName:@"group.com.example.mygroup"];
[defaults setObject:encodedObject forKey:@"WatchHomeViewTableList"];
[defaults synchronize];
And unserialize your data.
NSUserDefaults *defaults = [[NSUserDefaults alloc]
initWithSuiteName:@"group.com.example.mygroup"];
NSData *encodedObject = [defaults objectForKey:@"WatchHomeViewTableList"];
NSMutableArray *resultTracks = [NSKeyedUnarchiver unarchiveObjectWithData:encodedObject];