我正在尝试在func applicationDidEnterBackground中运行代码,并希望将timeDelay设置为第二天@上午5点
例如:如果用户在1月1日晚上10点退出应用程序,我希望timeDelay在1月2日凌晨5点退出
代码如下:
func applicationDidEnterBackground(_ application: UIApplication) {
submitBackgroundTasks()
}
func submitBackgroundTasks() {
// Declared at the "Permitted background task scheduler identifiers" in info.plist
let backgroundAppRefreshTaskSchedulerIdentifier = "bundle ID here"
let timeDelay = 10.0
do {
let backgroundAppRefreshTaskRequest = BGAppRefreshTaskRequest(identifier: backgroundAppRefreshTaskSchedulerIdentifier)
backgroundAppRefreshTaskRequest.earliestBeginDate = Date(timeIntervalSinceNow: timeDelay)
try BGTaskScheduler.shared.submit(backgroundAppRefreshTaskRequest)
print("Submitted task request")
} catch {
print("Failed to submit BGTask")
}
}有什么建议吗?谢谢。
发布于 2021-01-03 22:09:29
第一步是将日期更改为第二天:
let changedDate = Calendar.current.date(byAdding: .day, value: 1, to: Date())!
这将日期从1月1日晚上10点改为1月2日晚上10点我们有正确的日期,但我们需要将时间设置为上午5点:
let changedTimeAndDate = Calendar.current.date(bySettingHour: 5, minute: 0, second: 0, of: changedDate)!
https://stackoverflow.com/questions/65542454
复制相似问题