From 4de9cd96aa58d27c66545812bf836a131e2115d5 Mon Sep 17 00:00:00 2001 From: Sven Tiigi Date: Wed, 9 Oct 2024 21:00:47 +0200 Subject: [PATCH] Add didFailProvisionalNavigation https://github.com/SvenTiigi/YouTubePlayerKit/issues/104 --- Sources/Models/YouTubePlayer+Error.swift | 2 ++ Sources/WebView/YouTubePlayerWebView+Event.swift | 3 +++ ...ouTubePlayerWebView+WKNavigationDelegate.swift | 15 +++++++++++++++ Sources/YouTubePlayer.swift | 7 +++++++ 4 files changed, 27 insertions(+) diff --git a/Sources/Models/YouTubePlayer+Error.swift b/Sources/Models/YouTubePlayer+Error.swift index 8ee5a17..c07c38b 100644 --- a/Sources/Models/YouTubePlayer+Error.swift +++ b/Sources/Models/YouTubePlayer+Error.swift @@ -8,6 +8,8 @@ public extension YouTubePlayer { enum Error: Swift.Error, Sendable { /// Player setup failed with Error case setupFailed(Swift.Error) + /// Did fail provisional navigation + case didFailProvisionalNavigation(Swift.Error) /// The web views underlying web content process was terminated case webContentProcessDidTerminate /// The YouTube iFrame API JavaScript failed to load diff --git a/Sources/WebView/YouTubePlayerWebView+Event.swift b/Sources/WebView/YouTubePlayerWebView+Event.swift index cd47d8d..5729fb7 100644 --- a/Sources/WebView/YouTubePlayerWebView+Event.swift +++ b/Sources/WebView/YouTubePlayerWebView+Event.swift @@ -1,4 +1,5 @@ import Foundation +import WebKit // MARK: - YouTubePlayerWebView+Event @@ -10,6 +11,8 @@ extension YouTubePlayerWebView { case receivedJavaScriptEvent(YouTubePlayer.JavaScriptEvent) /// The frame of the YouTubePlayerWebView changed case frameChanged(CGRect) + /// Did fail provisional navigation + case didFailProvisionalNavigation(Error) /// Web content process did terminate case webContentProcessDidTerminate } diff --git a/Sources/WebView/YouTubePlayerWebView+WKNavigationDelegate.swift b/Sources/WebView/YouTubePlayerWebView+WKNavigationDelegate.swift index 1d50003..23f9dad 100644 --- a/Sources/WebView/YouTubePlayerWebView+WKNavigationDelegate.swift +++ b/Sources/WebView/YouTubePlayerWebView+WKNavigationDelegate.swift @@ -5,6 +5,21 @@ import WebKit extension YouTubePlayerWebView: WKNavigationDelegate { + /// WebView did fail provisional navigation + /// - Parameters: + /// - webView: The web view. + /// - navigation: The navigation. + /// - error: The error. + func webView( + _ webView: WKWebView, + didFailProvisionalNavigation navigation: WKNavigation!, + withError error: Error + ) { + self.eventSubject.send( + .didFailProvisionalNavigation(error) + ) + } + /// WebView decide policy for NavigationAction /// - Parameters: /// - webView: The WKWebView diff --git a/Sources/YouTubePlayer.swift b/Sources/YouTubePlayer.swift index 57734b1..94d0d61 100644 --- a/Sources/YouTubePlayer.swift +++ b/Sources/YouTubePlayer.swift @@ -167,6 +167,13 @@ private extension YouTubePlayer { self.webView.evaluate( javaScript: .init("setYouTubePlayerSize(\(parameters));") ) + case .didFailProvisionalNavigation(let error): + // Send object will change event + self.objectWillChange.send() + // Send web content process did terminate error + self.playerStateSubject.send( + .error(.didFailProvisionalNavigation(error)) + ) case .webContentProcessDidTerminate: // Send object will change event self.objectWillChange.send()