Skip to content

Commit

Permalink
Allow WKWebView to be inspectable
Browse files Browse the repository at this point in the history
  • Loading branch information
louischan-oursky committed Apr 3, 2024
2 parents 002c921 + a6a47fa commit 101e917
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 2 deletions.
1 change: 1 addition & 0 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -767,6 +767,7 @@ class _MyAppState extends State<MyApp> {
options: WebKitWebViewUIImplementationOptions(
ios: WebKitWebViewUIImplementationOptionsIOS(
modalPresentationStyle: ModalPresentationStyle.fullScreen,
isInspectable: true,
),
),
)
Expand Down
10 changes: 9 additions & 1 deletion ios/Classes/AGWKWebViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand All @@ -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)

Expand Down
5 changes: 4 additions & 1 deletion ios/Classes/SwiftAuthgearPlugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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":
Expand Down Expand Up @@ -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 {
Expand Down
2 changes: 2 additions & 0 deletions lib/src/native.dart
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ Future<String> openAuthorizeURLWithWebView({
String? modalPresentationStyle,
String? navigationBarBackgroundColor,
String? navigationBarButtonTintColor,
bool? iosIsInspectable,
String? actionBarBackgroundColor,
String? actionBarButtonTintColor,
}) async {
Expand All @@ -64,6 +65,7 @@ Future<String> openAuthorizeURLWithWebView({
"modalPresentationStyle": modalPresentationStyle,
"navigationBarBackgroundColor": navigationBarBackgroundColor,
"navigationBarButtonTintColor": navigationBarButtonTintColor,
"iosIsInspectable": iosIsInspectable,
"actionBarBackgroundColor": actionBarBackgroundColor,
"actionBarButtonTintColor": actionBarButtonTintColor,
});
Expand Down
3 changes: 3 additions & 0 deletions lib/src/ui_implementation.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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,
});
}

Expand Down Expand Up @@ -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:
Expand Down

0 comments on commit 101e917

Please sign in to comment.