【问题标题】:Apple watch app does not work when iPhone app is in terminated state?iPhone应用程序处于终止状态时Apple Watch应用程序不起作用?
【发布时间】:2015-04-29 08:44:35
【问题描述】:

我正在开发一款 Apple Watch 应用,但遇到了一个奇怪的问题,即我的手表应用仅在我手动打开 iPhone 应用或 iPhone 应用处于后台时才有效。当我终止我的 iPhone 应用并测试 Apple Watch 应用时,它就不再工作了。

这里我说的是手表应用流程:

  • 当 Apple Watch 应用启动时,我调用 Web api 从服务器获取响应。
  • 我使用openParentApplication:reply:方法从父应用调用web api
  • 我明白,我将不得不在后台线程中调用 web api 方法,因为 openParentApplication:reply: 方法会自动在 iPhone 中打开父应用程序并同时暂停,所以如果我们正在处理一个耗时任务使用这种方法那么我们应该使用WatchKit Development Tips 中提到的后台线程。所以我使用后台线程来调用 web api。
  • 收到回复后,我会将其传递给观看应用。

这是附上的sn-p:

手表应用 - 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!");
                                   }
                               }];
}

但是这种方法对我不起作用。

我在手表应用模拟器中发现了另一个问题,即 - (void)awakeWithContext:(id)context 用于我的初始视图控制器,但有时不会调用 - (void)willActivate 方法,我只看到手表应用程序微调器。有时它会起作用。这很奇怪。在使用情节提要添加的初始界面控制器中,我有大约 15 个控件(包括所有组)。

我还提到了Watchkit not calling willActivate method 并修改了我的代码,但仍然面临同样的问题。

谁能告诉我为什么这个问题在我的应用程序中仍然存在?

【问题讨论】:

    标签: ios objective-c iphone watchkit apple-watch


    【解决方案1】:

    您需要稍微不同地处理 iPhone 应用程序中的请求,以免您的应用程序被操作系统杀死。我在这里分享了一个类似的答案:https://stackoverflow.com/a/29848521/3704092

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-08
      • 1970-01-01
      • 2010-12-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多