我可以在AppleWatch上使用与"applicationWillTerminate()"类似的功能吗? [英] Is there a similar function to "applicationWillTerminate()", i can use on AppleWatch?

查看:94
本文介绍了我可以在AppleWatch上使用与"applicationWillTerminate()"类似的功能吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在AppleWatch App崩溃之前,我需要将数据保存在NSUserDefaults中.否则,iOS App会认为AppleWatch App仍在运行,并且无法正常工作.

I need to save data in NSUserDefaults before the AppleWatch App will crash. Otherwise the iOS App will think the AppleWatch App is still running and thats not working.

推荐答案

我认为,您的基本目标是要确保包含应用程序"在其Apple WatchKit App Extension崩溃时能够获知.

Your underlying goal, I think, is that you want to ensure that the Containing App is aware when its Apple WatchKit App Extension has crashed.

您可以通过获取Watchkit Extension定期发送包含应用程序订阅的Darwin通知来实现此目的.然后,当包含的应用在某个超时限制内未从扩展收到消息时,它知道WatchKit扩展崩溃了.这是我所知道的最轻巧的方法.

You can achieve this by getting the Watchkit Extension to periodically send out a Darwin Notification which the containing app subscribes to. Then when the containing app does not get a message from the extension within some timeout limit, it knows the WatchKit extension crashed. This is the most lightweight way I know to achieve this.

另一种方法是使WatchKit应用程序扩展程序定期调用:

Another way is to make the WatchKit app extension periodically call:

+ (BOOL)openParentApplication:(NSDictionary *)userInfo
         reply:(void (^)(NSDictionary *replyInfo,
                         NSError *error))reply

它不那么轻巧,但是具有的优点是允许您提供一些字典数据,例如时间戳记值或序列号.

It is not as lightweight, but has the advantage of allowing you to supply some dictionary data, such as a timestamp value or sequence number.

这篇关于我可以在AppleWatch上使用与"applicationWillTerminate()"类似的功能吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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