如何将数组数据从 iphone 传递到 objective c 中的 Apple Watch

How to pass an array data from iphone to apple watch in objective c

我必须开发一个 Apple Watch 应用程序,我必须在其中显示 Apple Watch 中的一些表格视图。对于此操作,我的 iPhone 中已经有核心数据,我从中检索到 NSArray 对象。 但现在我想将它传递给手表套件扩展,这怎么可能? 有人知道吗?

下面是returnsCore date以数组对象的形式获取记录的函数

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

如果您使用 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];

在 watchOS 1.0 下,可以通过三种方式将数据从您的 iPhone 应用程序交换到 Apple Watch 应用程序:

请注意,您首先需要在您的 Apple Developer 帐户中注册一个 App Group Identifier。

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