简体   繁体   English

iOS Web 当应用程序进入后台并进入前台时,视图会卡住

[英]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.

相关问题 从后台到前台,iOS应用程序会进入仪表板吗? - iOS application goes to dashboard when it comes from background to foreground? 当应用程序进入后台然后进入前台时,如何继续视频捕获? - How to continue video capture when app goes background then comes in foreground? 当应用程序进入后台时,我的View Controller会被释放 - My View Controller gets deallocated when the app goes to the background 当应用程序返回前台时,子线程随机被杀死 - Child thread randomly gets killed when app comes back to the foreground 应用在iOS 7中使用applicationDidEnterBackground进入后台时无法更新视图: - Unable to update view when app goes into background in iOS 7 using applicationDidEnterBackground: 当应用程序进入后台时,MPMoviePlayerViewController消失 - MPMoviePlayerViewController gets dissmiss when app goes background 应用程序进入后台时调用方法 - Methods gets called when app goes to the background 应用程序到达前台时,将iOS应用程序与ALAssetsLibrary同步的最佳方式 - Best way to sync iOS application with ALAssetsLibrary when app comes to foreground 当应用程序进入后台时暂停动画并在 Swift 中以前台模式恢复 - Pause animation when app goes background and resume in foreground mode in Swift 当应用程序进入前台时,哪个View Controller处于活动状态? - Which View Controller is active when app comes to foreground?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM