iOs 小工具后台更新

iOs Widget background update

根据 apple 关于 widgetPerformUpdateWithCompletionHandler: 的说法,iOs 小部件也可以在小部件不可见时更新其内容。

在我的调试会话中,我注意到每次通知中心视图消失时都会释放 todayViewController,所以我的问题是:

如果我的 todayViewController 被释放,它如何响应 widgetPerfomrUpdateWithComplationHandler?

我需要小部件每小时更新一次它的内容,如果它不可见,我该怎么做?

iOs 小部件生命周期如何?

请帮助我:) 谢谢

小部件生命周期由 iOS 本身管理。

当您进入后台时,系统会拍摄您今天的小部件的快照。系统管理后台获取(您的小部件的后台生命周期),它会调用 widgetPerfomrUpdateWithComplationHandler: 尽可能更新您的小部件数据,并每次拍摄快照。当您再次打开它时,它会首先加载最新的快照,然后再加载实时数据。


小部件内容更新

Updating Content

The Today extension point provides API for managing a widget’s state and handling updates to its content (you can read about this API in the Notification Center Framework Reference). Although there are a few platform-specific differences in the Today API, the functionality supported on both platforms is mostly the same.

To help your widget look up to date, the system occasionally captures snapshots of your widget’s view. When the widget becomes visible again, the most recent snapshot is displayed until the system replaces it with a live version of the view.

To update a widget’s state before a snapshot is taken, be sure to conform to the NCWidgetProviding protocol. When your widget receives the widgetPerformUpdateWithCompletionHandler: call, update your widget’s view with the most recent content and call the completion handler, using one of the following constants to describe the result of the update:

NCUpdateResultNewData—The new content required you to redraw the view

NCUpdateResultNoData—The widget doesn’t require updating

NCUpdateResultFailed—An error occurred during the update process

引用Today Widget


通知中心数据更新中

Notification Center

Note

The schedule and intended use of widgetPerformUpdateWithCompletionHandler: is intended as a convenient home for all data/model update logic. If implemented, the system will call at opportune times for the widget to update its state, both when Notification Center is visible, as well as in the background. An implementation is required to enable background updates. It’s expected that the widget will perform the work to update asynchronously and off the main thread as much as possible. Widgets should call the argument block when the work is complete, passing the appropriate NCUpdateResult. Widgets should NOT block returning from viewWillAppear: on the results of this operation. Instead, widgets should load cached state in viewWillAppear: in order to match the state of the view from the last viewWillDisappear:, then transition smoothly to the new data when it arrives.

引用iOS 8.1 Notification Center


小部件生命周期

参考:An App Extension’s Life Cycle

iOS8 release notes

Note The schedule and intended use of widgetPerformUpdateWithCompletionHandler: is intended as a convenient home for all data/model update logic. If implemented, the system will call at opportune times for the widget to update its state, both when Notification Center is visible, as well as in the background. An implementation is required to enable background updates. It’s expected that the widget will perform the work to update asynchronously and off the main thread as much as possible. Widgets should call the argument block when the work is complete, passing the appropriate NCUpdateResult. Widgets should NOT block returning from viewWillAppear: on the results of this operation. Instead, widgets should load cached state in viewWillAppear: in order to match the state of the view from the last viewWillDisappear:, then transition smoothly to the new data when it arrives.

您需要同时实施两者

  • viewDidAppear:
  • widgetPerformUpdateWithComplationHandler:

刷新 widgetPerformUpdateWithComplationHandler 中的数据:然后使用 NCUpdateResult 调用完成块。

在 viewDidAppear 中刷新您的视图:启动图像(快照)将来自您上次的 viewDidDissappear:新内容将在下一个 运行 循环结束时显示。

感谢 DigitalBrain_Dev 以供参考。

在 func widgetPerformUpdate(completionHandler: (@escaping (NCUpdateResult) -> Void)) 方法中调用 API 并调用 completionHandler(NCUpdateResult.newData) .