From a56340c08bfd5489401341338b3d4b7c31dfb3ce Mon Sep 17 00:00:00 2001 From: keisukeYamagishi Date: Sat, 17 Aug 2024 18:31:16 +0900 Subject: [PATCH] Add a didFailProvisionalNavigation observer. --- RxCocoa/iOS/WKWebView+Rx.swift | 11 +++++++++++ Tests/RxCocoaTests/WKWebView+RxTests.swift | 16 ++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/RxCocoa/iOS/WKWebView+Rx.swift b/RxCocoa/iOS/WKWebView+Rx.swift index 6332fae74..71b6dffda 100644 --- a/RxCocoa/iOS/WKWebView+Rx.swift +++ b/RxCocoa/iOS/WKWebView+Rx.swift @@ -52,6 +52,17 @@ extension Reactive where Base: WKWebView { ) } } + + public var didFailProvisionalNavigation: Observable<(WKNavigation, Error)> { + navigationDelegate + .methodInvoked(#selector(WKNavigationDelegate.webView(_:didFailProvisionalNavigation:withError:))) + .map { a in + ( + try castOrThrow(WKNavigation.self,a[1]), + try castOrThrow(Error.self, a[2]) + ) + } + } } #endif diff --git a/Tests/RxCocoaTests/WKWebView+RxTests.swift b/Tests/RxCocoaTests/WKWebView+RxTests.swift index 7516709f9..36c7afeb9 100644 --- a/Tests/RxCocoaTests/WKWebView+RxTests.swift +++ b/Tests/RxCocoaTests/WKWebView+RxTests.swift @@ -90,6 +90,22 @@ final class WKWebViewTests: RxTest { XCTAssertEqual(expectedError, error as? MockError) subscription.dispose() } + + func testdidFailProvisionalNavigation() { + let expectedNavigation = SafeWKNavigation() + let expexctedError = MockError.error("Something went wrong!") + let webView = WKWebView(frame: .zero) + var navigation: WKNavigation? + var error: Error? + let subscription = webView.rx.didFailProvisionalNavigation.subscribe { (nav, err) in + navigation = nav + error = err + } + webView.navigationDelegate!.webView?(webView, didFailProvisionalNavigation: expectedNavigation, withError:expexctedError) + XCTAssertEqual(expectedNavigation, navigation) + XCTAssertEqual(expexctedError, error as? MockError) + subscription.dispose() + } } // MARK: - Test Helpers