Coder Social home page Coder Social logo

abozanona / interactive_webview Goto Github PK

View Code? Open in Web Editor NEW

This project forked from duyduong/interactive_webview

0.0 0.0 0.0 140 KB

This plugin is forked to present a fix for #14 in the original repo.

Home Page: https://github.com/duyduong/interactive_webview/issues/14

License: MIT License

Kotlin 22.79% Ruby 9.14% Swift 19.12% Objective-C 1.69% Dart 41.10% JavaScript 6.17%

interactive_webview's Introduction

pub package

interactive_webview

Plugin that allow Flutter to communicate with a native WebView.

Warning: This is not a display WebView. This plugin is designed to make communication between Flutter and WebView javascript. You can call a Javascript function; in the other hand, you can send messgage (postMessage) from Javascript to Flutter

  • Android: using JavascriptInterface
  • iOS: using WKUserContentController

Getting Started

For help getting started with Flutter, view our online documentation.

How it works

InteractiveWebView provide a singleton instance linked to one unique webview, so you can take control of the webview from anywhere in the app. You don't need to care where is the webview. It is hidden and automatically added to the keyWindow (iOS) and decorView (Android)

Load HTML string with/without a base url

InteractiveWebView allow you to load an HTML string with different base url

final _webView = new InteractiveWebView();

_webView.loadHTML("<html><head></head><script> src='https://code.jquery.com/jquery-3.3.1.min.js'></script><body></body></html>", baseUrl: "https://www.google.com/");

by using base url, you can do some hacking work on Javascript such as embedding iframe and AJAX request from Javascript. Normally you cannot do these things if your base url is different from url you are requesting

Load url

final _webView = new InteractiveWebView();

_webView.loadUrl("https://www.google.com/");

Listen for events

final _webView = new InteractiveWebView();

_webView.stateChanged.listen((state) {
  // state.type: enum (didStatrt and didFinish)
  // state.url
});

_webView.didReceiveMessage.listen((message) {
  // message.name
  // message.data (should be a Map/List/String)
});

Call Javascript function

final _webView = new InteractiveWebView();

_webView.evalJavascript("<your javascript function>");

Set restricted schemes

Sometimes you want the webview to restrict from loading specific urls/schemes. You can archieve that by using:

final _webView = new InteractiveWebView();

_webView.setOptions(restrictedSchemes: <String>["google.com"]); // google.com will not allow to load in webview

How to post message from Javascript

As mentioned above, you can post message from Javascript to Flutter.

Both iOS and Android defined a message handler called 'native':

  • iOS: you can access this handler by using webkit.messageHandlers.native
  • Android: you can access this handler by using window.native

So you only need to check which one is available in the webview, for example:

const nativeCommunicator = typeof webkit !== 'undefined' ? webkit.messageHandlers.native : window.native;

Then you can post message to Flutter

Warning: postMessage accepts String value only. So if you want to send an object/array, you need to using JSON.stringify to convert it to JSON string. InteractiveWebView will convert it back to Map/List respectively.

// send an array data
const array = [{name: "Hello"}, 1, true, "from", "WebView!!!"];
nativeCommunicator.postMessage(JSON.stringify(array));

// send and object data
const obj = {action: "my_action", text: "Hello from WebView!!!", number: 1, bool: false};
nativeCommunicator.postMessage(JSON.stringify(obj));

// send a text data
const text = "Hello from WebView!!!";
nativeCommunicator.postMessage(text);

To listen to post message from Flutter, you can use didReceiveMessage, see Listen for events

interactive_webview's People

Contributors

abozanona avatar duyduong avatar tmorone-rezi avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.