BackgroundTasks 中的 BGAppRefreshTask
BGAppRefreshTask in the BackgroundTasks
当应用程序处于后台时,我有此代码显示一条消息 "Msg Background"。我需要的是只要应用程序在后台继续每 2 分钟显示一次该消息(只是时间的一个例子)。到目前为止我的代码只显示了一次消息,显然这句话没有正常工作。
UIApplication.shared.setMinimumBackgroundFetchInterval (UIApplication.backgroundFetchIntervalMinimum)
我也收到此警告:'setMinimumBackgroundFetchInterval' 在 iOS 13.0 中已弃用:改为在 BackgroundTasks 框架中使用 BGAppRefreshTask
我正在使用 swift 5 和 Xcode 11
class AppDelegate: UIResponder, UIApplicationDelegate {
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
GMSServices.provideAPIKey("AIzaSyBSpAt5zqvbh73FmG_Kb6xpiFMkWRmHppg")
UIApplication.shared.setMinimumBackgroundFetchInterval(UIApplication.backgroundFetchIntervalMinimum)
return true
}
func application(_ application: UIApplication, performFetchWithCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
print("Msg Background")
}
}
正如 Paulw11 所说,它不是按固定间隔调用的。 OS 控制何时执行。因此我们不能期待具体的时间。
您可能知道这一点,但为了以防万一,我将添加这一点,改为 setMinimumBackgroundFetchInterval has deprecated on iOS 13 and later. You might want to consider to use BackgroundTasks Framework。
但如果您想测试 performFetchWithCompletionHandler,您可以使用 XCode 导航栏 > 调试 > 执行后台获取。
screenshot of XCode navigation bar to find option
希望对您有所帮助!
当应用程序处于后台时,我有此代码显示一条消息 "Msg Background"。我需要的是只要应用程序在后台继续每 2 分钟显示一次该消息(只是时间的一个例子)。到目前为止我的代码只显示了一次消息,显然这句话没有正常工作。
UIApplication.shared.setMinimumBackgroundFetchInterval (UIApplication.backgroundFetchIntervalMinimum)
我也收到此警告:'setMinimumBackgroundFetchInterval' 在 iOS 13.0 中已弃用:改为在 BackgroundTasks 框架中使用 BGAppRefreshTask
我正在使用 swift 5 和 Xcode 11
class AppDelegate: UIResponder, UIApplicationDelegate {
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
GMSServices.provideAPIKey("AIzaSyBSpAt5zqvbh73FmG_Kb6xpiFMkWRmHppg")
UIApplication.shared.setMinimumBackgroundFetchInterval(UIApplication.backgroundFetchIntervalMinimum)
return true
}
func application(_ application: UIApplication, performFetchWithCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
print("Msg Background")
}
}
正如 Paulw11 所说,它不是按固定间隔调用的。 OS 控制何时执行。因此我们不能期待具体的时间。
您可能知道这一点,但为了以防万一,我将添加这一点,改为 setMinimumBackgroundFetchInterval has deprecated on iOS 13 and later. You might want to consider to use BackgroundTasks Framework。
但如果您想测试 performFetchWithCompletionHandler,您可以使用 XCode 导航栏 > 调试 > 执行后台获取。
screenshot of XCode navigation bar to find option
希望对您有所帮助!