我正在研究苹果手表应用程序,但面临一个奇怪的问题,即我的手表应用程序仅在我手动打开iPhone应用程序或iPhone应用程序处于后台时才有效。当我终止我的iPhone应用程序并测试苹果手表应用程序时,它就不再起作用了。
这里我提到了观看应用流程:
openParentApplication:reply:
方法从父应用以下是附件摘录:
观看应用 - InitialInterfaceController.m
- (void)awakeWithContext:(id)context {
[super awakeWithContext:context];
}
- (void)willActivate {
[super willActivate];
[self getDetails];
}
- (void)getDetails{
//Open parent app
[WKInterfaceController openParentApplication:@{@“request”:@“details”}
reply:^(NSDictionary *replyInfo, NSError *error) {
if (!error) {
NSLog(@“Success”);
[self parseKPI:replyInfo];
}
else{
NSLog(@"Error - %@", error.localizedDescription);
}
}];
}
iPhone App - AppDelegate.m
- (void)application:(UIApplication *)application
handleWatchKitExtensionRequest:(NSDictionary *)userInfo
reply:(void (^)(NSDictionary *))reply{
NSString *request = [userInfo objectForKey:@“request”];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{
// Get Details
if ([request isEqualToString:@"details"]) {
APIHandler *api = [[APIHandler alloc] init];
[api getDetailsUsername:@“my_user_name”
onSuccess:^(NSDictionary *details) {
dispatch_async(dispatch_get_main_queue(), ^{
reply(details);
});
} onFailure:^(NSString *message) {
dispatch_async(dispatch_get_main_queue(), ^{
reply(@{@"Error":message});
});
}];
}
}
iPhone App - APIHandler.m
- (void) getDetailsUsername:(NSString *)username
onSuccess:(void(^)(NSDictionary * details))success
onFailure:(void(^)(NSString *message))failure{
NSString *urlString = [NSString stringWithFormat:@"%@%@", HOST, DETAILS_API];
urlString = [urlString stringByAppendingFormat:@"?username=%@",username];
urlString = [urlString stringByAppendingFormat:@"&%@", self.APIKeyParameter];
NSURL *url = [NSURL URLWithString:urlString];
NSMutableURLRequest *mutableURL = [NSMutableURLRequest requestWithURL:url];
[NSURLConnection sendAsynchronousRequest:mutableURL
queue:[NSOperationQueue mainQueue]
completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
if (!connectionError) {
NSError *error = nil;
NSDictionary *details = [NSJSONSerialization JSONObjectWithData:data
options:NSJSONReadingMutableLeaves
error:&error];
success(details);
}
else{
failure(@"Connection Error!");
}
}];
}
但这种做法对我不起作用。
我在手表应用模拟器中发现了另外一个问题,即
调用我的初始视图控制器- (void)awakeWithContext:(id)context
,但有时不调用- (void)willActivate
方法,我只看到app app spinner。有时候它有效。这很奇怪。我使用故事板添加了初始接口控制器中的大约15个控件(包括所有组)。
我还提到Watchkit not calling willActivate method并修改了我的代码,但仍面临同样的问题。
任何人都可以告诉我为什么这个问题会在我的应用中持续存在?