From b8ec14d6b6025f88a3a3343cb197e4e4a78e6e9e Mon Sep 17 00:00:00 2001 From: Lorenzo Pichilli Date: Thu, 2 Jul 2020 17:30:55 +0200 Subject: [PATCH] Reverted calling handler.post on Android when a WebView is created, Fixed iOS extra bottom padding when opening the keyboard, fix #429 --- CHANGELOG.md | 6 ++++ README.md | 6 +++- .../InAppWebView/FlutterWebView.java | 28 ++++++++----------- example/.flutter-plugins-dependencies | 2 +- example/ios/Runner.xcodeproj/project.pbxproj | 4 +++ example/lib/in_app_webiew_example.screen.dart | 20 +++++++++++-- example/pubspec.yaml | 2 +- ios/Classes/InAppWebView.swift | 20 +++++++++++++ lib/src/X509Certificate/asn1_decoder.dart | 2 +- lib/src/headless_in_app_webview.dart | 2 +- lib/src/in_app_browser.dart | 2 ++ lib/src/in_app_webview.dart | 2 +- lib/src/webview.dart | 2 ++ pubspec.yaml | 2 +- 14 files changed, 74 insertions(+), 26 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 20dd7c27..f6a3a55f 100755 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,9 @@ +## 4.0.0+3 + +- Reverted calling `handler.post` on Android when a WebView is created +- Fixed iOS extra bottom padding when opening the keyboard +- Fixed "Build for web not working – The integer literal 9223372036854775807 can't be represented exactly in JavaScript" [#429](https://github.com/pichillilorenzo/flutter_inappwebview/issues/429) + ## 4.0.0 - Updated `onCreateWindow`, `onJsAlert`, `onJsConfirm`, `onJsPrompt` webview events diff --git a/README.md b/README.md index 8c4ad4ea..f27d98d9 100755 --- a/README.md +++ b/README.md @@ -14,7 +14,11 @@ A Flutter plugin that allows you to add an inline webview, to use an headless webview, and to open an in-app browser window. -### Requirements +## Articles/Resources + +- [InAppWebView: The Real Power of WebViews in Flutter](https://medium.com/flutter-community/inappwebview-the-real-power-of-webviews-in-flutter-c6d52374209d?source=friends_link&sk=cb74487219bcd85e610a670ee0b447d0) + +## Requirements - Dart sdk: ">=2.7.0 <3.0.0" - Flutter: ">=1.12.13+hotfix.5" diff --git a/android/src/main/java/com/pichillilorenzo/flutter_inappwebview/InAppWebView/FlutterWebView.java b/android/src/main/java/com/pichillilorenzo/flutter_inappwebview/InAppWebView/FlutterWebView.java index 39d1c68c..76b4797c 100755 --- a/android/src/main/java/com/pichillilorenzo/flutter_inappwebview/InAppWebView/FlutterWebView.java +++ b/android/src/main/java/com/pichillilorenzo/flutter_inappwebview/InAppWebView/FlutterWebView.java @@ -108,23 +108,17 @@ public class FlutterWebView implements PlatformView, MethodCallHandler { } } - final String finalInitialUrl = initialUrl; - Handler handler = new Handler(Looper.getMainLooper()); - handler.post(new Runnable() { - @Override - public void run() { - if (initialData != null) { - String data = initialData.get("data"); - String mimeType = initialData.get("mimeType"); - String encoding = initialData.get("encoding"); - String baseUrl = initialData.get("baseUrl"); - String historyUrl = initialData.get("historyUrl"); - webView.loadDataWithBaseURL(baseUrl, data, mimeType, encoding, historyUrl); - } - else - webView.loadUrl(finalInitialUrl, initialHeaders); - } - }); + if (initialData != null) { + String data = initialData.get("data"); + String mimeType = initialData.get("mimeType"); + String encoding = initialData.get("encoding"); + String baseUrl = initialData.get("baseUrl"); + String historyUrl = initialData.get("historyUrl"); + webView.loadDataWithBaseURL(baseUrl, data, mimeType, encoding, historyUrl); + } + else { + webView.loadUrl(initialUrl, initialHeaders); + } } if (containerView == null && id instanceof String) { diff --git a/example/.flutter-plugins-dependencies b/example/.flutter-plugins-dependencies index 868d49af..9a8c8ded 100755 --- a/example/.flutter-plugins-dependencies +++ b/example/.flutter-plugins-dependencies @@ -1 +1 @@ -{"info":"This is a generated file; do not edit or check into version control.","plugins":{"ios":[{"name":"e2e","path":"/Users/lorenzopichilli/flutter/.pub-cache/hosted/pub.dartlang.org/e2e-0.2.4+4/","dependencies":[]},{"name":"flutter_downloader","path":"/Users/lorenzopichilli/flutter/.pub-cache/hosted/pub.dartlang.org/flutter_downloader-1.4.4/","dependencies":[]},{"name":"flutter_inappwebview","path":"/Users/lorenzopichilli/Desktop/flutter_inappwebview/","dependencies":[]},{"name":"path_provider","path":"/Users/lorenzopichilli/flutter/.pub-cache/hosted/pub.dartlang.org/path_provider-1.6.10/","dependencies":[]},{"name":"permission_handler","path":"/Users/lorenzopichilli/flutter/.pub-cache/hosted/pub.dartlang.org/permission_handler-5.0.1/","dependencies":[]}],"android":[{"name":"e2e","path":"/Users/lorenzopichilli/flutter/.pub-cache/hosted/pub.dartlang.org/e2e-0.2.4+4/","dependencies":[]},{"name":"flutter_downloader","path":"/Users/lorenzopichilli/flutter/.pub-cache/hosted/pub.dartlang.org/flutter_downloader-1.4.4/","dependencies":[]},{"name":"flutter_inappwebview","path":"/Users/lorenzopichilli/Desktop/flutter_inappwebview/","dependencies":[]},{"name":"path_provider","path":"/Users/lorenzopichilli/flutter/.pub-cache/hosted/pub.dartlang.org/path_provider-1.6.10/","dependencies":[]},{"name":"permission_handler","path":"/Users/lorenzopichilli/flutter/.pub-cache/hosted/pub.dartlang.org/permission_handler-5.0.1/","dependencies":[]}],"macos":[{"name":"path_provider_macos","path":"/Users/lorenzopichilli/flutter/.pub-cache/hosted/pub.dartlang.org/path_provider_macos-0.0.4+3/","dependencies":[]}],"linux":[{"name":"path_provider_linux","path":"/Users/lorenzopichilli/flutter/.pub-cache/hosted/pub.dartlang.org/path_provider_linux-0.0.1+1/","dependencies":[]}],"windows":[],"web":[]},"dependencyGraph":[{"name":"e2e","dependencies":[]},{"name":"flutter_downloader","dependencies":[]},{"name":"flutter_inappwebview","dependencies":[]},{"name":"path_provider","dependencies":["path_provider_macos","path_provider_linux"]},{"name":"path_provider_linux","dependencies":[]},{"name":"path_provider_macos","dependencies":[]},{"name":"permission_handler","dependencies":[]}],"date_created":"2020-06-30 14:26:38.633976","version":"1.17.4"} \ No newline at end of file +{"info":"This is a generated file; do not edit or check into version control.","plugins":{"ios":[{"name":"e2e","path":"/Users/lorenzopichilli/flutter/.pub-cache/hosted/pub.dartlang.org/e2e-0.2.4+4/","dependencies":[]},{"name":"flutter_downloader","path":"/Users/lorenzopichilli/flutter/.pub-cache/hosted/pub.dartlang.org/flutter_downloader-1.4.4/","dependencies":[]},{"name":"flutter_inappwebview","path":"/Users/lorenzopichilli/Desktop/flutter_inappwebview/","dependencies":[]},{"name":"path_provider","path":"/Users/lorenzopichilli/flutter/.pub-cache/hosted/pub.dartlang.org/path_provider-1.6.10/","dependencies":[]},{"name":"permission_handler","path":"/Users/lorenzopichilli/flutter/.pub-cache/hosted/pub.dartlang.org/permission_handler-5.0.1/","dependencies":[]},{"name":"url_launcher","path":"/Users/lorenzopichilli/flutter/.pub-cache/hosted/pub.dartlang.org/url_launcher-5.4.11/","dependencies":[]},{"name":"webview_flutter","path":"/Users/lorenzopichilli/flutter/.pub-cache/hosted/pub.dartlang.org/webview_flutter-0.3.22+1/","dependencies":[]}],"android":[{"name":"e2e","path":"/Users/lorenzopichilli/flutter/.pub-cache/hosted/pub.dartlang.org/e2e-0.2.4+4/","dependencies":[]},{"name":"flutter_downloader","path":"/Users/lorenzopichilli/flutter/.pub-cache/hosted/pub.dartlang.org/flutter_downloader-1.4.4/","dependencies":[]},{"name":"flutter_inappwebview","path":"/Users/lorenzopichilli/Desktop/flutter_inappwebview/","dependencies":[]},{"name":"path_provider","path":"/Users/lorenzopichilli/flutter/.pub-cache/hosted/pub.dartlang.org/path_provider-1.6.10/","dependencies":[]},{"name":"permission_handler","path":"/Users/lorenzopichilli/flutter/.pub-cache/hosted/pub.dartlang.org/permission_handler-5.0.1/","dependencies":[]},{"name":"url_launcher","path":"/Users/lorenzopichilli/flutter/.pub-cache/hosted/pub.dartlang.org/url_launcher-5.4.11/","dependencies":[]},{"name":"webview_flutter","path":"/Users/lorenzopichilli/flutter/.pub-cache/hosted/pub.dartlang.org/webview_flutter-0.3.22+1/","dependencies":[]}],"macos":[{"name":"path_provider_macos","path":"/Users/lorenzopichilli/flutter/.pub-cache/hosted/pub.dartlang.org/path_provider_macos-0.0.4+3/","dependencies":[]},{"name":"url_launcher_macos","path":"/Users/lorenzopichilli/flutter/.pub-cache/hosted/pub.dartlang.org/url_launcher_macos-0.0.1+7/","dependencies":[]}],"linux":[{"name":"path_provider_linux","path":"/Users/lorenzopichilli/flutter/.pub-cache/hosted/pub.dartlang.org/path_provider_linux-0.0.1+1/","dependencies":[]}],"windows":[],"web":[{"name":"url_launcher_web","path":"/Users/lorenzopichilli/flutter/.pub-cache/hosted/pub.dartlang.org/url_launcher_web-0.1.2/","dependencies":[]}]},"dependencyGraph":[{"name":"e2e","dependencies":[]},{"name":"flutter_downloader","dependencies":[]},{"name":"flutter_inappwebview","dependencies":[]},{"name":"path_provider","dependencies":["path_provider_macos","path_provider_linux"]},{"name":"path_provider_linux","dependencies":[]},{"name":"path_provider_macos","dependencies":[]},{"name":"permission_handler","dependencies":[]},{"name":"url_launcher","dependencies":["url_launcher_web","url_launcher_macos"]},{"name":"url_launcher_macos","dependencies":[]},{"name":"url_launcher_web","dependencies":[]},{"name":"webview_flutter","dependencies":[]}],"date_created":"2020-07-02 09:44:29.597292","version":"1.17.4"} \ No newline at end of file diff --git a/example/ios/Runner.xcodeproj/project.pbxproj b/example/ios/Runner.xcodeproj/project.pbxproj index 98e6ef55..fe088145 100644 --- a/example/ios/Runner.xcodeproj/project.pbxproj +++ b/example/ios/Runner.xcodeproj/project.pbxproj @@ -259,6 +259,8 @@ "${BUILT_PRODUCTS_DIR}/flutter_downloader/flutter_downloader.framework", "${BUILT_PRODUCTS_DIR}/flutter_inappwebview/flutter_inappwebview.framework", "${BUILT_PRODUCTS_DIR}/path_provider/path_provider.framework", + "${BUILT_PRODUCTS_DIR}/url_launcher/url_launcher.framework", + "${BUILT_PRODUCTS_DIR}/webview_flutter/webview_flutter.framework", ); name = "[CP] Embed Pods Frameworks"; outputPaths = ( @@ -267,6 +269,8 @@ "${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/flutter_downloader.framework", "${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/flutter_inappwebview.framework", "${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/path_provider.framework", + "${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/url_launcher.framework", + "${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/webview_flutter.framework", ); runOnlyForDeploymentPostprocessing = 0; shellPath = /bin/sh; diff --git a/example/lib/in_app_webiew_example.screen.dart b/example/lib/in_app_webiew_example.screen.dart index 61893af3..29c68a75 100755 --- a/example/lib/in_app_webiew_example.screen.dart +++ b/example/lib/in_app_webiew_example.screen.dart @@ -3,6 +3,7 @@ import 'dart:io'; import 'package:flutter/material.dart'; import 'package:flutter_inappwebview/flutter_inappwebview.dart'; +import 'package:url_launcher/url_launcher.dart'; import 'main.dart'; @@ -86,7 +87,7 @@ class _InAppWebViewExampleScreenState extends State { initialOptions: InAppWebViewGroupOptions( crossPlatform: InAppWebViewOptions( debuggingEnabled: true, - useShouldOverrideUrlLoading: true + useShouldOverrideUrlLoading: true, ), ), onWebViewCreated: (InAppWebViewController controller) { @@ -100,7 +101,22 @@ class _InAppWebViewExampleScreenState extends State { }); }, shouldOverrideUrlLoading: (controller, shouldOverrideUrlLoadingRequest) async { - print("shouldOverrideUrlLoading"); + var url = shouldOverrideUrlLoadingRequest.url; + var uri = Uri.parse(url); + + if (!["http", "https", "file", + "chrome", "data", "javascript", + "about"].contains(uri.scheme)) { + if (await canLaunch(url)) { + // Launch the App + await launch( + url, + ); + // and cancel the request + return ShouldOverrideUrlLoadingAction.CANCEL; + } + } + return ShouldOverrideUrlLoadingAction.ALLOW; }, onLoadStop: (InAppWebViewController controller, String url) async { diff --git a/example/pubspec.yaml b/example/pubspec.yaml index c4375f72..6b0beb76 100755 --- a/example/pubspec.yaml +++ b/example/pubspec.yaml @@ -23,7 +23,7 @@ dependencies: flutter_downloader: ^1.4.4 path_provider: ^1.6.9 permission_handler: ^5.0.0+hotfix.6 - # webview_flutter: ^0.3.22+1 + url_launcher: ^5.4.11 # connectivity: ^0.4.5+6 flutter_inappwebview: path: ../ diff --git a/ios/Classes/InAppWebView.swift b/ios/Classes/InAppWebView.swift index 7e59336d..ad1d2970 100755 --- a/ios/Classes/InAppWebView.swift +++ b/ios/Classes/InAppWebView.swift @@ -869,6 +869,26 @@ public class InAppWebView: WKWebView, UIScrollViewDelegate, WKUIDelegate, WKNavi self.longPressRecognizer!.addTarget(self, action: #selector(longPressGestureDetected)) } + override public var frame: CGRect { + get { + return super.frame + } + set { + super.frame = newValue + + self.scrollView.contentInset = UIEdgeInsets.zero; + if #available(iOS 11, *) { + // Above iOS 11, adjust contentInset to compensate the adjustedContentInset so the sum will + // always be 0. + if (scrollView.adjustedContentInset != UIEdgeInsets.zero) { + let insetToAdjust = self.scrollView.adjustedContentInset; + scrollView.contentInset = UIEdgeInsets(top: -insetToAdjust.top, left: -insetToAdjust.left, + bottom: -insetToAdjust.bottom, right: -insetToAdjust.right); + } + } + } + } + required public init(coder aDecoder: NSCoder) { super.init(coder: aDecoder)! } diff --git a/lib/src/X509Certificate/asn1_decoder.dart b/lib/src/X509Certificate/asn1_decoder.dart index 13aecef8..94a85305 100644 --- a/lib/src/X509Certificate/asn1_decoder.dart +++ b/lib/src/X509Certificate/asn1_decoder.dart @@ -160,7 +160,7 @@ class ASN1DERDecoder { static List loadSubContent({@required Iterator iterator}) { var len = getContentLength(iterator: iterator); - int int64MaxValue = 9223372036854775807; + int int64MaxValue = double.maxFinite.toInt(); if (len >= BigInt.from(int64MaxValue)) { return []; diff --git a/lib/src/headless_in_app_webview.dart b/lib/src/headless_in_app_webview.dart index 108e087e..5906a715 100644 --- a/lib/src/headless_in_app_webview.dart +++ b/lib/src/headless_in_app_webview.dart @@ -198,7 +198,7 @@ class HeadlessInAppWebView implements WebView { @override final Future Function(InAppWebViewController controller, - CreateWindowRequest onCreateWindowRequest) onCreateWindow; + CreateWindowRequest createWindowRequest) onCreateWindow; @override final void Function(InAppWebViewController controller) onCloseWindow; diff --git a/lib/src/in_app_browser.dart b/lib/src/in_app_browser.dart index e7b9a73d..4359705d 100755 --- a/lib/src/in_app_browser.dart +++ b/lib/src/in_app_browser.dart @@ -347,6 +347,8 @@ class InAppBrowser { /// ///[createWindowRequest] represents the request. /// + ///**NOTE**: to allow JavaScript to open windows, you need to set [InAppWebViewOptions.javaScriptCanOpenWindowsAutomatically] option to `true`. + /// ///**NOTE**: on Android you need to set [AndroidInAppWebViewOptions.supportMultipleWindows] option to `true`. /// ///**NOTE**: on iOS, setting these initial options: [InAppWebViewOptions.supportZoom], [InAppWebViewOptions.useOnLoadResource], [InAppWebViewOptions.useShouldInterceptAjaxRequest], diff --git a/lib/src/in_app_webview.dart b/lib/src/in_app_webview.dart index 2b3eb56c..c031c3f3 100755 --- a/lib/src/in_app_webview.dart +++ b/lib/src/in_app_webview.dart @@ -162,7 +162,7 @@ class InAppWebView extends StatefulWidget implements WebView { @override final Future Function(InAppWebViewController controller, - CreateWindowRequest onCreateWindowRequest) onCreateWindow; + CreateWindowRequest createWindowRequest) onCreateWindow; @override final void Function(InAppWebViewController controller) onCloseWindow; diff --git a/lib/src/webview.dart b/lib/src/webview.dart index b9947e60..92d9fb95 100644 --- a/lib/src/webview.dart +++ b/lib/src/webview.dart @@ -138,6 +138,8 @@ abstract class WebView { /// ///[createWindowRequest] represents the request. /// + ///**NOTE**: to allow JavaScript to open windows, you need to set [InAppWebViewOptions.javaScriptCanOpenWindowsAutomatically] option to `true`. + /// ///**NOTE**: on Android you need to set [AndroidInAppWebViewOptions.supportMultipleWindows] option to `true`. /// ///**NOTE**: on iOS, setting these initial options: [InAppWebViewOptions.supportZoom], [InAppWebViewOptions.useOnLoadResource], [InAppWebViewOptions.useShouldInterceptAjaxRequest], diff --git a/pubspec.yaml b/pubspec.yaml index c88a5141..d2fe8bb4 100755 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,6 +1,6 @@ name: flutter_inappwebview description: A Flutter plugin that allows you to add an inline webview, to use an headless webview, and to open an in-app browser window. -version: 4.0.0 +version: 4.0.0+3 homepage: https://github.com/pichillilorenzo/flutter_inappwebview environment: