有没有一种方法可以在Apple Watch的后台每天运行一次代码?

时间:2023-01-22 17:37:43

I want to add a Complication to my Watch app that shows information retrieved from the internet. This information changes every day.

我想给我的手表应用程序添加一个复杂的东西,它显示从互联网检索到的信息。这些信息每天都在变化。

I need to find a way to retrieve this information without having to open the Watch app or iOS app daily.

我需要找到一种方法来检索这些信息,而不需要每天打开Watch应用或iOS应用。

I know running code in the background isn't really allowed on iOS, but it seems like a basic necessity for running Complications on the Watch, so maybe it's allowed on WatchOS?

我知道在后台运行代码在iOS上是不允许的,但它似乎是在手表上运行复杂程序的基本必要条件,所以它可能在WatchOS上被允许?

Does anybody know how to do this?

有人知道怎么做吗?

2 个解决方案

#1


1  

Have a look at getNextRequestedUpdateDateWithHandler: in the ComplicationController. It allows you to request the next time you would like the complication code to be woken up. It will wake up in requestedUpdateDidBegin at the approximate time you request (within some limits, 24 hours should be fine.)

查看一下getNextRequestedUpdateDateWithHandler:在同谋控制器中。它允许您在下次希望复杂代码被唤醒时请求。它将在requestedUpdateDidBegin中按您要求的大约时间醒来(在某些限制下,24小时应该没问题)。

#2


0  

The first time the watch is active you could request the data from your server, update your internal data and save internally that the information of the current day is read. In this way you can avoid the background activity. You should check only the time (and date) when you activate your WatchApp.

第一次手表处于活动状态时,您可以从服务器请求数据,更新内部数据,并在内部保存当前日期的信息。这样可以避免后台活动。你应该只检查你激活WatchApp的时间(和日期)。

#1


1  

Have a look at getNextRequestedUpdateDateWithHandler: in the ComplicationController. It allows you to request the next time you would like the complication code to be woken up. It will wake up in requestedUpdateDidBegin at the approximate time you request (within some limits, 24 hours should be fine.)

查看一下getNextRequestedUpdateDateWithHandler:在同谋控制器中。它允许您在下次希望复杂代码被唤醒时请求。它将在requestedUpdateDidBegin中按您要求的大约时间醒来(在某些限制下,24小时应该没问题)。

#2


0  

The first time the watch is active you could request the data from your server, update your internal data and save internally that the information of the current day is read. In this way you can avoid the background activity. You should check only the time (and date) when you activate your WatchApp.

第一次手表处于活动状态时,您可以从服务器请求数据,更新内部数据,并在内部保存当前日期的信息。这样可以避免后台活动。你应该只检查你激活WatchApp的时间(和日期)。