diff --git a/Source/Turbo/Navigator/Navigator.swift b/Source/Turbo/Navigator/Navigator.swift index 9c5b0655..05ce60fa 100644 --- a/Source/Turbo/Navigator/Navigator.swift +++ b/Source/Turbo/Navigator/Navigator.swift @@ -222,7 +222,11 @@ extension Navigator: SessionDelegate { } public func session(_ session: Session, didReceiveAuthenticationChallenge challenge: URLAuthenticationChallenge, completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void) { - delegate?.didReceiveAuthenticationChallenge(challenge, completionHandler: completionHandler) + guard let delegate else { + completionHandler(.performDefaultHandling, nil) + return + } + delegate.didReceiveAuthenticationChallenge(challenge, completionHandler: completionHandler) } public func sessionDidFinishRequest(_ session: Session) { diff --git a/Source/Turbo/Navigator/WKUIController.swift b/Source/Turbo/Navigator/WKUIController.swift index 0cfb4254..394d4c40 100644 --- a/Source/Turbo/Navigator/WKUIController.swift +++ b/Source/Turbo/Navigator/WKUIController.swift @@ -13,14 +13,22 @@ open class WKUIController: NSObject, WKUIDelegate { } open func webView(_ webView: WKWebView, runJavaScriptAlertPanelWithMessage message: String, initiatedByFrame frame: WKFrameInfo, completionHandler: @escaping () -> Void) { + guard let delegate else { + completionHandler() + return + } let alert = UIAlertController(title: message, message: nil, preferredStyle: .alert) alert.addAction(UIAlertAction(title: "Close", style: .default) { _ in completionHandler() }) - delegate?.present(alert, animated: true) + delegate.present(alert, animated: true) } open func webView(_ webView: WKWebView, runJavaScriptConfirmPanelWithMessage message: String, initiatedByFrame frame: WKFrameInfo, completionHandler: @escaping (Bool) -> Void) { + guard let delegate else { + completionHandler(false) + return + } let alert = UIAlertController(title: message, message: nil, preferredStyle: .alert) alert.addAction(UIAlertAction(title: "OK", style: .default) { _ in completionHandler(true) @@ -28,6 +36,6 @@ open class WKUIController: NSObject, WKUIDelegate { alert.addAction(UIAlertAction(title: "Cancel", style: .cancel) { _ in completionHandler(false) }) - delegate?.present(alert, animated: true) + delegate.present(alert, animated: true) } } diff --git a/Source/Turbo/Session/Session.swift b/Source/Turbo/Session/Session.swift index 7aebec5d..483d45b1 100644 --- a/Source/Turbo/Session/Session.swift +++ b/Source/Turbo/Session/Session.swift @@ -219,7 +219,11 @@ extension Session: VisitDelegate { } func visit(_ visit: Visit, didReceiveAuthenticationChallenge challenge: URLAuthenticationChallenge, completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void) { - delegate?.session(self, didReceiveAuthenticationChallenge: challenge, completionHandler: completionHandler) + guard let delegate else { + completionHandler(.performDefaultHandling, nil) + return + } + delegate.session(self, didReceiveAuthenticationChallenge: challenge, completionHandler: completionHandler) } func visitDidProposeVisitToLocation(_ location: URL) { diff --git a/Source/Turbo/Visit/ColdBootVisit.swift b/Source/Turbo/Visit/ColdBootVisit.swift index e3e645f2..757eb6c9 100644 --- a/Source/Turbo/Visit/ColdBootVisit.swift +++ b/Source/Turbo/Visit/ColdBootVisit.swift @@ -128,7 +128,11 @@ extension ColdBootVisit: WKNavigationDelegate { } func webView(_ webView: WKWebView, didReceive challenge: URLAuthenticationChallenge, completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void) { - delegate?.visit(self, didReceiveAuthenticationChallenge: challenge, completionHandler: completionHandler) + guard let delegate else { + completionHandler(.performDefaultHandling, nil) + return + } + delegate.visit(self, didReceiveAuthenticationChallenge: challenge, completionHandler: completionHandler) } }