-
Notifications
You must be signed in to change notification settings - Fork 192
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #919 from WalletConnect/fix-web-view-refresh
[W3I] Fix web view refresh
- Loading branch information
Showing
4 changed files
with
48 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import WebKit | ||
import Foundation | ||
|
||
class WebViewRefreshHandler { | ||
private var webViewURLObserver: NSKeyValueObservation! | ||
private let webView: WKWebView | ||
private let initUrl: URL | ||
private var isReloadingContent = false | ||
private let logger: ConsoleLogging | ||
|
||
init(webView: WKWebView, | ||
initUrl: URL, | ||
logger: ConsoleLogging) { | ||
self.webView = webView | ||
self.initUrl = initUrl | ||
self.logger = logger | ||
setUpWebViewObserver() | ||
} | ||
|
||
func setUpWebViewObserver() { | ||
self.webViewURLObserver = webView.observe(\.url, options: .new) { [unowned self] webview, change in | ||
logger.debug("URL: \(String(describing: change.newValue))") | ||
if let newValue = change.newValue, | ||
let url = newValue?.absoluteString, | ||
url.contains("/login"), | ||
!isReloadingContent { | ||
isReloadingContent = true | ||
let request = URLRequest(url: initUrl) | ||
|
||
webview.load(request) | ||
_ = Timer.scheduledTimer(withTimeInterval: 5.0, repeats: false) { [unowned self] timer in | ||
isReloadingContent = false | ||
} | ||
} | ||
} | ||
} | ||
} |