iOs Widget background update

前端 未结 3 465
小蘑菇
小蘑菇 2021-01-02 00:53

According to what apple says about widgetPerformUpdateWithCompletionHandler:, the iOs Widget can update his content also when the widget is not visible.

In my debug

3条回答
  •  醉话见心
    2021-01-02 01:36

    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.

提交回复
热议问题