问题描述
我有一个应用程序具有非常丰富的网络层,而我的 Apple Watch 应用程序依赖于所有型号.遗憾的是,该应用的模块化程度不够高,无法在手表应用中使用此层.
I have an app that has a very rich network layer and my apple watch app depends on all the models. Unfortunately the app is not modular enough to make this layer available in the watch app.
我通过使用 openParentApplication 解决了这个问题:唤醒 iPhone 应用程序,执行请求并返回结果.
I solved this problem by using openParentApplication: to wake up the iPhone app, perform the request and give back the results.
在 watchOS 2 中,此方法已消失,我应该使用 WatchConnectivity.使用它的最佳方法是发送 userInfo 字典.
In watchOS 2 this method is gone and I should use WatchConnectivity. The best way to use this would be by sending userInfo dictionaries.
但是我怎样才能唤醒 iPhone 应用来处理我的请求呢?要获得有关新 userInfos 的通知,我必须使用 WCSessionDelegate,为此我需要一个 WCSession 对象.但是我什么时候应该创建它?以及如何唤醒应用程序?
But how can I wake up the iPhone app to handle my requests? To get notifications about new userInfos I have to use the WCSessionDelegate and for that I need a WCSession object. But when should I create that? And how to wake up the app?
推荐答案
我向 Apple 工程师询问了这个问题并得到以下提示:iOS 应用程序应该在后台任务中启动.所以以下对我来说效果很好:
I asked an Apple Engineer about this and got the following tip: The iOS-App should be started in a background-task. So the following worked for me pretty well:
UIApplication *application = [UIApplication sharedApplication];
__block UIBackgroundTaskIdentifier identifier = UIBackgroundTaskInvalid;
dispatch_block_t endBlock = ^ {
if (identifier != UIBackgroundTaskInvalid) {
[application endBackgroundTask:identifier];
}
identifier = UIBackgroundTaskInvalid;
};
identifier = [application beginBackgroundTaskWithExpirationHandler:endBlock];
将此添加到您的 session:didReceiveMessage:
或 session:didReceiveMessageData:
方法中,以启动具有三分钟超时的后台任务.
Add this to your session:didReceiveMessage:
or session:didReceiveMessageData:
method to start a background task with a three minute timeout.
这篇关于如何从 watchOS 2 唤醒 iPhone 应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!