gpt4 book ai didi

ios - 如何在 Objective C 中将数组数据从 iPhone 传递到 Apple Watch

转载 作者:行者123 更新时间:2023-11-29 01:35:48 26 4
gpt4 key购买 nike

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

下面的函数以数组对象的形式返回核心日期获取的记录。

-(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,则可以通过应用组在您的 watch 和 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];

关于ios - 如何在 Objective C 中将数组数据从 iPhone 传递到 Apple Watch,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32987805/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com