From a6a47fa6214c61cc4f5e1d9bf71623af990a1ddd Mon Sep 17 00:00:00 2001 From: Tung Wu Date: Tue, 2 Apr 2024 19:09:40 +0800 Subject: [PATCH] Allow wkwebview to be inspectable in ios --- example/lib/main.dart | 1 + ios/Classes/AGWKWebViewController.swift | 10 +++++++++- ios/Classes/SwiftAuthgearPlugin.swift | 5 ++++- lib/src/native.dart | 2 ++ lib/src/ui_implementation.dart | 3 +++ 5 files changed, 19 insertions(+), 2 deletions(-) diff --git a/example/lib/main.dart b/example/lib/main.dart index a7a54a1f..8ed77af5 100644 --- a/example/lib/main.dart +++ b/example/lib/main.dart @@ -767,6 +767,7 @@ class _MyAppState extends State { options: WebKitWebViewUIImplementationOptions( ios: WebKitWebViewUIImplementationOptionsIOS( modalPresentationStyle: ModalPresentationStyle.fullScreen, + isInspectable: true, ), ), ) diff --git a/ios/Classes/AGWKWebViewController.swift b/ios/Classes/AGWKWebViewController.swift index b7ef33df..6b7152fc 100644 --- a/ios/Classes/AGWKWebViewController.swift +++ b/ios/Classes/AGWKWebViewController.swift @@ -20,16 +20,18 @@ class AGWKWebViewController: UIViewController, WKNavigationDelegate { private var completionHandler: CompletionHandler? private let webView: WKWebView private var result: URL? + private let isInspectable: Bool private let disableUserSelectSource: String = """ document.documentElement.style.webkitUserSelect = 'none'; document.documentElement.style.userSelect = 'none'; """ - init(url: URL, redirectURI: URL, completionHandler: @escaping CompletionHandler) { + init(url: URL, redirectURI: URL, isInspectable: Bool, completionHandler: @escaping CompletionHandler) { self.url = url self.redirectURI = redirectURI self.completionHandler = completionHandler + self.isInspectable = isInspectable let configuration = WKWebViewConfiguration() @@ -44,6 +46,12 @@ class AGWKWebViewController: UIViewController, WKNavigationDelegate { self.webView = WKWebView(frame: .zero, configuration: configuration) self.webView.translatesAutoresizingMaskIntoConstraints = false self.webView.allowsBackForwardNavigationGestures = true + if #available(iOS 16.4, *) { + self.webView.isInspectable = isInspectable + } else { + // isInspectable is not available under ios 16.4 + // The webview is always inspectable + } super.init(nibName: nil, bundle: nil) diff --git a/ios/Classes/SwiftAuthgearPlugin.swift b/ios/Classes/SwiftAuthgearPlugin.swift index b4b8bcc8..ad6c1c91 100644 --- a/ios/Classes/SwiftAuthgearPlugin.swift +++ b/ios/Classes/SwiftAuthgearPlugin.swift @@ -45,12 +45,14 @@ public class SwiftAuthgearPlugin: NSObject, FlutterPlugin, ASWebAuthenticationPr let modalPresentationStyle = UIModalPresentationStyle.from(string: arguments["modalPresentationStyle"] as? String) let navigationBarBackgroundColor = UIColor(argb: arguments["navigationBarBackgroundColor"] as? String) let navigationBarButtonTintColor = UIColor(argb: arguments["navigationBarButtonTintColor"] as? String) + let isInspectable = arguments["iosIsInspectable"] as? Bool self.openAuthorizeURLWithWebView( url: url, redirectURI: redirectURI, modalPresentationStyle: modalPresentationStyle, navigationBarBackgroundColor: navigationBarBackgroundColor, navigationBarButtonTintColor: navigationBarButtonTintColor, + isInspectable: isInspectable, result: result ) case "openURL": @@ -218,9 +220,10 @@ public class SwiftAuthgearPlugin: NSObject, FlutterPlugin, ASWebAuthenticationPr modalPresentationStyle: UIModalPresentationStyle, navigationBarBackgroundColor: UIColor?, navigationBarButtonTintColor: UIColor?, + isInspectable: Bool?, result: @escaping FlutterResult ) { - let controller = AGWKWebViewController(url: url, redirectURI: redirectURI) { resultURL, error in + let controller = AGWKWebViewController(url: url, redirectURI: redirectURI, isInspectable: isInspectable ?? false) { resultURL, error in if let error = error { let nsError = error as NSError if nsError.domain == AGWKWebViewControllerErrorDomain && nsError.code == AGWKWebViewControllerErrorCodeCanceledLogin { diff --git a/lib/src/native.dart b/lib/src/native.dart index a2a6fb07..4010a5fb 100644 --- a/lib/src/native.dart +++ b/lib/src/native.dart @@ -54,6 +54,7 @@ Future openAuthorizeURLWithWebView({ String? modalPresentationStyle, String? navigationBarBackgroundColor, String? navigationBarButtonTintColor, + bool? iosIsInspectable, String? actionBarBackgroundColor, String? actionBarButtonTintColor, }) async { @@ -64,6 +65,7 @@ Future openAuthorizeURLWithWebView({ "modalPresentationStyle": modalPresentationStyle, "navigationBarBackgroundColor": navigationBarBackgroundColor, "navigationBarButtonTintColor": navigationBarButtonTintColor, + "iosIsInspectable": iosIsInspectable, "actionBarBackgroundColor": actionBarBackgroundColor, "actionBarButtonTintColor": actionBarButtonTintColor, }); diff --git a/lib/src/ui_implementation.dart b/lib/src/ui_implementation.dart index 657b8ef6..c8ec126f 100644 --- a/lib/src/ui_implementation.dart +++ b/lib/src/ui_implementation.dart @@ -46,11 +46,13 @@ class WebKitWebViewUIImplementationOptionsIOS { final ModalPresentationStyle? modalPresentationStyle; final int? navigationBarBackgroundColor; final int? navigationBarButtonTintColor; + final bool? isInspectable; WebKitWebViewUIImplementationOptionsIOS({ this.modalPresentationStyle, this.navigationBarBackgroundColor, this.navigationBarButtonTintColor, + this.isInspectable, }); } @@ -95,6 +97,7 @@ class WebKitWebViewUIImplementation implements UIImplementation { options?.ios?.navigationBarBackgroundColor?.toRadixString(16), navigationBarButtonTintColor: options?.ios?.navigationBarButtonTintColor?.toRadixString(16), + iosIsInspectable: options?.ios?.isInspectable, actionBarBackgroundColor: options?.android?.actionBarBackgroundColor?.toRadixString(16), actionBarButtonTintColor: