Commit ab7b1b68 authored by pichillilorenzo's avatar pichillilorenzo

added and class, added method for and

parent 456be7d7
This diff is collapsed.
## 0.5.4
- added `WebHistory` and `WebHistoryItem` class
- added `getCopyBackForwardList` method for `InAppWebView` and `InAppBrowser`
## 0.5.3 ## 0.5.3
- added `CookieManager` class - added `CookieManager` class
......
# Flutter InAppBrowser Plugin [![Share on Twitter](https://img.shields.io/twitter/url/http/shields.io.svg?style=social)](https://twitter.com/intent/tweet?text=Flutter%20InAppBrowser%20plugin!&url=https://github.com/pichillilorenzo/flutter_inappbrowser&hashtags=flutter,flutterio,dart,dartlang,webview) [![Share on Facebook](https://img.shields.io/badge/share-facebook-blue.svg?longCache=true&style=flat&colorB=%234267b2)](https://www.facebook.com/sharer/sharer.php?u=https%3A//github.com/pichillilorenzo/flutter_inappbrowser) # Flutter InAppBrowser Plugin [![Share on Twitter](https://img.shields.io/twitter/url/http/shields.io.svg?style=social)](https://twitter.com/intent/tweet?text=Flutter%20InAppBrowser%20plugin!&url=https://github.com/pichillilorenzo/flutter_inappbrowser&hashtags=flutter,flutterio,dart,dartlang,webview) [![Share on Facebook](https://img.shields.io/badge/share-facebook-blue.svg?longCache=true&style=flat&colorB=%234267b2)](https://www.facebook.com/sharer/sharer.php?u=https%3A//github.com/pichillilorenzo/flutter_inappbrowser)
[![Pub](https://img.shields.io/pub/v/flutter_inappbrowser.svg)](https://pub.dartlang.org/packages/flutter_inappbrowser) [![Pub](https://img.shields.io/pub/v/flutter_inappbrowser.svg)](https://pub.dartlang.org/packages/flutter_inappbrowser)
[![Awesome Flutter](https://img.shields.io/badge/Awesome-Flutter-blue.svg?longCache=true&style=flat-square)](https://stackoverflow.com/questions/tagged/flutter?sort=votes)
[![License](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](/LICENSE) [![License](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](/LICENSE)
[![Donate to this project using Paypal](https://img.shields.io/badge/paypal-donate-yellow.svg)](https://www.paypal.me/LorenzoPichilli) [![Donate to this project using Paypal](https://img.shields.io/badge/paypal-donate-yellow.svg)](https://www.paypal.me/LorenzoPichilli)
...@@ -9,6 +10,10 @@ ...@@ -9,6 +10,10 @@
A Flutter plugin that allows you to add an inline webview or open an in-app browser window. A Flutter plugin that allows you to add an inline webview or open an in-app browser window.
This plugin is inspired by the popular [cordova-plugin-inappbrowser](https://github.com/apache/cordova-plugin-inappbrowser)! This plugin is inspired by the popular [cordova-plugin-inappbrowser](https://github.com/apache/cordova-plugin-inappbrowser)!
### Requirements
- Dart sdk: ">=2.1.0-dev.7.1 <3.0.0"
- Flutter: ">=0.10.1 <2.0.0"
### IMPORTANT Note for iOS ### IMPORTANT Note for iOS
To be able to use this plugin on iOS, you need to create the Flutter App with `flutter create -i swift` (see [flutter/flutter#13422 (comment)](https://github.com/flutter/flutter/issues/13422#issuecomment-392133780)), otherwise, you will get this message: To be able to use this plugin on iOS, you need to create the Flutter App with `flutter create -i swift` (see [flutter/flutter#13422 (comment)](https://github.com/flutter/flutter/issues/13422#issuecomment-392133780)), otherwise, you will get this message:
``` ```
...@@ -29,7 +34,7 @@ First, add `flutter_inappbrowser` as a [dependency in your pubspec.yaml file](ht ...@@ -29,7 +34,7 @@ First, add `flutter_inappbrowser` as a [dependency in your pubspec.yaml file](ht
## Usage ## Usage
Classes: Classes:
- [InAppWebView](#inappwebview-class): Flutter Widget for adding an **inline native WebView** integrated in the flutter widget tree. [**Available only for Android** ([AndroidView](https://docs.flutter.io/flutter/widgets/AndroidView-class.html)) at this moment]. - [InAppWebView](#inappwebview-class): Flutter Widget for adding an **inline native WebView** integrated in the flutter widget tree. [**Available only for Android** ([AndroidView](https://docs.flutter.io/flutter/widgets/AndroidView-class.html)) at this moment. For iOS, it will be available as soon as the Flutter team will release the corresponding dart class.].
- [InAppBrowser](#inappbrowser-class): In-App Browser using native WebView. - [InAppBrowser](#inappbrowser-class): In-App Browser using native WebView.
- [ChromeSafariBrowser](#chromesafaribrowser-class): In-App Browser using [Chrome Custom Tabs](https://developer.android.com/reference/android/support/customtabs/package-summary) on Android / [SFSafariViewController](https://developer.apple.com/documentation/safariservices/sfsafariviewcontroller) on iOS. - [ChromeSafariBrowser](#chromesafaribrowser-class): In-App Browser using [Chrome Custom Tabs](https://developer.android.com/reference/android/support/customtabs/package-summary) on Android / [SFSafariViewController](https://developer.apple.com/documentation/safariservices/sfsafariviewcontroller) on iOS.
- [InAppLocalhostServer](#inapplocalhostserver-class): This class allows you to create a simple server on `http://localhost:[port]/`. The default `port` value is `8080`. - [InAppLocalhostServer](#inapplocalhostserver-class): This class allows you to create a simple server on `http://localhost:[port]/`. The default `port` value is `8080`.
...@@ -42,6 +47,7 @@ Flutter Widget for adding an **inline native WebView** integrated in the flutter ...@@ -42,6 +47,7 @@ Flutter Widget for adding an **inline native WebView** integrated in the flutter
So, if you want use it, you can but you will have some limitation such as the inability to use the keyboard! So, if you want use it, you can but you will have some limitation such as the inability to use the keyboard!
**Available only for Android** ([AndroidView](https://docs.flutter.io/flutter/widgets/AndroidView-class.html)) at this moment. **Available only for Android** ([AndroidView](https://docs.flutter.io/flutter/widgets/AndroidView-class.html)) at this moment.
For iOS, it will be available as soon as the Flutter team will release the corresponding dart class.
Use `InAppWebViewController` to control the WebView instance. Use `InAppWebViewController` to control the WebView instance.
Example: Example:
......
...@@ -4,7 +4,6 @@ import android.app.Activity; ...@@ -4,7 +4,6 @@ import android.app.Activity;
import android.content.Context; import android.content.Context;
import android.util.Log; import android.util.Log;
import android.view.View; import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebChromeClient; import android.webkit.WebChromeClient;
import android.webkit.WebView; import android.webkit.WebView;
import android.webkit.WebViewClient; import android.webkit.WebViewClient;
...@@ -186,6 +185,9 @@ public class FlutterWebView implements PlatformView, MethodCallHandler { ...@@ -186,6 +185,9 @@ public class FlutterWebView implements PlatformView, MethodCallHandler {
case "getOptions": case "getOptions":
result.success((webView != null) ? webView.getOptions() : null); result.success((webView != null) ? webView.getOptions() : null);
break; break;
case "getCopyBackForwardList":
result.success((webView != null) ? webView.getCopyBackForwardList() : null);
break;
case "dispose": case "dispose":
dispose(); dispose();
result.success(true); result.success(true);
...@@ -201,6 +203,7 @@ public class FlutterWebView implements PlatformView, MethodCallHandler { ...@@ -201,6 +203,7 @@ public class FlutterWebView implements PlatformView, MethodCallHandler {
webView.setWebChromeClient(new WebChromeClient()); webView.setWebChromeClient(new WebChromeClient());
webView.setWebViewClient(new WebViewClient() { webView.setWebViewClient(new WebViewClient() {
public void onPageFinished(WebView view, String url) { public void onPageFinished(WebView view, String url) {
webView.destroy();
webView = null; webView = null;
} }
}); });
......
...@@ -381,4 +381,11 @@ public class InAppBrowserActivity extends AppCompatActivity { ...@@ -381,4 +381,11 @@ public class InAppBrowserActivity extends AppCompatActivity {
else else
result.success(""); result.success("");
} }
public HashMap<String, Object> getCopyBackForwardList() {
if (webView != null)
return webView.getCopyBackForwardList();
return null;
}
} }
...@@ -265,6 +265,9 @@ public class InAppBrowserFlutterPlugin implements MethodCallHandler { ...@@ -265,6 +265,9 @@ public class InAppBrowserFlutterPlugin implements MethodCallHandler {
case "getOptions": case "getOptions":
result.success(getOptions(uuid)); result.success(getOptions(uuid));
break; break;
case "getCopyBackForwardList":
result.success(getCopyBackForwardList(uuid));
break;
default: default:
result.notImplemented(); result.notImplemented();
} }
...@@ -556,4 +559,11 @@ public class InAppBrowserFlutterPlugin implements MethodCallHandler { ...@@ -556,4 +559,11 @@ public class InAppBrowserFlutterPlugin implements MethodCallHandler {
return null; return null;
} }
public HashMap<String, Object> getCopyBackForwardList(String uuid) {
InAppBrowserActivity inAppBrowserActivity = webViewActivities.get(uuid);
if (inAppBrowserActivity != null)
return inAppBrowserActivity.getCopyBackForwardList();
return null;
}
} }
...@@ -11,6 +11,8 @@ import android.util.JsonToken; ...@@ -11,6 +11,8 @@ import android.util.JsonToken;
import android.util.Log; import android.util.Log;
import android.webkit.CookieManager; import android.webkit.CookieManager;
import android.webkit.ValueCallback; import android.webkit.ValueCallback;
import android.webkit.WebBackForwardList;
import android.webkit.WebHistoryItem;
import android.webkit.WebSettings; import android.webkit.WebSettings;
import android.webkit.WebView; import android.webkit.WebView;
...@@ -22,7 +24,9 @@ import com.pichillilorenzo.flutter_inappbrowser.Util; ...@@ -22,7 +24,9 @@ import com.pichillilorenzo.flutter_inappbrowser.Util;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.StringReader; import java.io.StringReader;
import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.List;
import java.util.Map; import java.util.Map;
import io.flutter.plugin.common.MethodChannel; import io.flutter.plugin.common.MethodChannel;
...@@ -394,4 +398,31 @@ public class InAppWebView extends WebView { ...@@ -394,4 +398,31 @@ public class InAppWebView extends WebView {
} }
}); });
} }
public HashMap<String, Object> getCopyBackForwardList() {
WebBackForwardList currentList = copyBackForwardList();
int currentSize = currentList.getSize();
int currentIndex = currentList.getCurrentIndex();
List<HashMap<String, String>> history = new ArrayList<HashMap<String, String>>();
for(int i = 0; i < currentSize; i++) {
WebHistoryItem historyItem = currentList.getItemAtIndex(i);
HashMap<String, String> historyItemMap = new HashMap<>();
historyItemMap.put("originalUrl", historyItem.getOriginalUrl());
historyItemMap.put("title", historyItem.getTitle());
historyItemMap.put("url", historyItem.getUrl());
history.add(historyItemMap);
}
HashMap<String, Object> result = new HashMap<>();
result.put("history", history);
result.put("currentIndex", currentIndex);
return result;
}
} }
...@@ -16,6 +16,12 @@ class MyInAppBrowser extends InAppBrowser { ...@@ -16,6 +16,12 @@ class MyInAppBrowser extends InAppBrowser {
Future onLoadStop(String url) async { Future onLoadStop(String url) async {
print("\n\nStopped $url\n\n"); print("\n\nStopped $url\n\n");
WebHistory history = await this.webViewController.getCopyBackForwardList();
print(history.list.length);
print(history.list[history.currentIndex].url);
for(WebHistoryItem item in history.list) {
print(item.title);
}
// print(await this.webViewController.canGoBack()); // print(await this.webViewController.canGoBack());
// print(await this.webViewController.canGoForward()); // print(await this.webViewController.canGoForward());
......
...@@ -925,4 +925,30 @@ class InAppBrowserWebViewController: UIViewController, WKUIDelegate, WKNavigatio ...@@ -925,4 +925,30 @@ class InAppBrowserWebViewController: UIViewController, WKUIDelegate, WKNavigatio
self.navigationDelegate?.onProgressChanged(uuid: self.uuid, webView: self.webView, progress: progress) self.navigationDelegate?.onProgressChanged(uuid: self.uuid, webView: self.webView, progress: progress)
} }
} }
func getCopyBackForwardList() -> [String: Any] {
let currentList = self.webView.backForwardList
let currentIndex = currentList.backList.count
var completeList = currentList.backList
if currentList.currentItem != nil {
completeList.append(currentList.currentItem!)
}
completeList.append(contentsOf: currentList.forwardList)
var history: [[String: String]] = []
for historyItem in completeList {
var historyItemMap: [String: String] = [:]
historyItemMap["originalUrl"] = historyItem.initialURL.absoluteString
historyItemMap["title"] = historyItem.title
historyItemMap["url"] = historyItem.url.absoluteString
history.append(historyItemMap)
}
var result: [String: Any] = [:]
result["history"] = history
result["currentIndex"] = currentIndex
return result;
}
} }
...@@ -193,6 +193,9 @@ public class SwiftFlutterPlugin: NSObject, FlutterPlugin { ...@@ -193,6 +193,9 @@ public class SwiftFlutterPlugin: NSObject, FlutterPlugin {
case "getOptions": case "getOptions":
result(self.getOptions(uuid: uuid)) result(self.getOptions(uuid: uuid))
break break
case "getCopyBackForwardList":
result(self.getCopyBackForwardList(uuid: uuid));
break;
default: default:
result(FlutterMethodNotImplemented) result(FlutterMethodNotImplemented)
break break
...@@ -708,4 +711,11 @@ public class SwiftFlutterPlugin: NSObject, FlutterPlugin { ...@@ -708,4 +711,11 @@ public class SwiftFlutterPlugin: NSObject, FlutterPlugin {
return nil return nil
} }
func getCopyBackForwardList(uuid: String) -> [String: Any]? {
if let webViewController = self.webViewControllers[uuid] {
return webViewController!.getCopyBackForwardList()
}
return nil
}
} }
...@@ -658,6 +658,7 @@ class InAppWebViewController { ...@@ -658,6 +658,7 @@ class InAppWebViewController {
String _inAppBrowserUuid; String _inAppBrowserUuid;
InAppBrowser _inAppBrowser; InAppBrowser _inAppBrowser;
InAppWebViewController(int id, InAppWebView widget) { InAppWebViewController(int id, InAppWebView widget) {
_id = id; _id = id;
_channel = MethodChannel('com.pichillilorenzo/flutter_inappwebview_$id'); _channel = MethodChannel('com.pichillilorenzo/flutter_inappwebview_$id');
...@@ -1030,12 +1031,59 @@ class InAppWebViewController { ...@@ -1030,12 +1031,59 @@ class InAppWebViewController {
return options; return options;
} }
///Gets the WebHistory for this WebView. This contains the back/forward list for use in querying each item in the history stack.
///This contains only a snapshot of the current state.
///Multiple calls to this method may return different objects.
///The object returned from this method will not be updated to reflect any new state.
Future<WebHistory> getCopyBackForwardList() async {
Map<String, dynamic> args = <String, dynamic>{};
if (_inAppBrowserUuid != null) {
_inAppBrowser._throwIsNotOpened();
args.putIfAbsent('uuid', () => _inAppBrowserUuid);
}
Map<dynamic, dynamic> result = await _channel.invokeMethod('getCopyBackForwardList', args);
result = result.cast<String, dynamic>();
List<dynamic> historyListMap = result["history"];
historyListMap = historyListMap.cast<LinkedHashMap<dynamic, dynamic>>();
int currentIndex = result["currentIndex"];
List<WebHistoryItem> historyList = List();
for(LinkedHashMap<dynamic, dynamic> historyItem in historyListMap) {
historyList.add(WebHistoryItem(historyItem["originalUrl"], historyItem["title"], historyItem["url"]));
}
return WebHistory(historyList, currentIndex);
}
Future<void> _dispose() async { Future<void> _dispose() async {
await _channel.invokeMethod('dispose'); await _channel.invokeMethod('dispose');
} }
} }
///WebHistory class.
///
///This class contains a snapshot of the current back/forward list for a WebView.
class WebHistory {
List<WebHistoryItem> _list;
List<WebHistoryItem> get list => _list;
int currentIndex;
WebHistory(this._list, this.currentIndex);
}
///WebHistoryItem class.
///
///A convenience class for accessing fields in an entry in the back/forward list of a WebView. Each WebHistoryItem is a snapshot of the requested history item.
class WebHistoryItem {
String originalUrl;
String title;
String url;
WebHistoryItem(this.originalUrl, this.title, this.url);
}
///InAppLocalhostServer class. ///InAppLocalhostServer class.
/// ///
///This class allows you to create a simple server on `http://localhost:[port]/` in order to be able to load your assets file on a server. The default [port] value is `8080`. ///This class allows you to create a simple server on `http://localhost:[port]/` in order to be able to load your assets file on a server. The default [port] value is `8080`.
......
name: flutter_inappbrowser name: flutter_inappbrowser
description: A Flutter plugin that allows you to add an inline webview or open an in-app browser window. (inspired by the popular cordova-plugin-inappbrowser). description: A Flutter plugin that allows you to add an inline webview or open an in-app browser window. (inspired by the popular cordova-plugin-inappbrowser).
version: 0.5.3 version: 0.5.4
author: Lorenzo Pichilli <pichillilorenzo@gmail.com> author: Lorenzo Pichilli <pichillilorenzo@gmail.com>
homepage: https://github.com/pichillilorenzo/flutter_inappbrowser homepage: https://github.com/pichillilorenzo/flutter_inappbrowser
environment: environment:
sdk: ">=2.0.0-dev.68.0 <3.0.0" sdk: ">=2.1.0-dev.7.1 <3.0.0"
flutter: ">=0.10.1 <2.0.0"
dependencies: dependencies:
flutter: flutter:
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment