如何将数组数据从 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 应用之间共享数据。
参考
- Share Data in your Swift WatchKit Apps with App Groups
- WATCHKIT: BEST PRACTICES FOR SHARING DATA BETWEEN YOUR WATCH AND IOS APP
编辑:
在 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 应用程序:
NSUserDefaults with suite: 您可以将 NSUserDefaults 与您的应用程序和扩展之间共享的虚拟 "box" 数据一起使用。只需使用 -[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: Darwin 通知的包装器,但具有类似于 WKInterfaceController/AppDelegate 中使用的简单通信模型。您可以在 GitHub.
上找到该库
请注意,您首先需要在您的 Apple Developer 帐户中注册一个 App Group Identifier。
有关 watchOS 2.0 和 3.0 的更新,请参阅 documentation。
我必须开发一个 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 应用之间共享数据。
参考
- Share Data in your Swift WatchKit Apps with App Groups
- WATCHKIT: BEST PRACTICES FOR SHARING DATA BETWEEN YOUR WATCH AND IOS APP
编辑:
在 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 应用程序:
NSUserDefaults with suite: 您可以将 NSUserDefaults 与您的应用程序和扩展之间共享的虚拟 "box" 数据一起使用。只需使用
-[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: Darwin 通知的包装器,但具有类似于 WKInterfaceController/AppDelegate 中使用的简单通信模型。您可以在 GitHub.
上找到该库
请注意,您首先需要在您的 Apple Developer 帐户中注册一个 App Group Identifier。
有关 watchOS 2.0 和 3.0 的更新,请参阅 documentation。