如何从watchOS 2唤醒iPhone应用程序? [英] How to wake up iPhone app from watchOS 2?

查看:151
本文介绍了如何从watchOS 2唤醒iPhone应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序具有非常丰富的网络层,我的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-App应该在后台任务中启动。所以以下内容对我很有帮助:

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];

将此添加到会话:didReceiveMessage: session:didReceiveMessageData:以3分钟超时启动后台任务的方法。

Add this to your session:didReceiveMessage: or session:didReceiveMessageData: method to start a background task with a three minute timeout.

这篇关于如何从watchOS 2唤醒iPhone应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆