diff --git a/Sources/Web3Inbox/Web3InboxClient.swift b/Sources/Web3Inbox/Web3InboxClient.swift index 730180633..76aa2bce9 100644 --- a/Sources/Web3Inbox/Web3InboxClient.swift +++ b/Sources/Web3Inbox/Web3InboxClient.swift @@ -16,6 +16,8 @@ public final class Web3InboxClient { private let chatWebviewProxy: WebViewProxy private let pushWebviewProxy: WebViewProxy + private let webViewRefreshHandler: WebViewRefreshHandler + private let chatWebviewSubscriber: WebViewRequestSubscriber private let pushWebviewSubscriber: WebViewRequestSubscriber @@ -30,7 +32,8 @@ init( chatWebviewSubscriber: WebViewRequestSubscriber, pushWebviewSubscriber: WebViewRequestSubscriber, pushClientProxy: PushClientProxy, - pushClientSubscriber: PushClientRequestSubscriber + pushClientSubscriber: PushClientRequestSubscriber, + webViewRefreshHandler: WebViewRefreshHandler ) { self.webView = webView self.account = account @@ -43,7 +46,7 @@ init( self.pushWebviewSubscriber = pushWebviewSubscriber self.pushClientProxy = pushClientProxy self.pushClientSubscriber = pushClientSubscriber - + self.webViewRefreshHandler = webViewRefreshHandler setupSubscriptions() } diff --git a/Sources/Web3Inbox/Web3InboxClientFactory.swift b/Sources/Web3Inbox/Web3InboxClientFactory.swift index 50d255e29..8d01cb0e2 100644 --- a/Sources/Web3Inbox/Web3InboxClientFactory.swift +++ b/Sources/Web3Inbox/Web3InboxClientFactory.swift @@ -24,6 +24,8 @@ final class Web3InboxClientFactory { let pushClientProxy = PushClientProxy(client: pushClient, onSign: onSign) let pushClientSubscriber = PushClientRequestSubscriber(client: pushClient, logger: logger) + let webViewRefreshHandler = WebViewRefreshHandler(webView: webView, initUrl: url, logger: logger) + return Web3InboxClient( webView: webView, account: account, @@ -35,13 +37,14 @@ final class Web3InboxClientFactory { chatWebviewSubscriber: chatWebviewSubscriber, pushWebviewSubscriber: pushWebviewSubscriber, pushClientProxy: pushClientProxy, - pushClientSubscriber: pushClientSubscriber + pushClientSubscriber: pushClientSubscriber, + webViewRefreshHandler: webViewRefreshHandler ) } private static func buildUrl(account: Account, config: [ConfigParam: Bool]) -> URL { var urlComponents = URLComponents(string: "https://web3inbox-dev-hidden.vercel.app/")! - var queryItems = [URLQueryItem(name: "chatProvider", value: "ios"), URLQueryItem(name: "pushProvider", value: "ios"), URLQueryItem(name: "account", value: account.address)] + var queryItems = [URLQueryItem(name: "chatProvider", value: "ios"), URLQueryItem(name: "pushProvider", value: "ios"), URLQueryItem(name: "account", value: account.address), URLQueryItem(name: "authProvider", value: "ios")] for param in config.filter({ $0.value == false}) { queryItems.append(URLQueryItem(name: "\(param.key)", value: "false")) diff --git a/Sources/Web3Inbox/WebView/WebViewProxy.swift b/Sources/Web3Inbox/WebView/WebViewProxy.swift index b0a4451f0..af3d0d8d7 100644 --- a/Sources/Web3Inbox/WebView/WebViewProxy.swift +++ b/Sources/Web3Inbox/WebView/WebViewProxy.swift @@ -13,6 +13,7 @@ actor WebViewProxy { self.webView = webView self.scriptFormatter = scriptFormatter self.logger = logger + } @MainActor diff --git a/Sources/Web3Inbox/WebView/WebViewRefreshHandler.swift b/Sources/Web3Inbox/WebView/WebViewRefreshHandler.swift new file mode 100644 index 000000000..c6155d9f9 --- /dev/null +++ b/Sources/Web3Inbox/WebView/WebViewRefreshHandler.swift @@ -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 + } + } + } + } +}