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
- 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)
[![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)
[![Donate to this project using Paypal](https://img.shields.io/badge/paypal-donate-yellow.svg)](https://www.paypal.me/LorenzoPichilli)
......@@ -9,6 +10,10 @@
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)!
### Requirements
- Dart sdk: ">=2.1.0-dev.7.1 <3.0.0"
- Flutter: ">=0.10.1 <2.0.0"
### 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:
```
......@@ -29,7 +34,7 @@ First, add `flutter_inappbrowser` as a [dependency in your pubspec.yaml file](ht
## Usage
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.
- [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`.
......@@ -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!
**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.
Example:
......
......@@ -4,7 +4,6 @@ import android.app.Activity;
import android.content.Context;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
......@@ -186,6 +185,9 @@ public class FlutterWebView implements PlatformView, MethodCallHandler {
case "getOptions":
result.success((webView != null) ? webView.getOptions() : null);
break;
case "getCopyBackForwardList":
result.success((webView != null) ? webView.getCopyBackForwardList() : null);
break;
case "dispose":
dispose();
result.success(true);
......@@ -201,6 +203,7 @@ public class FlutterWebView implements PlatformView, MethodCallHandler {
webView.setWebChromeClient(new WebChromeClient());
webView.setWebViewClient(new WebViewClient() {
public void onPageFinished(WebView view, String url) {
webView.destroy();
webView = null;
}
});
......
......@@ -381,4 +381,11 @@ public class InAppBrowserActivity extends AppCompatActivity {
else
result.success("");
}
public HashMap<String, Object> getCopyBackForwardList() {
if (webView != null)
return webView.getCopyBackForwardList();
return null;
}
}
......@@ -265,6 +265,9 @@ public class InAppBrowserFlutterPlugin implements MethodCallHandler {
case "getOptions":
result.success(getOptions(uuid));
break;
case "getCopyBackForwardList":
result.success(getCopyBackForwardList(uuid));
break;
default:
result.notImplemented();
}
......@@ -556,4 +559,11 @@ public class InAppBrowserFlutterPlugin implements MethodCallHandler {
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;
import android.util.Log;
import android.webkit.CookieManager;
import android.webkit.ValueCallback;
import android.webkit.WebBackForwardList;
import android.webkit.WebHistoryItem;
import android.webkit.WebSettings;
import android.webkit.WebView;
......@@ -22,7 +24,9 @@ import com.pichillilorenzo.flutter_inappbrowser.Util;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import io.flutter.plugin.common.MethodChannel;
......@@ -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 {
Future onLoadStop(String url) async {
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.canGoForward());
......
......@@ -925,4 +925,30 @@ class InAppBrowserWebViewController: UIViewController, WKUIDelegate, WKNavigatio
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 {
case "getOptions":
result(self.getOptions(uuid: uuid))
break
case "getCopyBackForwardList":
result(self.getCopyBackForwardList(uuid: uuid));
break;
default:
result(FlutterMethodNotImplemented)
break
......@@ -708,4 +711,11 @@ public class SwiftFlutterPlugin: NSObject, FlutterPlugin {
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 {
String _inAppBrowserUuid;
InAppBrowser _inAppBrowser;
InAppWebViewController(int id, InAppWebView widget) {
_id = id;
_channel = MethodChannel('com.pichillilorenzo/flutter_inappwebview_$id');
......@@ -1030,12 +1031,59 @@ class InAppWebViewController {
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 {
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.
///
///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
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>
homepage: https://github.com/pichillilorenzo/flutter_inappbrowser
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:
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