[英]iOS Web View Gets Stuck when ever the app goes to background and comes to foreground
We have a web-view which hosts a web action sheet with a timer.我们有一个 Web 视图,其中包含一个带有计时器的 web 操作表。 Whenever there is a navigation to a different app and back to our app, the timer gets stuck and the web action sheet becomes unresponsive.
每当导航到不同的应用程序并返回到我们的应用程序时,计时器就会卡住,并且 web 操作表变得无响应。
Any solutions or work around to solve this issue?任何解决方案或解决方法来解决这个问题?
Use the following function to create a timer that keeps working when the app is in background:使用以下 function 创建一个计时器,该计时器在应用程序处于后台时继续工作:
func executeAfterDelay(delay: TimeInterval, completion: @escaping(()->Void)){
backgroundTaskId = UIApplication.shared.beginBackgroundTask(
withName: "BackgroundSound",
expirationHandler: {[weak self] in
if let taskId = self?.backgroundTaskId{
UIApplication.shared.endBackgroundTask(taskId)
}
})
let startTime = Date()
DispatchQueue.global(qos: .background).async {
while Date().timeIntervalSince(startTime) < delay{
Thread.sleep(forTimeInterval: 0.01)
}
DispatchQueue.main.async {[weak self] in
completion()
if let taskId = self?.backgroundTaskId{
UIApplication.shared.endBackgroundTask(taskId)
}
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.