From 75140682e74a07561f71bdb2be20b8168c602b0e Mon Sep 17 00:00:00 2001 From: "mergify[bot]" <37929162+mergify[bot]@users.noreply.github.com> Date: Wed, 19 Apr 2023 11:24:43 -0400 Subject: [PATCH] Bugfix FXIOS-6206 [v112.2] Restore iOS version checks on js injection (#14011) (#14016) * Restore iOS version checks on js injection * Add a comment for future reference * Fix swiftlint (cherry picked from commit d28cd9d0560cfa98d3092e9f87c2bd48bac5e484) Co-authored-by: OrlaM --- Shared/Extensions/WKWebViewExtensions.swift | 62 ++++++++++++++++----- 1 file changed, 49 insertions(+), 13 deletions(-) diff --git a/Shared/Extensions/WKWebViewExtensions.swift b/Shared/Extensions/WKWebViewExtensions.swift index b8868384c7ab..b47a7a9a4789 100644 --- a/Shared/Extensions/WKWebViewExtensions.swift +++ b/Shared/Extensions/WKWebViewExtensions.swift @@ -6,24 +6,40 @@ import Foundation import WebKit extension WKWebView { - /// This calls WebKit evaluateJavaScript function + /// This calls different WebKit evaluateJavaScript functions depending on iOS version + /// - If iOS14 or higher, evaluates Javascript in a .defaultClient sandboxed content world + /// - If below iOS14, evaluates Javascript without sandboxed environment /// - Parameters: /// - javascript: String representing javascript to be evaluated public func evaluateJavascriptInDefaultContentWorld(_ javascript: String) { - self.evaluateJavaScript(javascript, in: nil, in: .defaultClient, completionHandler: { _ in }) + // iOS 14.3 is required here because of a webkit bug in lower iOS versions with this API + if #available(iOS 14.3, *) { + self.evaluateJavaScript(javascript, in: nil, in: .defaultClient, completionHandler: { _ in }) + } else { + self.evaluateJavaScript(javascript) + } } - /// This calls WebKit evaluateJavaScript function with a completion that passes a tuple with optional data or an optional error + /// This calls different WebKit evaluateJavaScript functions depending on iOS version with a completion that passes a tuple with optional data or an optional error + /// - If iOS14 or higher, evaluates Javascript in a .defaultClient sandboxed content world + /// - If below iOS14, evaluates Javascript without sandboxed environment /// - Parameters: /// - javascript: String representing javascript to be evaluated /// - completion: Tuple containing optional data and an optional error public func evaluateJavascriptInDefaultContentWorld(_ javascript: String, _ completion: @escaping (Any?, Error?) -> Void) { - self.evaluateJavaScript(javascript, in: nil, in: .defaultClient) { result in - switch result { - case .success(let value): - completion(value, nil) - case .failure(let error): - completion(nil, error) + // iOS 14.3 is required here because of a webkit bug in lower iOS versions with this API + if #available(iOS 14.3, *) { + self.evaluateJavaScript(javascript, in: nil, in: .defaultClient) { result in + switch result { + case .success(let value): + completion(value, nil) + case .failure(let error): + completion(nil, error) + } + } + } else { + self.evaluateJavaScript(javascript) { data, error in + completion(data, error) } } } @@ -31,20 +47,40 @@ extension WKWebView { extension WKUserContentController { public func addInDefaultContentWorld(scriptMessageHandler: WKScriptMessageHandler, name: String) { - add(scriptMessageHandler, contentWorld: .defaultClient, name: name) + // iOS 14.3 is required here because of a webkit bug in lower iOS versions with this API + if #available(iOS 14.3, *) { + add(scriptMessageHandler, contentWorld: .defaultClient, name: name) + } else { + add(scriptMessageHandler, name: name) + } } public func addInPageContentWorld(scriptMessageHandler: WKScriptMessageHandler, name: String) { - add(scriptMessageHandler, contentWorld: .page, name: name) + // iOS 14.3 is required here because of a webkit bug in lower iOS versions with this API + if #available(iOS 14.3, *) { + add(scriptMessageHandler, contentWorld: .page, name: name) + } else { + add(scriptMessageHandler, name: name) + } } } extension WKUserScript { public class func createInDefaultContentWorld(source: String, injectionTime: WKUserScriptInjectionTime, forMainFrameOnly: Bool) -> WKUserScript { - return WKUserScript(source: source, injectionTime: injectionTime, forMainFrameOnly: forMainFrameOnly, in: .defaultClient) + // iOS 14.3 is required here because of a webkit bug in lower iOS versions with this API + if #available(iOS 14.3, *) { + return WKUserScript(source: source, injectionTime: injectionTime, forMainFrameOnly: forMainFrameOnly, in: .defaultClient) + } else { + return WKUserScript(source: source, injectionTime: injectionTime, forMainFrameOnly: forMainFrameOnly) + } } public class func createInPageContentWorld(source: String, injectionTime: WKUserScriptInjectionTime, forMainFrameOnly: Bool) -> WKUserScript { - return WKUserScript(source: source, injectionTime: injectionTime, forMainFrameOnly: forMainFrameOnly, in: .page) + // iOS 14.3 is required here because of a webkit bug in lower iOS versions with this API + if #available(iOS 14.3, *) { + return WKUserScript(source: source, injectionTime: injectionTime, forMainFrameOnly: forMainFrameOnly, in: .page) + } else { + return WKUserScript(source: source, injectionTime: injectionTime, forMainFrameOnly: forMainFrameOnly) + } } }