当 iPhone 应用程序处于终止状态时,Apple Watch 应用程序无法运行?
Apple watch app does not work when iPhone app is in terminated state?
我正在开发 Apple Watch 应用程序,但遇到了一个奇怪的问题,即我的手表应用程序仅在我手动打开 iPhone 应用程序或 iPhone 应用程序在后台运行时才能运行。当我终止我的 iPhone 应用程序并测试 apple watch 应用程序时,它就不再工作了。
这里我说的是手表应用流程:
- 当 apple watch 应用程序启动时,我调用网络 api 从服务器获取响应。
- 我使用
openParentApplication:reply:
方法从父应用程序调用网络 api
- 我知道,我必须在后台线程中调用 web api 方法,因为 openParentApplication:reply: 方法会自动打开 iPhone 中的父应用程序并暂停时间,因此如果我们正在使用此方法处理耗时任务,那么我们应该使用 WatchKit Development Tips 中提到的后台线程。所以我正在使用后台线程调用 web api.
- 收到回复后,我将其传递给手表应用程序。
这是附加的片段:
观看应用程序 - 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 应用程序 - 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 应用程序 - 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!");
}
}];
}
但这种方法对我不起作用。
我在 watch app simulator 中又发现了一个问题,即
调用了我的初始视图控制器的 - (void)awakeWithContext:(id)context
,但有时未调用 - (void)willActivate
方法,我只看到手表应用程序微调器。有时它有效。这很奇怪。我在使用故事板添加的初始界面控制器中有大约 15 个控件(包括所有组)。
我也参考了 Watchkit not calling willActivate method 并修改了我的代码,但仍然面临同样的问题。
任何人都可以告诉我为什么这个问题在我的应用程序中持续存在吗?
您需要以稍微不同的方式处理 iPhone 应用程序中的请求,以免您的应用程序被 OS 终止。我在这里分享了一个类似的答案:
我正在开发 Apple Watch 应用程序,但遇到了一个奇怪的问题,即我的手表应用程序仅在我手动打开 iPhone 应用程序或 iPhone 应用程序在后台运行时才能运行。当我终止我的 iPhone 应用程序并测试 apple watch 应用程序时,它就不再工作了。
这里我说的是手表应用流程:
- 当 apple watch 应用程序启动时,我调用网络 api 从服务器获取响应。
- 我使用
openParentApplication:reply:
方法从父应用程序调用网络 api - 我知道,我必须在后台线程中调用 web api 方法,因为 openParentApplication:reply: 方法会自动打开 iPhone 中的父应用程序并暂停时间,因此如果我们正在使用此方法处理耗时任务,那么我们应该使用 WatchKit Development Tips 中提到的后台线程。所以我正在使用后台线程调用 web api.
- 收到回复后,我将其传递给手表应用程序。
这是附加的片段:
观看应用程序 - 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 应用程序 - 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 应用程序 - 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!");
}
}];
}
但这种方法对我不起作用。
我在 watch app simulator 中又发现了一个问题,即
调用了我的初始视图控制器的 - (void)awakeWithContext:(id)context
,但有时未调用 - (void)willActivate
方法,我只看到手表应用程序微调器。有时它有效。这很奇怪。我在使用故事板添加的初始界面控制器中有大约 15 个控件(包括所有组)。
我也参考了 Watchkit not calling willActivate method 并修改了我的代码,但仍然面临同样的问题。
任何人都可以告诉我为什么这个问题在我的应用程序中持续存在吗?
您需要以稍微不同的方式处理 iPhone 应用程序中的请求,以免您的应用程序被 OS 终止。我在这里分享了一个类似的答案: