我遇到了一个问题,如果我在 iPhone 模拟器之前在 Watch 模拟器中运行我的应用程序,Watch 应用程序中没有数据。
这是因为我从 iPhone 应用程序获取 Watch 数据(我在其中查询 Parse
并将结果保存到 Watch 可以访问它的 Group Defaults),因为从我相信我们被告知无法在 Watch 应用(扩展)中运行此类功能。
我对此有误吗?
如果 iPhone 应用程序尚未运行,我应该如何在第一次运行时填充 Watch 应用程序?
iPhone TableViewController.m
:
- (void)viewDidLoad {
PFQuery *query = [self queryForTable];
// ... Query Parse to find data, then save it App Group...
NSString *container = @"group.com.me.off";
NSUserDefaults *defaults = [[NSUserDefaults alloc] initWithSuiteName:container];
// ... finish saving to App Group
}
观看“InterfaceController.m”:
- (void)awakeWithContext:(id)context {
[super awakeWithContext:context];
// Initialize Parse.
[Parse setApplicationId:@"xxx"
clientKey:@"xxx"];
// GMT Date from Phone
NSDate *gmtNow = [NSDate date];
NSLog(@"GMT Now: %@", gmtNow);
// Query Parse
PFQuery *query = [PFQuery queryWithClassName:@"na"];
[query whereKey:@"dateGame" greaterThanOrEqualTo:gmtNow];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (!error) {
NSMutableArray *localMatchup = [@[] mutableCopy];
for (PFObject *object in objects) {
// Add objects to local Arrays
[localMatchup addObject:[object objectForKey:@"matchup"]];
// App Group
NSString *container = @"group.com.me.off";
NSUserDefaults *defaults = [[NSUserDefaults alloc] initWithSuiteName:container];
// Matchup
[defaults setObject:localMatchup forKey:@"KeyMatchup"];
NSArray *savedMatchup = [defaults objectForKey:@"KeyMatchup"];
NSLog(@"Default Matchup: %@", savedMatchup);
savedMatchup = self.matchupArray;
}
dispatch_async(dispatch_get_main_queue(), ^{
NSLog(@"Say you went to dispatch");
});
}
}];
// App Group
NSString *container = @"group.com.me.off";
NSUserDefaults *defaults = [[NSUserDefaults alloc] initWithSuiteName:container];
// Matchup
NSArray *savedMatchup = [defaults objectForKey:@"KeyMatchup"];
self.matchupArray = savedMatchup;
}
- (void)willActivate {
[super willActivate];
// Hide "No Games" Label
if ([self.matchupArray count] > 0) {
self.noGamesLabel.hidden = YES;
self.rowTable.hidden = NO;
[self.rowTable setNumberOfRows:[self.matchupArray count] withRowType:@"rows"];
for (NSInteger index = 0; index < [self.timeArray count]; index++) {
// Matchup
NSString *matchupString = [self.matchupArray objectAtIndex:index];
RowController *controller = [self.rowTable rowControllerAtIndex:index];
[controller.matchupLabel setText:matchupString];
}
}
// Show "No Games" Label
else {
self.rowTable.hidden = YES;
self.noGamesLabel.hidden = NO;
}
}
最佳答案
您可以从扩展程序中查询 Parse,并且可以写入组默认值。您的扩展程序可以像您的 iOS 应用程序一样发出 URL 请求。您需要小心,不要在查询运行时阻塞主线程。您需要有一些初始 UI 来向用户显示您正在查询初始数据,以便应用程序看起来不会卡住。
关于ios - 如果 Watch 应用程序在 iPhone 应用程序运行之前打开,则不会填充任何数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28998918/