mirror of
https://github.com/undeaDD/FakeWebKit.git
synced 2026-03-11 19:00:22 +01:00
safely wrap for tvOS only
This commit is contained in:
@@ -1,13 +1,14 @@
|
|||||||
import UIKit
|
#if os(tvOS)
|
||||||
|
import UIKit
|
||||||
|
|
||||||
// MARK: - ✅ - Cookie + DataStore
|
// MARK: - ✅ - Cookie + DataStore
|
||||||
|
|
||||||
public struct WKCookie {
|
public struct WKCookie {
|
||||||
public let name: String
|
public let name: String
|
||||||
public let value: String
|
public let value: String
|
||||||
}
|
}
|
||||||
|
|
||||||
public class WKCookieStore {
|
public class WKCookieStore {
|
||||||
fileprivate let cookieStorage = HTTPCookieStorage.shared
|
fileprivate let cookieStorage = HTTPCookieStorage.shared
|
||||||
|
|
||||||
public func getAllCookies(_ callback: ([WKCookie]) -> Void) {
|
public func getAllCookies(_ callback: ([WKCookie]) -> Void) {
|
||||||
@@ -26,32 +27,32 @@ public class WKCookieStore {
|
|||||||
cookieStorage.setCookie(httpCookie)
|
cookieStorage.setCookie(httpCookie)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public struct WKDataStore {
|
public struct WKDataStore {
|
||||||
public let httpCookieStore = WKCookieStore()
|
public let httpCookieStore = WKCookieStore()
|
||||||
}
|
}
|
||||||
|
|
||||||
// MARK: - ✅ - Script Message Handler
|
// MARK: - ✅ - Script Message Handler
|
||||||
|
|
||||||
public struct WKScriptMessage {
|
public struct WKScriptMessage {
|
||||||
public let name: String
|
public let name: String
|
||||||
public let body: AnyObject
|
public let body: AnyObject
|
||||||
}
|
}
|
||||||
|
|
||||||
public protocol WKScriptMessageHandler : NSObjectProtocol {
|
public protocol WKScriptMessageHandler : NSObjectProtocol {
|
||||||
func userContentController(_ userContentController: WKUserContentController,
|
func userContentController(_ userContentController: WKUserContentController,
|
||||||
didReceive message: WKScriptMessage)
|
didReceive message: WKScriptMessage)
|
||||||
}
|
}
|
||||||
|
|
||||||
// MARK: - ✅ - User Scripts + Messaging
|
// MARK: - ✅ - User Scripts + Messaging
|
||||||
|
|
||||||
public enum WKUserScriptInjectionTime {
|
public enum WKUserScriptInjectionTime {
|
||||||
case atDocumentStart
|
case atDocumentStart
|
||||||
case atDocumentEnd
|
case atDocumentEnd
|
||||||
}
|
}
|
||||||
|
|
||||||
public struct WKUserScript {
|
public struct WKUserScript {
|
||||||
public let source: String
|
public let source: String
|
||||||
public let injectionTime: WKUserScriptInjectionTime
|
public let injectionTime: WKUserScriptInjectionTime
|
||||||
public let forMainFrameOnly: Bool
|
public let forMainFrameOnly: Bool
|
||||||
@@ -61,9 +62,9 @@ public struct WKUserScript {
|
|||||||
self.injectionTime = injectionTime
|
self.injectionTime = injectionTime
|
||||||
self.forMainFrameOnly = forMainFrameOnly
|
self.forMainFrameOnly = forMainFrameOnly
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public class WKUserContentController {
|
public class WKUserContentController {
|
||||||
fileprivate weak var owner: WKWebView?
|
fileprivate weak var owner: WKWebView?
|
||||||
fileprivate var scripts: [WKUserScript] = []
|
fileprivate var scripts: [WKUserScript] = []
|
||||||
fileprivate var scriptMessageHandlers: [String : WKScriptMessageHandler] = [:]
|
fileprivate var scriptMessageHandlers: [String : WKScriptMessageHandler] = [:]
|
||||||
@@ -81,11 +82,11 @@ public class WKUserContentController {
|
|||||||
scriptMessageHandlers[name] = scriptMessageHandler
|
scriptMessageHandlers[name] = scriptMessageHandler
|
||||||
owner?._updateJSBridgeBindings()
|
owner?._updateJSBridgeBindings()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// MARK: - ✅ - Configuration
|
// MARK: - ✅ - Configuration
|
||||||
|
|
||||||
public class WKWebViewConfiguration {
|
public class WKWebViewConfiguration {
|
||||||
public init() {}
|
public init() {}
|
||||||
|
|
||||||
public var websiteDataStore = WKDataStore()
|
public var websiteDataStore = WKDataStore()
|
||||||
@@ -125,46 +126,46 @@ public class WKWebViewConfiguration {
|
|||||||
"mediaTypesRequiringUserActionForPlayback must be empty")
|
"mediaTypesRequiringUserActionForPlayback must be empty")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// MARK: - ✅ - Navigation + Delegates
|
// MARK: - ✅ - Navigation + Delegates
|
||||||
|
|
||||||
public struct WKNavigation {
|
public struct WKNavigation {
|
||||||
@available(*, unavailable, message: "The effectiveContentMode property is not supported.")
|
@available(*, unavailable, message: "The effectiveContentMode property is not supported.")
|
||||||
public var effectiveContentMode: Any {
|
public var effectiveContentMode: Any {
|
||||||
fatalError("This property is unavailable.")
|
fatalError("This property is unavailable.")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public struct WKNavigationAction: Sendable {
|
public struct WKNavigationAction: Sendable {
|
||||||
public let request: URLRequest
|
public let request: URLRequest
|
||||||
|
|
||||||
@available(*, unavailable, message: "The navigationType property is not supported.")
|
@available(*, unavailable, message: "The navigationType property is not supported.")
|
||||||
public var navigationType: Any {
|
public var navigationType: Any {
|
||||||
fatalError("This property is unavailable.")
|
fatalError("This property is unavailable.")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public enum WKNavigationActionPolicy: Sendable {
|
public enum WKNavigationActionPolicy: Sendable {
|
||||||
case allow
|
case allow
|
||||||
case cancel
|
case cancel
|
||||||
|
|
||||||
@available(*, unavailable, message: "The .download policy is not supported.")
|
@available(*, unavailable, message: "The .download policy is not supported.")
|
||||||
case download
|
case download
|
||||||
}
|
}
|
||||||
|
|
||||||
public protocol WKNavigationDelegate : NSObjectProtocol {
|
public protocol WKNavigationDelegate : NSObjectProtocol {
|
||||||
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation)
|
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation)
|
||||||
func webView(_ webView: WKWebView, didFail navigation: WKNavigation, withError error: Error)
|
func webView(_ webView: WKWebView, didFail navigation: WKNavigation, withError error: Error)
|
||||||
func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction,
|
func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction,
|
||||||
decisionHandler: @escaping (WKNavigationActionPolicy) -> Void)
|
decisionHandler: @escaping (WKNavigationActionPolicy) -> Void)
|
||||||
func webView(_ webView: WKWebView, didStartProvisionalNavigation navigation: WKNavigation
|
func webView(_ webView: WKWebView, didStartProvisionalNavigation navigation: WKNavigation
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
// MARK: - ✅ - WKWebView ( without WebKit )
|
// MARK: - ✅ - WKWebView ( without WebKit )
|
||||||
|
|
||||||
public class WKWebView: UIView {
|
public class WKWebView: UIView {
|
||||||
fileprivate var _webView: AnyObject?
|
fileprivate var _webView: AnyObject?
|
||||||
fileprivate var _delegateProxy: _DelegateProxy?
|
fileprivate var _delegateProxy: _DelegateProxy?
|
||||||
public static var logger: DebugLogger?
|
public static var logger: DebugLogger?
|
||||||
@@ -271,19 +272,19 @@ public class WKWebView: UIView {
|
|||||||
completionHandler?(result?.takeUnretainedValue(), nil)
|
completionHandler?(result?.takeUnretainedValue(), nil)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// MARK: Internal Helper Functions
|
// MARK: Internal Helper Functions
|
||||||
|
|
||||||
public protocol DebugLogger {
|
public protocol DebugLogger {
|
||||||
func log(_ message: String)
|
func log(_ message: String)
|
||||||
func logError(_ message: String)
|
func logError(_ message: String)
|
||||||
}
|
}
|
||||||
|
|
||||||
@inlinable
|
@inlinable
|
||||||
func sel(_ name: String) -> Selector { Selector(name) }
|
func sel(_ name: String) -> Selector { Selector(name) }
|
||||||
|
|
||||||
fileprivate extension WKWebView {
|
fileprivate extension WKWebView {
|
||||||
|
|
||||||
@inline(never)
|
@inline(never)
|
||||||
func getInternalClassName() -> [String] {
|
func getInternalClassName() -> [String] {
|
||||||
@@ -392,10 +393,10 @@ fileprivate extension WKWebView {
|
|||||||
WKWebView.logger?.log("JSBridge message received for '\(handlerName)'")
|
WKWebView.logger?.log("JSBridge message received for '\(handlerName)'")
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@objc
|
@objc
|
||||||
fileprivate class _DelegateProxy: NSObject {
|
fileprivate class _DelegateProxy: NSObject {
|
||||||
fileprivate weak var owner: WKWebView?
|
fileprivate weak var owner: WKWebView?
|
||||||
|
|
||||||
init(owner: WKWebView) {
|
init(owner: WKWebView) {
|
||||||
@@ -509,4 +510,5 @@ fileprivate class _DelegateProxy: NSObject {
|
|||||||
WKWebView.logger?.log("Decision: \(decision == .allow ? "ALLOW" : "CANCEL")")
|
WKWebView.logger?.log("Decision: \(decision == .allow ? "ALLOW" : "CANCEL")")
|
||||||
return decision == .allow
|
return decision == .allow
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
#endif
|
||||||
|
|||||||
Reference in New Issue
Block a user