看哪。我试图用swift 4.2创建一个IOS应用程序,根据我的客户,它必须发出警报,但它只能用二维码停止。因此,我将需要的是一个在后台工作的计时器,每秒钟检查一次时间,因为别的东西,用户可以停止它,没有二维码。我找到的唯一的东西是后台获取,但它能在每次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
我认为你应该使用本地通知,而不是。它将被解雇,当你想通知关于过去的时间。你可以在任何时候取消它,并且有任何你想要的条件。
IMO定时器和后台获取不适合这种情况。