Skip to content
This repository has been archived by the owner on Oct 2, 2021. It is now read-only.

Commit

Permalink
Avoid timeout when the app starts up in the background
Browse files Browse the repository at this point in the history
Closes #9.
  • Loading branch information
martijnwalraven committed Oct 3, 2016
1 parent a27143f commit 8bbf1ae
Showing 1 changed file with 13 additions and 3 deletions.
16 changes: 13 additions & 3 deletions src/ios/WebAppLocalServer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -112,10 +112,12 @@ open class WebAppLocalServer: METPlugin, AssetBundleManagerDelegate {
self?.revertToLastKnownGoodVersion()
}
}

NotificationCenter.default.addObserver(self, selector: #selector(WebAppLocalServer.pageDidLoad), name: NSNotification.Name.CDVPageDidLoad, object: webView)

NotificationCenter.default.addObserver(self, selector: #selector(WebAppLocalServer.applicationDidEnterBackground), name: NSNotification.Name.UIApplicationDidEnterBackground, object: nil)

NotificationCenter.default.addObserver(self, selector: #selector(WebAppLocalServer.pageDidLoad), name: NSNotification.Name.CDVPageDidLoad, object: webView)
NotificationCenter.default.addObserver(self, selector: #selector(WebAppLocalServer.applicationWillEnterForeground), name: NSNotification.Name.UIApplicationWillEnterForeground, object: nil)
}

func initializeAssetBundles() {
Expand Down Expand Up @@ -194,7 +196,10 @@ open class WebAppLocalServer: METPlugin, AssetBundleManagerDelegate {
self.pendingAssetBundle = nil
}

startupTimer?.start(withTimeInterval: startupTimeoutInterval)
// Don't start the startup timer if the app started up in the background
if UIApplication.shared.applicationState == UIApplicationState.active {
startupTimer?.start(withTimeInterval: startupTimeoutInterval)
}
}

// MARK: - Notifications
Expand All @@ -207,6 +212,11 @@ open class WebAppLocalServer: METPlugin, AssetBundleManagerDelegate {
// blacklisting a version just because the web view has been suspended
startupTimer?.stop()
}

func applicationWillEnterForeground() {
// Restart startup timer when entering the foreground again
startupTimer?.start(withTimeInterval: startupTimeoutInterval)
}

// MARK: - Public plugin commands

Expand Down

0 comments on commit 8bbf1ae

Please sign in to comment.