如何在目标c中将数组数据从iphone传递到apple watch

时间:2015-10-07 08:51:27

标签: ios objective-c iphone watchkit apple-watch

我必须开发一个苹果手表应用程序,我必须在苹果手表中显示一些表格视图。对于这个操作,我已经在我的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;
}

2 个答案:

答案 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应用程序:

请注意,您首先需要在Apple Developer帐户中注册应用程序组标识符。

有关watchOS 2.0和3.0的更新,请参阅documentation