From aa583ad68a6ef8b850660734dd4db71e308c12a7 Mon Sep 17 00:00:00 2001 From: pichillilorenzo Date: Sun, 23 Sep 2018 21:38:31 +0200 Subject: [PATCH] added shouldOverrideUrlLoading method --- .idea/workspace.xml | 71 ++++--------------- .../InAppBrowserFlutterPlugin.java | 3 +- .../InAppBrowserOptions.java | 1 + .../InAppBrowserWebViewClient.java | 21 +++--- example/lib/main.dart | 8 ++- ios/Classes/InAppBrowserOptions.swift | 3 +- .../InAppBrowserWebViewController.swift | 12 +++- ios/Classes/SwiftFlutterPlugin.swift | 18 +++-- lib/flutter_inappbrowser.dart | 16 +++-- 9 files changed, 67 insertions(+), 86 deletions(-) diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 75ebc0be..a89f9522 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -11,11 +11,11 @@ - + - + @@ -38,8 +38,8 @@ - - + + @@ -50,8 +50,8 @@ - - + + @@ -139,8 +139,8 @@ @@ -176,53 +176,6 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - @@ -444,7 +397,7 @@ - + @@ -862,8 +815,8 @@ - - + + @@ -872,8 +825,8 @@ - - + + diff --git a/android/src/main/java/com/pichillilorenzo/flutter_inappbrowser/InAppBrowserFlutterPlugin.java b/android/src/main/java/com/pichillilorenzo/flutter_inappbrowser/InAppBrowserFlutterPlugin.java index a83353dd..35c2ebfb 100644 --- a/android/src/main/java/com/pichillilorenzo/flutter_inappbrowser/InAppBrowserFlutterPlugin.java +++ b/android/src/main/java/com/pichillilorenzo/flutter_inappbrowser/InAppBrowserFlutterPlugin.java @@ -78,7 +78,6 @@ public class InAppBrowserFlutterPlugin implements MethodCallHandler { channel.setMethodCallHandler(new InAppBrowserFlutterPlugin(registrar, registrar.activity())); } - @RequiresApi(api = Build.VERSION_CODES.KITKAT) @Override public void onMethodCall(MethodCall call, final Result result) { String source; @@ -443,7 +442,7 @@ public class InAppBrowserFlutterPlugin implements MethodCallHandler { public void run() { Map obj = new HashMap<>(); - channel.invokeMethod("exit", obj); + channel.invokeMethod("onExit", obj); // The JS protects against multiple calls, so this should happen only when // close() is called by other native code. diff --git a/android/src/main/java/com/pichillilorenzo/flutter_inappbrowser/InAppBrowserOptions.java b/android/src/main/java/com/pichillilorenzo/flutter_inappbrowser/InAppBrowserOptions.java index d0023c00..e07e4d36 100644 --- a/android/src/main/java/com/pichillilorenzo/flutter_inappbrowser/InAppBrowserOptions.java +++ b/android/src/main/java/com/pichillilorenzo/flutter_inappbrowser/InAppBrowserOptions.java @@ -29,6 +29,7 @@ public class InAppBrowserOptions { boolean domStorageEnabled = true; boolean useWideViewPort = true; boolean safeBrowsingEnabled = true; + boolean useShouldOverrideUrlLoading = false; public void parse(HashMap options) { Iterator it = options.entrySet().iterator(); diff --git a/android/src/main/java/com/pichillilorenzo/flutter_inappbrowser/InAppBrowserWebViewClient.java b/android/src/main/java/com/pichillilorenzo/flutter_inappbrowser/InAppBrowserWebViewClient.java index b2b2ef4d..0d336caa 100644 --- a/android/src/main/java/com/pichillilorenzo/flutter_inappbrowser/InAppBrowserWebViewClient.java +++ b/android/src/main/java/com/pichillilorenzo/flutter_inappbrowser/InAppBrowserWebViewClient.java @@ -28,7 +28,12 @@ public class InAppBrowserWebViewClient extends WebViewClient { @Override public boolean shouldOverrideUrlLoading(WebView webView, String url) { - //return true; + if (activity.options.useShouldOverrideUrlLoading) { + Map obj = new HashMap<>(); + obj.put("url", url); + InAppBrowserFlutterPlugin.channel.invokeMethod("shouldOverrideUrlLoading", obj); + return true; + } if (url.startsWith(WebView.SCHEME_TEL)) { try { @@ -81,11 +86,9 @@ public class InAppBrowserWebViewClient extends WebViewClient { Log.e(LOG_TAG, "Error sending sms " + url + ":" + e.toString()); } } - else { - return super.shouldOverrideUrlLoading(webView, url); - } - return false; + return super.shouldOverrideUrlLoading(webView, url); + } @@ -108,7 +111,7 @@ public class InAppBrowserWebViewClient extends WebViewClient { Map obj = new HashMap<>(); obj.put("url", url); - InAppBrowserFlutterPlugin.channel.invokeMethod("loadstart", obj); + InAppBrowserFlutterPlugin.channel.invokeMethod("onLoadStart", obj); } @@ -131,7 +134,7 @@ public class InAppBrowserWebViewClient extends WebViewClient { Map obj = new HashMap<>(); obj.put("url", url); - InAppBrowserFlutterPlugin.channel.invokeMethod("loadstop", obj); + InAppBrowserFlutterPlugin.channel.invokeMethod("onLoadStop", obj); } public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) { @@ -143,7 +146,7 @@ public class InAppBrowserWebViewClient extends WebViewClient { obj.put("url", failingUrl); obj.put("code", errorCode); obj.put("message", description); - InAppBrowserFlutterPlugin.channel.invokeMethod("loaderror", obj); + InAppBrowserFlutterPlugin.channel.invokeMethod("onLoadError", obj); } public void onReceivedSslError (WebView view, SslErrorHandler handler, SslError error) { @@ -175,7 +178,7 @@ public class InAppBrowserWebViewClient extends WebViewClient { break; } obj.put("message", "SslError: " + message); - InAppBrowserFlutterPlugin.channel.invokeMethod("loaderror", obj); + InAppBrowserFlutterPlugin.channel.invokeMethod("onLoadError", obj); handler.cancel(); } diff --git a/example/lib/main.dart b/example/lib/main.dart index 3d4ce3c6..9b500bdb 100644 --- a/example/lib/main.dart +++ b/example/lib/main.dart @@ -52,6 +52,11 @@ class MyInAppBrowser extends InAppBrowser { print("\n\nBrowser closed!\n\n"); } + @override + void shouldOverrideUrlLoading(String url) { + print("\n\n override $url\n\n"); + this.loadUrl(url); + } } MyInAppBrowser inAppBrowser = new MyInAppBrowser(); @@ -80,8 +85,9 @@ class _MyAppState extends State { body: new Center( child: new RaisedButton(onPressed: () { inAppBrowser.open("https://flutter.io/", options: { - //"hidden": true + //"hidden": true, //"toolbarTopFixedTitle": "Fixed title", + "useShouldOverrideUrlLoading": true //"hideUrlBar": true, //"toolbarTop": false, //"toolbarBottom": false diff --git a/ios/Classes/InAppBrowserOptions.swift b/ios/Classes/InAppBrowserOptions.swift index 0acbbae8..66424e2d 100644 --- a/ios/Classes/InAppBrowserOptions.swift +++ b/ios/Classes/InAppBrowserOptions.swift @@ -38,6 +38,7 @@ public class InAppBrowserOptions: NSObject { var allowsPictureInPictureMediaPlayback = true var javaScriptCanOpenWindowsAutomatically = false var javaScriptEnabled = true + var useShouldOverrideUrlLoading = false override init(){ super.init() @@ -45,7 +46,7 @@ public class InAppBrowserOptions: NSObject { public func parse(options: [String: Any]) { for (key, value) in options { - if self.value(forKey: key) != nil { + if self.responds(to: Selector(key)) { self.setValue(value, forKey: key) } } diff --git a/ios/Classes/InAppBrowserWebViewController.swift b/ios/Classes/InAppBrowserWebViewController.swift index 81b6d297..5a3c5ef2 100644 --- a/ios/Classes/InAppBrowserWebViewController.swift +++ b/ios/Classes/InAppBrowserWebViewController.swift @@ -426,6 +426,12 @@ class InAppBrowserWebViewController: UIViewController, WKUIDelegate, WKNavigatio let url = navigationAction.request.url + if (url != nil && navigationAction.navigationType == .linkActivated && (browserOptions?.useShouldOverrideUrlLoading)!) { + navigationDelegate?.shouldOverrideUrlLoading(webView, url: url!) + decisionHandler(.cancel) + return + } + if url != nil && (navigationAction.navigationType == .linkActivated || navigationAction.navigationType == .backForward) { currentURL = url updateUrlTextField(url: (url?.absoluteString)!) @@ -477,7 +483,7 @@ class InAppBrowserWebViewController: UIViewController, WKUIDelegate, WKNavigatio spinner.startAnimating() } - return (navigationDelegate?.webViewDidStartLoad(webView))! + return (navigationDelegate?.onLoadStart(webView))! } func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) { @@ -487,7 +493,7 @@ class InAppBrowserWebViewController: UIViewController, WKUIDelegate, WKNavigatio backButton.isEnabled = webView.canGoBack forwardButton.isEnabled = webView.canGoForward spinner.stopAnimating() - navigationDelegate?.webViewDidFinishLoad(webView) + navigationDelegate?.onLoadStop(webView) } // func webView(_ webView: WKWebView, @@ -505,6 +511,6 @@ class InAppBrowserWebViewController: UIViewController, WKUIDelegate, WKNavigatio backButton.isEnabled = webView.canGoBack forwardButton.isEnabled = webView.canGoForward spinner.stopAnimating() - navigationDelegate?.webViewDidFailLoadWithError(webView, error: error) + navigationDelegate?.onLoadError(webView, error: error) } } diff --git a/ios/Classes/SwiftFlutterPlugin.swift b/ios/Classes/SwiftFlutterPlugin.swift index e624f990..e7c6fd33 100644 --- a/ios/Classes/SwiftFlutterPlugin.swift +++ b/ios/Classes/SwiftFlutterPlugin.swift @@ -339,25 +339,29 @@ public class SwiftFlutterPlugin: NSObject, FlutterPlugin { injectDeferredObject(arguments["urlFile"] as! String, withWrapper: jsWrapper, result: result) } - func webViewDidStartLoad(_ webView: WKWebView) { + func onLoadStart(_ webView: WKWebView) { let url: String = webViewController!.currentURL!.absoluteString - channel.invokeMethod("loadstart", arguments: ["url": url]) + channel.invokeMethod("onLoadStart", arguments: ["url": url]) } - func webViewDidFinishLoad(_ webView: WKWebView) { + func onLoadStop(_ webView: WKWebView) { let url: String = webViewController!.currentURL!.absoluteString - channel.invokeMethod("loadstop", arguments: ["url": url]) + channel.invokeMethod("onLoadStop", arguments: ["url": url]) } - func webViewDidFailLoadWithError(_ webView: WKWebView, error: Error) { + func onLoadError(_ webView: WKWebView, error: Error) { let url: String = webViewController!.currentURL!.absoluteString let arguments = ["url": url, "code": error._code, "message": error.localizedDescription] as [String : Any] - channel.invokeMethod("loaderror", arguments: arguments) + channel.invokeMethod("onLoadError", arguments: arguments) + } + + func shouldOverrideUrlLoading(_ webView: WKWebView, url: URL) { + channel.invokeMethod("shouldOverrideUrlLoading", arguments: ["url": url.absoluteString]) } func browserExit() { - channel.invokeMethod("exit", arguments: []) + channel.invokeMethod("onExit", arguments: []) // Set navigationDelegate to nil to ensure no callbacks are received from it. webViewController?.navigationDelegate = nil diff --git a/lib/flutter_inappbrowser.dart b/lib/flutter_inappbrowser.dart index fbc01326..a2587d87 100644 --- a/lib/flutter_inappbrowser.dart +++ b/lib/flutter_inappbrowser.dart @@ -34,23 +34,27 @@ class InAppBrowser { Future _handleMethod(MethodCall call) async { switch(call.method) { - case "loadstart": + case "onLoadStart": String url = call.arguments["url"]; onLoadStart(url); break; - case "loadstop": + case "onLoadStop": String url = call.arguments["url"]; onLoadStop(url); break; - case "loaderror": + case "onLoadError": String url = call.arguments["url"]; int code = call.arguments["code"]; String message = call.arguments["message"]; onLoadError(url, code, message); break; - case "exit": + case "onExit": onExit(); break; + case "shouldOverrideUrlLoading": + String url = call.arguments["url"]; + shouldOverrideUrlLoading(url); + break; } return new Future.value(""); } @@ -226,4 +230,8 @@ class InAppBrowser { } + void shouldOverrideUrlLoading(String url) { + + } + }