我必须开发一个苹果手表应用程序,我必须在苹果手表中显示一些表格视图。对于这个操作,我已经在我的iPhone中拥有Core数据,我将从中检索到NSArray对象。 但是现在我想将它传递到手表套件扩展部分,那么它可能如何? 有人有想法吗?
下面是以数组对象的形式返回Core日期获取记录的函数。
-(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;
}
答案 0 :(得分:1)
如果您使用watchOS 1,则可以使用应用程序组在手表和iOS应用程序之间共享数据。
参考文献
修改强>
在iphone端,序列化您的数据。
NSData *encodedObject = [NSKeyedArchiver archivedDataWithRootObject:resultTracks];
NSUserDefaults *defaults = [[NSUserDefaults alloc]
initWithSuiteName:@"group.com.example.mygroup"];
[defaults setObject:encodedObject forKey:@"WatchHomeViewTableList"];
[defaults synchronize];
并对您的数据进行反序列化。
NSUserDefaults *defaults = [[NSUserDefaults alloc]
initWithSuiteName:@"group.com.example.mygroup"];
NSData *encodedObject = [defaults objectForKey:@"WatchHomeViewTableList"];
NSMutableArray *resultTracks = [NSKeyedUnarchiver unarchiveObjectWithData:encodedObject];
答案 1 :(得分:0)
在使用App Groups的watchOS 1.0下,有三种方法可以将iPhone应用程序中的数据交换到Apple Watch应用程序:
NSUserDefaults with suite:您可以将NSUserDefaults与您的应用和扩展程序之间共享的虚拟“盒子”数据一起使用。只需使用方法-[NSUserDefaults initWithSuiteName:@"myAppGroupIdentifier"]
创建一个新的NSUserDefaults实例,并将其用作iOS提供的共享用户默认值。 https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSUserDefaults_Class/
Darwin通知: observer 模式的低级实现,与NSNotificationCenter中使用的模式类似。这有点难以使用,所以我建议使用MMWormhole。 https://developer.apple.com/library/mac/documentation/Darwin/Conceptual/MacOSXNotifcationOv/DarwinNotificationConcepts/DarwinNotificationConcepts.html
MMWormhole:用于达尔文通知的包装器,但是具有类似于WKInterfaceController / AppDelegate中使用的简单通信模型。你可以在GitHub上找到这个库。
请注意,您首先需要在Apple Developer帐户中注册应用程序组标识符。
有关watchOS 2.0和3.0的更新,请参阅documentation。