ios - 如何在通知中心频繁更新今日小部件?

标签 ios objective-c ios8-today-widget

我发现我的通知中心小部件不会经常重新加载。因此,会显示过时的数据。每次打开“今日” View 时,我都无法找到让小部件刷新的方法。我见过 NBA Gametime 等应用程序,每次我下拉查看小部件时它们都能更新。

如何在我的应用中获得此功能?

最佳答案

基于我自己的实验以及 this SO thread 中的报告,似乎每次打开 Today/Notifications 区域时都会调用 TodayViewController.viewDidLoad() 函数。更具体地说,@Karl Monaghan在评论中报告说 iOS 8.1 发行说明有以下内容:

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.

所以,回答你的问题:

  1. 我认为我们永远不会真正知道 widgetPerformUpdateWithCompletionHandler 方法被调用的频率,我认为 Apple 更喜欢这种方式
  2. viewDidLoad 函数在每次显示小部件时都会被调用,因此您可以在该方法中调用更新函数。或者,您可以将需要在小部件中显示的任何更新放入该函数内,尽管我不知道这会对应用性能产生什么样的影响。

希望这对您有所帮助。

更新

我还发现每次小部件显示在屏幕上时,似乎都会调用 initWithCoderinitWithNibName 之一,因此您可以将调用以在这些文件中更新您的小部件,但如果我没记错的话,此时 View 实际上并未实例化,因此最好不要将您的代码放入这些函数中以更新您的小部件。

关于ios - 如何在通知中心频繁更新今日小部件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30009441/

相关文章:

ios - 我应该如何命名可导入的 .sqlite 持久存储?

ios - UITableView 底部分隔符插图 - IOS 7 中的奇怪行为

objective-c - int *p; 和有什么区别?和 ClassA *Obj?

xcode - 如何访问今日通知小部件中的图像文件

ios - xctool 使用 today 扩展构建

ios - 有没有一种方法可以一次性添加所有应用程序图标,而不是逐一添加所有应用程序图标?

ios - 弱链接框架导致 "File Not Found"错误

ios - Xcode 意外错误(0xC011)在运行程序后和我按下管理器时

objective-c - 具有两个或多个操作的 NSButton

ios - 如何取消特定 View 的通知中心视觉效果 (iOS)?