杨锴
2024-08-14 909e20941e45f8712c012db602034b47da0bfdb0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
//
//  WKWebView+Rx.swift
//  RxCocoa
//
//  Created by Giuseppe Lanza on 14/02/2020.
//  Copyright © 2020 Krunoslav Zaher. All rights reserved.
//
 
#if os(iOS) || os(macOS)
 
import RxSwift
import WebKit
 
@available(iOS 8.0, macOS 10.10, macOSApplicationExtension 10.10, *)
extension Reactive where Base: WKWebView {
    
    /// Reactive wrapper for `navigationDelegate`.
    /// For more information take a look at `DelegateProxyType` protocol documentation.
    public var navigationDelegate: DelegateProxy<WKWebView, WKNavigationDelegate> {
        RxWKNavigationDelegateProxy.proxy(for: base)
    }
    
    /// Reactive wrapper for `navigationDelegate` message.
    public var didCommit: Observable<WKNavigation> {
        navigationDelegate
            .methodInvoked(#selector(WKNavigationDelegate.webView(_:didCommit:)))
            .map { a in try castOrThrow(WKNavigation.self, a[1]) }
    }
    
    /// Reactive wrapper for `navigationDelegate` message.
    public var didStartLoad: Observable<WKNavigation> {
        navigationDelegate
            .methodInvoked(#selector(WKNavigationDelegate.webView(_:didStartProvisionalNavigation:)))
            .map { a in try castOrThrow(WKNavigation.self, a[1]) }
    }
 
    /// Reactive wrapper for `navigationDelegate` message.
    public var didFinishLoad: Observable<WKNavigation> {
        navigationDelegate
            .methodInvoked(#selector(WKNavigationDelegate.webView(_:didFinish:)))
            .map { a in try castOrThrow(WKNavigation.self, a[1]) }
    }
 
    /// Reactive wrapper for `navigationDelegate` message.
    public var didFailLoad: Observable<(WKNavigation, Error)> {
        navigationDelegate
            .methodInvoked(#selector(WKNavigationDelegate.webView(_:didFail:withError:)))
            .map { a in
                (
                    try castOrThrow(WKNavigation.self, a[1]),
                    try castOrThrow(Error.self, a[2])
                )
            }
    }
}
 
#endif