iOs Widget background update

拟墨画扇 提交于 2019-11-30 13:02:23

The widget life-cycle is managed by the iOS itself.

When you go to background, the system takes a snapshot of your today's widget. The system manages the background fetch (background life cycle of your widget), it'll call the widgetPerfomrUpdateWithComplationHandler: for updating your widget data whenever possible and takes a snapshot each time. And when you open it again, it'll load the most recent snapshot first and loads the live data after that.


Widget Content Update

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

Reference Today Widget


Notification Center Data Updating

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.

Reference iOS 8.1 Notification Center


Widget Life Cycle

Reference: An App Extension’s Life Cycle

aumansoftware

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.

You need to implement both

  • viewDidAppear:
  • widgetPerformUpdateWithComplationHandler:

Refresh your data in widgetPerformUpdateWithComplationHandler: then call the completion block with NCUpdateResult.

Refresh your view in viewDidAppear: The launch image (snapshot) will be from your last viewDidDissappear: New contents will be displayed at the end of the next run loop.

Credit to DigitalBrain_Dev for document reference.

Call your API in func widgetPerformUpdate(completionHandler: (@escaping (NCUpdateResult) -> Void)) method and call the completionHandler(NCUpdateResult.newData) .

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!