5

我正在实施苹果手表。在我的 Watchkit 扩展中,我使用该方法与“主应用程序”进行通信。

    [WKInterfaceController openParentApplication:applicationData reply:^(NSDictionary *replyInfo, NSError *error) {}];

根据苹果文档,该应用程序可以在后台处理请求。

When you call the openParentApplication:reply: method, iOS launches or 
wakes up the parent app in the background and calls the
application:handleWatchKitExtensionRequest:reply:method of its app delegate.

但是我的应用程序总是处于活动状态,即使我在方法中没有代码,handleWatchKitExtensionRequest

如果可能的话,有什么建议吗?

提前致谢

4

4 回答 4

2

根据 Apple dev 在 Apple Developer Forum 上的官方回复,这是 WatchKit Framework Beta 2 中的错误。

https://devforums.apple.com/message/1082689#1082689

为了明确一件事,iPhone 应用程序是在后台启动的。在模拟器中,当前应用程序正在前台启动。这将不是设备上的体验。该文档特别指出,“调用该方法会导致 iOS 在后台启动应用程序......”。

(职位编号 6)

顺便说一句,昨天发布了 Beta 3,也许它已经修复了。

更重要的是,如果 iPhone 将被锁定,您的 iOS 应用程序也将在后台启动。

于 2014-12-19T09:40:24.690 回答
2

If your app is not active, openParentApplication cannot activate it. It can only perform tasks in the background. For this to work, it is important that you start a background task in handleWatchKitExtensionRequest as specified in the documentation. This ensures that the main app on the iPhone is not suspended before it can send its reply.

Code in the app delegate of the main app on iPhone:

- (void)application:(UIApplication *)application handleWatchKitExtensionRequest:(NSDictionary *)userInfo reply:(void ( ^)( NSDictionary * ))reply 
{
   __block UIBackgroundTaskIdentifier watchKitHandler;
   watchKitHandler = [[UIApplication sharedApplication]
          beginBackgroundTaskWithName:@"backgroundTask"
                    expirationHandler:^{
              watchKitHandler = UIBackgroundTaskInvalid;
          }];

   if ([[userInfo objectForKey:@"request"] isEqualToString:@"getData"]) {
      // get data
      // ...
      reply( data );
   }

   dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)NSEC_PER_SEC * 1), dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
       [[UIApplication sharedApplication] endBackgroundTask:watchKitHandler];
   });
}
于 2015-05-02T09:42:42.500 回答
1

我认为主应用程序的“功能”部分缺少后台模式,将来会添加。

我尝试打开所有当前支持的后台模式,但它们都不起作用(即主应用程序总是在启动)。

我的提示 - 耐心等待下一个测试版。

于 2014-12-18T19:41:49.023 回答
0

I had this exact same problem bolting WatchKit onto an existing app. I had UIApplicationExitsOnSuspend (aka Application does not run in background) set to YES in the info.plist. Change it to NO and the error goes away.

于 2015-03-19T18:10:40.293 回答