【问题标题】:Background Fetch working every Second in Swift 4.2在 Swift 4.2 中,后台获取每秒都在工作
【发布时间】:2020-06-13 13:15:35
【问题描述】:

看。我正在尝试使用 swift 4.2 创建一个 IOS 应用程序,据我的客户说,它必须发出警报,但只能通过 QR 码停止。因此,我需要一个在后台工作的计时器,每秒钟检查一次时间,因为别的东西,用户可以在没有二维码的情况下停止它。我唯一找到的就是后台提取,但它可以每次都进行 1 秒检查吗?

有人有更好的主意吗?

干杯

这是我现在的源代码

应用委托:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
     UIApplication.shared.setMinimumBackgroundFetchInterval(2.0)
}

//ini----------------------------background task
func application(_ application: UIApplication, performFetchWithCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
    // fetch data from internet now
    let time:TimerNow = TimerNow();
    let hours = preferences.string(forKey: "hours");
    let minutes = preferences.string(forKey: "minutes");
    if((hours == time.getHours())&&(minutes == time.getMinutes())){
        lem.onPlayCommand();
    }
    completionHandler(.newData)
}
//fin ---------------------------background task

【问题讨论】:

    标签: swift background fetch alarm


    【解决方案1】:

    我认为您应该改用本地通知。当您想要通知已用时间时,它将被触发。您可以随时在任何您想要的条件下取消它。

    IMO 计时器和后台获取不适合这种情况。

    【讨论】:

    • 谢谢。我已经考虑过了,但我需要一些允许我递归运行脚本的东西。由于声音会一直循环到 QR 读数,这样可以吗?你有例子吗?
    • 我需要一些在小时和分钟匹配时运行函数的脚本。并且音乐必须随着通知自动播放
    猜你喜欢
    • 1970-01-01
    • 2015-09-21
    • 2023-03-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-22
    相关资源
    最近更新 更多