ios - 后台任务中的 BGAppRefreshTask

标签 ios swift iphone xcode

我有这段代码,当应用程序处于后台时显示消息“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 所说,它不是按固定间隔调用的。操作系统控制何时执行它。因此我们无法预计具体时间。

你可能知道这一点,但为了以防万一,我将添加这一点,setMinimumBackgroundFetchInterval已在 iOS 13 及更高版本中弃用。您可能需要考虑使用 BackgroundTasks Framework相反。

但是如果你想测试performFetchWithCompletionHandler,你可以使用XCode导航栏>调试>执行后台获取。

screenshot of XCode navigation bar to find option

希望对你有帮助!

关于ios - 后台任务中的 BGAppRefreshTask,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59939107/

相关文章:

iphone - 当选择文本字段时将 View 移到键盘后面

iphone - 使用 Storyboard对应用程序委托(delegate)的引用 View

ios - iOS NSTextAttachment无法将基线偏移量设置为零

ios - 如何在 View Controller 被关闭后显示警报

ios - 如何使表格 View 单元格中的间距均匀?

swift - Rx swift : how to build up the observable function call chain other than use callback?

ios - 无法使用 facebook new sdk 3.11 将链接和图像发布到 friend 墙上

ios - Int to format %.2f 在 iPhone 5 上返回意外数字

iphone - 查找使用过的应用程序名称的 bundle/开发人员

ios - 在图像上淡出滚动 UITextView 吗?