Skip to content

Commit

Permalink
fix iOS setCookie #196, added iOS shared Process Pool #196, added scr…
Browse files Browse the repository at this point in the history
…ollTo and scrollBy methods
  • Loading branch information
pichillilorenzo committed Nov 19, 2019
1 parent 8486cd0 commit 3106caf
Show file tree
Hide file tree
Showing 10 changed files with 228 additions and 177 deletions.
264 changes: 105 additions & 159 deletions .idea/workspace.xml

Large diffs are not rendered by default.

3 changes: 1 addition & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,10 @@
- Added `onSafeBrowsingHit` event (available only for Android)
- Added `onJsAlert`, `onJsConfirm` and `onJsPrompt` events to manage javascript popup dialogs
- Added `onReceivedHttpAuthRequest` event
- Added `clearCache()` method
- Added `clearCache`, `scrollTo`, `scrollBy`, `getHtml`, `injectJavascriptFileFromAsset` and `injectCSSFileFromAsset` methods method
- Added `HttpAuthCredentialDatabase` class
- Added `onReceivedServerTrustAuthRequest` and `onReceivedClientCertRequest` events to manage SSL requests
- Added `onFindResultReceived` event, `findAllAsync`, `findNext` and `clearMatches` methods
- Added `getHtml`, `injectJavascriptFileFromAsset` and `injectCSSFileFromAsset` methods
- Added `shouldInterceptAjaxRequest`, `onAjaxReadyStateChange`, `onAjaxProgress` and `shouldInterceptFetchRequest` events with `useShouldInterceptAjaxRequest` and `useShouldInterceptFetchRequest` webview options
- Added `onNavigationStateChange` event
- Fun: added `getTRexRunnerHtml` and `getTRexRunnerCss` methods to get html (with javascript) and css to recreate the Chromium's t-rex runner game
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,22 @@ public void run() {
result.success(false);
}
break;
case "scrollTo":
if (webView != null) {
Integer x = (Integer) call.argument("x");
Integer y = (Integer) call.argument("y");
webView.scrollTo(x, y);
}
result.success(true);
break;
case "scrollBy":
if (webView != null) {
Integer x = (Integer) call.argument("x");
Integer y = (Integer) call.argument("y");
webView.scrollBy(x, y);
}
result.success(true);
break;
default:
result.notImplemented();
}
Expand Down
3 changes: 1 addition & 2 deletions example/ios/Flutter/flutter_export_environment.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@
# This is a generated file; do not edit or check into version control.
export "FLUTTER_ROOT=/Users/lorenzopichilli/flutter"
export "FLUTTER_APPLICATION_PATH=/Users/lorenzopichilli/Desktop/flutter_inappbrowser/example"
export "FLUTTER_TARGET=/Users/lorenzopichilli/Desktop/flutter_inappbrowser/example/lib/main.dart"
export "FLUTTER_TARGET=lib/main.dart"
export "FLUTTER_BUILD_DIR=build"
export "SYMROOT=${SOURCE_ROOT}/../build/ios"
export "FLUTTER_FRAMEWORK_DIR=/Users/lorenzopichilli/flutter/bin/cache/artifacts/engine/ios"
export "FLUTTER_BUILD_NAME=1.0.0"
export "FLUTTER_BUILD_NUMBER=1"
export "TRACK_WIDGET_CREATION=true"
29 changes: 20 additions & 9 deletions example/lib/in_app_webiew_example.screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -83,18 +83,21 @@ class _InAppWebViewExampleScreenState extends State<InAppWebViewExampleScreen> {
title: Text('InAppBrowser'),
onTap: () {
Navigator.popAndPushNamed(context, '/InAppBrowser');
dispose();
},
),
ListTile(
title: Text('ChromeSafariBrowser'),
onTap: () {
Navigator.popAndPushNamed(context, '/ChromeSafariBrowser');
dispose();
},
),
ListTile(
title: Text('InAppWebView'),
onTap: () {
Navigator.popAndPushNamed(context, '/');
dispose();
},
),
],
Expand All @@ -119,24 +122,24 @@ class _InAppWebViewExampleScreenState extends State<InAppWebViewExampleScreen> {
BoxDecoration(border: Border.all(color: Colors.blueAccent)),
child: InAppWebView(
//initialUrl: "https://www.youtube.com/embed/M7lc1UVf-VE?playsinline=1",
//initialUrl: "https://github.com",
initialUrl: "https://github.com",
//initialUrl: "chrome://safe-browsing/match?type=malware",
//initialUrl: "http://192.168.1.20:8081/",
//initialUrl: "https://192.168.1.20:4433/",
initialFile: "assets/index.html",
//initialFile: "assets/index.html",
initialHeaders: {},
initialOptions: InAppWebViewWidgetOptions(
inAppWebViewOptions: InAppWebViewOptions(
//disableVerticalScroll: false,
//disableHorizontalScroll: false,
debuggingEnabled: true,
clearCache: true,
useShouldOverrideUrlLoading: true,
//useShouldOverrideUrlLoading: true,
useOnTargetBlank: true,
useOnLoadResource: true,
useOnDownloadStart: true,
useShouldInterceptAjaxRequest: true,
useShouldInterceptFetchRequest: true,
//useOnLoadResource: true,
//useOnDownloadStart: true,
//useShouldInterceptAjaxRequest: true,
//useShouldInterceptFetchRequest: true,
//preferredContentMode: InAppWebViewUserPreferredContentMode.DESKTOP,
resourceCustomSchemes: [
"my-special-custom-scheme"
Expand Down Expand Up @@ -192,8 +195,7 @@ class _InAppWebViewExampleScreenState extends State<InAppWebViewExampleScreen> {
this.url = url;
});
},
onLoadStop:
(InAppWebViewController controller, String url) async {
onLoadStop: (InAppWebViewController controller, String url) async {
print("stopped $url");
if (Platform.isAndroid) {
controller.clearSslPreferences();
Expand All @@ -202,6 +204,15 @@ class _InAppWebViewExampleScreenState extends State<InAppWebViewExampleScreen> {
//controller.findAllAsync("flutter");
print(await controller.getFavicons());
print(await CookieManager.instance().getCookies(url: url));
//await CookieManager.instance().setCookie(url: url, name: "myCookie", value: "myValue");
//print(await CookieManager.instance().getCookies(url: url));
//await Future.delayed(const Duration(milliseconds: 2000));
//controller.scrollTo(x: 0, y: 500);
//await Future.delayed(const Duration(milliseconds: 2000));
//controller.scrollBy(x: 0, y: 150);
},
onScrollChanged: (InAppWebViewController controller, int x, int y) {
//print("\nSCROLLED\n");
},
onLoadError: (InAppWebViewController controller, String url,
int code, String message) async {
Expand Down
16 changes: 16 additions & 0 deletions ios/Classes/FlutterWebViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,22 @@ public class FlutterWebViewController: NSObject, FlutterPlatformView {
}
result(true)
break
case "scrollTo":
if webView != nil {
let x = arguments!["x"] as! Int
let y = arguments!["y"] as! Int
webView!.scrollTo(x: x, y: y)
}
result(true)
break
case "scrollBy":
if webView != nil {
let x = arguments!["x"] as! Int
let y = arguments!["y"] as! Int
webView!.scrollBy(x: x, y: y)
}
result(true)
break
case "removeFromSuperview":
webView!.removeFromSuperview()
result(true)
Expand Down
14 changes: 13 additions & 1 deletion ios/Classes/InAppWebView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -886,6 +886,8 @@ public class InAppWebView: WKWebView, UIScrollViewDelegate, WKUIDelegate, WKNavi
public static func preWKWebViewConfiguration(options: InAppWebViewOptions?) -> WKWebViewConfiguration {
let configuration = WKWebViewConfiguration()

configuration.processPool = WKProcessPoolManager.sharedProcessPool

if #available(iOS 10.0, *) {
configuration.mediaTypesRequiringUserActionForPlayback = ((options?.mediaPlaybackRequiresUserGesture)!) ? .all : []
} else {
Expand Down Expand Up @@ -1811,7 +1813,7 @@ public class InAppWebView: WKWebView, UIScrollViewDelegate, WKUIDelegate, WKNavi
scrollView.contentOffset = CGPoint(x: lastScrollX, y: scrollView.contentOffset.y);
}
}
if navigationDelegate != nil {
if navigationDelegate != nil && !(disableVerticalScroll && disableHorizontalScroll) {
let x = Int(scrollView.contentOffset.x / scrollView.contentScaleFactor)
let y = Int(scrollView.contentOffset.y / scrollView.contentScaleFactor)
onScrollChanged(x: x, y: y)
Expand Down Expand Up @@ -2127,6 +2129,16 @@ public class InAppWebView: WKWebView, UIScrollViewDelegate, WKUIDelegate, WKNavi
evaluateJavaScript("wkwebview_ClearMatches();", completionHandler: completionHandler)
}

func scrollTo(x: Int, y: Int) {
scrollView.setContentOffset(CGPoint(x: x, y: y), animated: false)
}

func scrollBy(x: Int, y: Int) {
let newX = CGFloat(x) + scrollView.contentOffset.x
let newY = CGFloat(y) + scrollView.contentOffset.y
scrollView.setContentOffset(CGPoint(x: newX, y: newY), animated: false)
}

public override func removeFromSuperview() {
configuration.userContentController.removeScriptMessageHandler(forName: "consoleLog")
configuration.userContentController.removeScriptMessageHandler(forName: "consoleDebug")
Expand Down
5 changes: 3 additions & 2 deletions ios/Classes/MyCookieManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,9 @@ class MyCookieManager: NSObject, FlutterPlugin {
properties[.secure] = (isSecure != nil && isSecure!) ? "TRUE" : "FALSE"

let cookie = HTTPCookie(properties: properties)!
MyCookieManager.httpCookieStore!.setCookie(cookie)
result(true)
MyCookieManager.httpCookieStore!.setCookie(cookie, completionHandler: {() in
result(true)
})
}

public static func getCookies(url: String, result: @escaping FlutterResult) {
Expand Down
13 changes: 13 additions & 0 deletions ios/Classes/WKProcessPoolManager.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//
// WKProcessPoolManager.swift
// flutter_inappbrowser
//
// Created by Lorenzo Pichilli on 19/11/2019.
//

import Foundation
import WebKit

class WKProcessPoolManager {
static let sharedProcessPool = WKProcessPool()
}
42 changes: 40 additions & 2 deletions lib/src/in_app_webview.dart
Original file line number Diff line number Diff line change
Expand Up @@ -336,8 +336,6 @@ class _InAppWebViewState extends State<InAppWebView> {
@override
void dispose(){
super.dispose();
if (Platform.isIOS)
_controller._channel.invokeMethod('removeFromSuperview');
}

void _onPlatformViewCreated(int id) {
Expand Down Expand Up @@ -1406,4 +1404,44 @@ class InAppWebViewController {
Future<String> getTRexRunnerCss() async {
return await rootBundle.loadString("packages/flutter_inappbrowser/t_rex_runner/t-rex.css");
}

///Scrolls the WebView to the position.
///
///[x] represents the x position to scroll to.
///
///[y] represents the y position to scroll to.
Future<void> scrollTo({@required int x, @required int y}) async {
assert(x != null && y != null);
Map<String, dynamic> args = <String, dynamic>{};
if (_inAppBrowserUuid != null && _inAppBrowser != null) {
_inAppBrowser.throwIsNotOpened();
args.putIfAbsent('uuid', () => _inAppBrowserUuid);
}
args.putIfAbsent('x', () => x);
args.putIfAbsent('y', () => y);
await _channel.invokeMethod('scrollTo', args);
}

///Moves the scrolled position of the WebView.
///
///[x] represents the amount of pixels to scroll by horizontally.
///
///[y] represents the amount of pixels to scroll by vertically.
Future<void> scrollBy({@required int x, @required int y}) async {
assert(x != null && y != null);
Map<String, dynamic> args = <String, dynamic>{};
if (_inAppBrowserUuid != null && _inAppBrowser != null) {
_inAppBrowser.throwIsNotOpened();
args.putIfAbsent('uuid', () => _inAppBrowserUuid);
}
args.putIfAbsent('x', () => x);
args.putIfAbsent('y', () => y);
await _channel.invokeMethod('scrollBy', args);
}

/*Future<void> dispose() async {
Map<String, dynamic> args = <String, dynamic>{};
if (Platform.isIOS)
await _channel.invokeMethod('removeFromSuperview', args);
}*/
}

0 comments on commit 3106caf

Please sign in to comment.