diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 98dd3d6d..6df4fb48 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -16,9 +16,10 @@ - + + + - @@ -38,8 +39,8 @@ - - + + @@ -47,48 +48,19 @@ - + - - + + - + + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - @@ -173,16 +145,15 @@ - @@ -191,8 +162,19 @@ - + + + + + + + + + + - - @@ -377,11 +348,11 @@ - - + + @@ -390,20 +361,19 @@ - + - - + + - @@ -659,36 +629,6 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - @@ -698,8 +638,36 @@ - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/example/lib/main.dart b/example/lib/main.dart index 553a2f87..201221d6 100644 --- a/example/lib/main.dart +++ b/example/lib/main.dart @@ -1,354 +1,265 @@ -import 'dart:async'; -import 'dart:convert'; -import 'package:flutter/material.dart'; -import 'package:flutter_inappbrowser/flutter_inappbrowser.dart'; - -class MyInAppBrowser extends InAppBrowser { - @override - Future onBrowserCreated() async { - print("\n\nBrowser Ready!\n\n"); - } - - @override - Future onLoadStart(String url) async { - print("\n\nStarted $url\n\n"); -// print("\n\n ${await this.isHidden()} \n\n"); -// print(await this.webViewController.canGoBack()); -// print(await this.webViewController.canGoForward()); - } - - @override - Future onLoadStop(String url) async { - print("\n\nStopped $url\n\n"); - -// print(base64.encode(await this.webViewController.getFavicon())); -// WebHistory history = await this.webViewController.getCopyBackForwardList(); -// print(history.list.length); -// print(history.currentIndex); -// print(history.list[history.currentIndex].url); -// for(WebHistoryItem item in history.list) { -// print(item.title); -// } - -// -// print(await this.webViewController.canGoBackOrForward(1)); -// if (await this.webViewController.canGoBackOrForward(-2)) { -// this.webViewController.goTo(history.list[0]); -// } - -// await this.webViewController.goBackOrForward(-1); - -// print(await this.webViewController.canGoBack()); -// print(await this.webViewController.canGoForward()); - -// var screenshot = await this.webViewController.takeScreenshot(); -// await this.webViewController.injectScriptCode(""" -// document.body.innerHTML = ''; -// """); - -// var options = await this.getOptions(); -// if (options["javaScriptEnabled"]) { -// await this.setOptions({ -// //"progressBar": true, -// //"useOnLoadResource": true, -// //"hidden": true, -// //"toolbarTopFixedTitle": "Fixed title A", -// //"useShouldOverrideUrlLoading": true -// //"hideUrlBar": true, -// "javaScriptEnabled": false, -// "toolbarTop": true, -// "toolbarBottom": false -// }); -// } -// else { -// await this.setOptions({ -// //"progressBar": false, -// //"useOnLoadResource": false, -// //"hidden": true, -// //"toolbarTopFixedTitle": "Fixed title B", -// //"useShouldOverrideUrlLoading": true -// //"hideUrlBar": false, -// "javaScriptEnabled": true, -// "toolbarTop": false, -// "toolbarBottom": true -// }); -// } - -// print("\n\n ${await this.isHidden()} \n\n"); -// -// await this.webViewController.injectScriptCode("window.flutter_inappbrowser.callHandler('handlerTest', 1, 5,'string', {'key': 5}, [4,6,8]);"); -// await this.webViewController.injectScriptCode("window.flutter_inappbrowser.callHandler('handlerTest2', false, null, undefined);"); -// await this.webViewController.injectScriptCode("setTimeout(function(){window.flutter_inappbrowser.callHandler('handlerTest', 'anotherString');}, 1000);"); - -// await this.webViewController.injectScriptCode("console.log({'testObject': 5});"); -// await this.webViewController.injectScriptCode("console.warn('testWarn',null);"); -// await this.webViewController.injectScriptCode("console.log('testObjectStringify', JSON.stringify({'asd': 5}));"); -// await this.webViewController.injectScriptCode("console.info('testInfo', 6);"); -// await this.webViewController.injectScriptCode("console.error('testError', false);"); -// await this.webViewController.injectScriptCode("console.debug('testDebug', true);"); -// -// print(await this.webViewController.injectScriptCode("document.cookie")); -// -// print(""); -// print(await CookieManager.getCookies(url)); -// print(""); -// print(await CookieManager.getCookie(url, "my_cookie2")); -// print(""); -// await CookieManager.deleteCookie(url, "my_cookie2"); -// await CookieManager.deleteCookie(url, "_gid", domain: ".googleblog.com"); -// print(""); -// print(await CookieManager.getCookies(url)); -// print(""); -// await CookieManager.deleteCookies(url); -// print(""); -// print(await CookieManager.getCookies(url)); -// print(""); -// await CookieManager.deleteAllCookies(); -// print(""); -// print(await CookieManager.getCookies(url)); -// print(""); -// -// print(await this.webViewController.injectScriptCode("null")); -// print(await this.webViewController.injectScriptCode("undefined")); -// print(await this.webViewController.injectScriptCode("3")); -// print(await this.webViewController.injectScriptCode(""" -// function asd (a,b) { -// return a+b; -// }; -// asd(3,5); -// """)); -// print(await this.webViewController.injectScriptCode(""" -// ["3",56,"sdf"]; -// """)); -// print(await this.webViewController.injectScriptCode(""" -// var x = {"as":4, "dfdfg": 6}; -// x; -// """)); -// -// await this.webViewController.injectScriptFile("https://code.jquery.com/jquery-3.3.1.min.js"); -// this.webViewController.injectScriptCode(""" -// \$( "body" ).html( "Next Step..." ) -// """); -// -// // add custom css -// this.webViewController.injectStyleCode(""" -// body { -// background-color: #3c3c3c !important; -// } -// """); -// this.webViewController.injectStyleFile("https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css"); - } - - @override - Future onScrollChanged(int x, int y) async { -// print(x.toString() + " " + y.toString()); - } - - @override - void onLoadError(String url, int code, String message) { - print("\n\nCan't load $url.. Error: $message\n\n"); - } - - @override - void onProgressChanged(int progress) { -// print("Progress: $progress"); - } - - @override - void onExit() { - print("\n\nBrowser closed!\n\n"); - } - - @override - void shouldOverrideUrlLoading(String url) { - print("\n\n override $url\n\n"); - this.webViewController.loadUrl(url); - -// var postData = "username=my_username&password=my_password"; -// inAppBrowserFallback.webViewController.postUrl("http://localhost:8080", utf8.encode(postData)); - -// var htmlData = """ -// -// -// -// -// -// -// Document -// -// -// -// -// -//
-//
-// dart logo -//
-//
-// One of three columns -//
-//
-// One of three columns -//
-//
-// One of three columns -//
-//
-//
-// -//
-// -// -// """; -// inAppBrowserFallback.webViewController.loadData(htmlData); - } - - @override - void onLoadResource( - WebResourceResponse response, WebResourceRequest request) { - print("Started at: " + - response.startTime.toString() + - "ms ---> duration: " + - response.duration.toString() + - "ms " + - response.url); -// if (response.headers["content-length"] != null) -// print(response.headers["content-length"] + " length"); - } - - @override - void onConsoleMessage(ConsoleMessage consoleMessage) { - print(consoleMessage.message); -// print(""" -// console output: -// sourceURL: ${consoleMessage.sourceURL} -// lineNumber: ${consoleMessage.lineNumber} -// message: ${consoleMessage.message} -// messageLevel: ${consoleMessage.messageLevel} -// """); - } -} - -MyInAppBrowser inAppBrowserFallback = new MyInAppBrowser(); - -class MyChromeSafariBrowser extends ChromeSafariBrowser { - MyChromeSafariBrowser(browserFallback) : super(browserFallback); - - @override - void onOpened() { - print("ChromeSafari browser opened"); - } - - @override - void onLoaded() { - print("ChromeSafari browser loaded"); - } - - @override - void onClosed() { - print("ChromeSafari browser closed"); - } -} - -// adding a webview fallback -MyChromeSafariBrowser chromeSafariBrowser = - new MyChromeSafariBrowser(inAppBrowserFallback); -InAppLocalhostServer localhostServer = new InAppLocalhostServer(); - -Future main() async { -// await localhostServer.start(); - runApp(new MyApp()); -} - -class MyApp extends StatefulWidget { - @override - _MyAppState createState() => new _MyAppState(); -} - -class _MyAppState extends State { - @override - void initState() { - super.initState(); -// int indexTest = inAppBrowserFallback.webViewController.addJavaScriptHandler("handlerTest", -// (arguments) async { -// print("handlerTest arguments"); -// print(arguments); -// }); -// int indexTest2 = inAppBrowserFallback.webViewController.addJavaScriptHandler("test2", (arguments) async { -// print("handlerTest2 arguments"); -// print(arguments); -// inAppBrowserFallback.webViewController.removeJavaScriptHandler("test", indexTest); -// }); - } - - @override - Widget build(BuildContext context) { - return new MaterialApp( - home: new Scaffold( - appBar: new AppBar( - title: const Text('Flutter InAppBrowser Plugin example app'), - ), - body: new Center( - child: new RaisedButton( - onPressed: () async { -// await chromeSafariBrowser.open("https://flutter.io/"); -// -// await InAppBrowser.openWithSystemBrowser("https://flutter.io/"); -// -// await inAppBrowserFallback.open(url: "http://localhost:8080/assets/index.html", options: { -// "useOnLoadResource": true, -// //"hidden": true, -// //"toolbarTopFixedTitle": "Fixed title", -// //"useShouldOverrideUrlLoading": true -// //"hideUrlBar": true, -// //"toolbarTop": false, -// "toolbarBottom": false -// }); -// -// await inAppBrowserFallback.openFile("assets/index.html", options: { -// "useOnLoadResource": true, -// //"hidden": true, -// //"useShouldOverrideUrlLoading": true -// //"hideUrlBar": true, -// //"toolbarTop": false, -// //"toolbarBottom": false -// }); -// -// await CookieManager.setCookie("https://flutter.io/", "my_cookie2", "cookieValue2", domain: "flutter.io", expiresDate: 1540838864611); -// await CookieManager.setCookie("https://flutter.io/", "my_cookie", "cookieValue", domain: "flutter.io", expiresDate: 1540838864611); - -// await inAppBrowserFallback.openData("Data example

This is a \"p\" tag

", options: {}); - - await inAppBrowserFallback.open(url: "https://flutter.io/", options: { - //"useOnLoadResource": true, - //"hidden": true, - //"toolbarTopFixedTitle": "Fixed title", - "useShouldOverrideUrlLoading": true, - //"hideUrlBar": true, - //"toolbarTop": false, - //"toolbarBottom": false - }); - - }, - child: Text("Open InAppBrowser") - ), - ), - ), - ); - } -} - - -// Inline WebView Example -// //import 'dart:async'; +//import 'dart:convert'; //import 'package:flutter/material.dart'; //import 'package:flutter_inappbrowser/flutter_inappbrowser.dart'; // +//class MyInAppBrowser extends InAppBrowser { +// @override +// Future onBrowserCreated() async { +// print("\n\nBrowser Ready!\n\n"); +// } +// +// @override +// Future onLoadStart(String url) async { +// print("\n\nStarted $url\n\n"); +//// print("\n\n ${await this.isHidden()} \n\n"); +//// print(await this.webViewController.canGoBack()); +//// print(await this.webViewController.canGoForward()); +// } +// +// @override +// Future onLoadStop(String url) async { +// print("\n\nStopped $url\n\n"); +// +//// print(base64.encode(await this.webViewController.getFavicon())); +//// WebHistory history = await this.webViewController.getCopyBackForwardList(); +//// print(history.list.length); +//// print(history.currentIndex); +//// print(history.list[history.currentIndex].url); +//// for(WebHistoryItem item in history.list) { +//// print(item.title); +//// } +// +//// +//// print(await this.webViewController.canGoBackOrForward(1)); +//// if (await this.webViewController.canGoBackOrForward(-2)) { +//// this.webViewController.goTo(history.list[0]); +//// } +// +//// await this.webViewController.goBackOrForward(-1); +// +//// print(await this.webViewController.canGoBack()); +//// print(await this.webViewController.canGoForward()); +// +//// var screenshot = await this.webViewController.takeScreenshot(); +//// await this.webViewController.injectScriptCode(""" +//// document.body.innerHTML = ''; +//// """); +// +//// var options = await this.getOptions(); +//// if (options["javaScriptEnabled"]) { +//// await this.setOptions({ +//// //"progressBar": true, +//// //"useOnLoadResource": true, +//// //"hidden": true, +//// //"toolbarTopFixedTitle": "Fixed title A", +//// //"useShouldOverrideUrlLoading": true +//// //"hideUrlBar": true, +//// "javaScriptEnabled": false, +//// "toolbarTop": true, +//// "toolbarBottom": false +//// }); +//// } +//// else { +//// await this.setOptions({ +//// //"progressBar": false, +//// //"useOnLoadResource": false, +//// //"hidden": true, +//// //"toolbarTopFixedTitle": "Fixed title B", +//// //"useShouldOverrideUrlLoading": true +//// //"hideUrlBar": false, +//// "javaScriptEnabled": true, +//// "toolbarTop": false, +//// "toolbarBottom": true +//// }); +//// } +// +//// print("\n\n ${await this.isHidden()} \n\n"); +//// +//// await this.webViewController.injectScriptCode("window.flutter_inappbrowser.callHandler('handlerTest', 1, 5,'string', {'key': 5}, [4,6,8]);"); +//// await this.webViewController.injectScriptCode("window.flutter_inappbrowser.callHandler('handlerTest2', false, null, undefined);"); +//// await this.webViewController.injectScriptCode("setTimeout(function(){window.flutter_inappbrowser.callHandler('handlerTest', 'anotherString');}, 1000);"); +// +//// await this.webViewController.injectScriptCode("console.log({'testObject': 5});"); +//// await this.webViewController.injectScriptCode("console.warn('testWarn',null);"); +//// await this.webViewController.injectScriptCode("console.log('testObjectStringify', JSON.stringify({'asd': 5}));"); +//// await this.webViewController.injectScriptCode("console.info('testInfo', 6);"); +//// await this.webViewController.injectScriptCode("console.error('testError', false);"); +//// await this.webViewController.injectScriptCode("console.debug('testDebug', true);"); +//// +//// print(await this.webViewController.injectScriptCode("document.cookie")); +//// +//// print(""); +//// print(await CookieManager.getCookies(url)); +//// print(""); +//// print(await CookieManager.getCookie(url, "my_cookie2")); +//// print(""); +//// await CookieManager.deleteCookie(url, "my_cookie2"); +//// await CookieManager.deleteCookie(url, "_gid", domain: ".googleblog.com"); +//// print(""); +//// print(await CookieManager.getCookies(url)); +//// print(""); +//// await CookieManager.deleteCookies(url); +//// print(""); +//// print(await CookieManager.getCookies(url)); +//// print(""); +//// await CookieManager.deleteAllCookies(); +//// print(""); +//// print(await CookieManager.getCookies(url)); +//// print(""); +//// +//// print(await this.webViewController.injectScriptCode("null")); +//// print(await this.webViewController.injectScriptCode("undefined")); +//// print(await this.webViewController.injectScriptCode("3")); +//// print(await this.webViewController.injectScriptCode(""" +//// function asd (a,b) { +//// return a+b; +//// }; +//// asd(3,5); +//// """)); +//// print(await this.webViewController.injectScriptCode(""" +//// ["3",56,"sdf"]; +//// """)); +//// print(await this.webViewController.injectScriptCode(""" +//// var x = {"as":4, "dfdfg": 6}; +//// x; +//// """)); +//// +//// await this.webViewController.injectScriptFile("https://code.jquery.com/jquery-3.3.1.min.js"); +//// this.webViewController.injectScriptCode(""" +//// \$( "body" ).html( "Next Step..." ) +//// """); +//// +//// // add custom css +//// this.webViewController.injectStyleCode(""" +//// body { +//// background-color: #3c3c3c !important; +//// } +//// """); +//// this.webViewController.injectStyleFile("https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css"); +// } +// +// @override +// Future onScrollChanged(int x, int y) async { +//// print(x.toString() + " " + y.toString()); +// } +// +// @override +// void onLoadError(String url, int code, String message) { +// print("\n\nCan't load $url.. Error: $message\n\n"); +// } +// +// @override +// void onProgressChanged(int progress) { +//// print("Progress: $progress"); +// } +// +// @override +// void onExit() { +// print("\n\nBrowser closed!\n\n"); +// } +// +// @override +// void shouldOverrideUrlLoading(String url) { +// print("\n\n override $url\n\n"); +// this.webViewController.loadUrl(url); +// +//// var postData = "username=my_username&password=my_password"; +//// inAppBrowserFallback.webViewController.postUrl("http://localhost:8080", utf8.encode(postData)); +// +//// var htmlData = """ +//// +//// +//// +//// +//// +//// +//// Document +//// +//// +//// +//// +//// +////
+////
+//// dart logo +////
+////
+//// One of three columns +////
+////
+//// One of three columns +////
+////
+//// One of three columns +////
+////
+////
+//// +////
+//// +//// +//// """; +//// inAppBrowserFallback.webViewController.loadData(htmlData); +// } +// +// @override +// void onLoadResource( +// WebResourceResponse response, WebResourceRequest request) { +// print("Started at: " + +// response.startTime.toString() + +// "ms ---> duration: " + +// response.duration.toString() + +// "ms " + +// response.url); +//// if (response.headers["content-length"] != null) +//// print(response.headers["content-length"] + " length"); +// } +// +// @override +// void onConsoleMessage(ConsoleMessage consoleMessage) { +// print(consoleMessage.message); +//// print(""" +//// console output: +//// sourceURL: ${consoleMessage.sourceURL} +//// lineNumber: ${consoleMessage.lineNumber} +//// message: ${consoleMessage.message} +//// messageLevel: ${consoleMessage.messageLevel} +//// """); +// } +//} +// +//MyInAppBrowser inAppBrowserFallback = new MyInAppBrowser(); +// +//class MyChromeSafariBrowser extends ChromeSafariBrowser { +// MyChromeSafariBrowser(browserFallback) : super(browserFallback); +// +// @override +// void onOpened() { +// print("ChromeSafari browser opened"); +// } +// +// @override +// void onLoaded() { +// print("ChromeSafari browser loaded"); +// } +// +// @override +// void onClosed() { +// print("ChromeSafari browser closed"); +// } +//} +// +//// adding a webview fallback +//MyChromeSafariBrowser chromeSafariBrowser = +// new MyChromeSafariBrowser(inAppBrowserFallback); +//InAppLocalhostServer localhostServer = new InAppLocalhostServer(); +// //Future main() async { +//// await localhostServer.start(); // runApp(new MyApp()); //} // @@ -358,118 +269,207 @@ class _MyAppState extends State { //} // //class _MyAppState extends State { -// -// InAppWebViewController webView; -// String url = ""; -// double progress = 0; -// // @override // void initState() { // super.initState(); -// } -// -// @override -// void dispose() { -// super.dispose(); +//// int indexTest = inAppBrowserFallback.webViewController.addJavaScriptHandler("handlerTest", +//// (arguments) async { +//// print("handlerTest arguments"); +//// print(arguments); +//// }); +//// int indexTest2 = inAppBrowserFallback.webViewController.addJavaScriptHandler("test2", (arguments) async { +//// print("handlerTest2 arguments"); +//// print(arguments); +//// inAppBrowserFallback.webViewController.removeJavaScriptHandler("test", indexTest); +//// }); // } // // @override // Widget build(BuildContext context) { -// return MaterialApp( -// home: Scaffold( -// appBar: AppBar( -// title: const Text('Inline WebView example app'), +// return new MaterialApp( +// home: new Scaffold( +// appBar: new AppBar( +// title: const Text('Flutter InAppBrowser Plugin example app'), // ), -// body: Container( -// child: Column( -// children: [ -// Container( -// padding: EdgeInsets.all(20.0), -// child: Text("CURRENT URL\n${ (url.length > 50) ? url.substring(0, 50) + "..." : url }"), -// ), -// (progress != 1.0) ? LinearProgressIndicator(value: progress) : null, -// Expanded( -// child: Container( -// margin: const EdgeInsets.all(10.0), -// decoration: BoxDecoration( -// border: Border.all(color: Colors.blueAccent) -// ), -// child: InAppWebView( -// initialUrl: "https://flutter.io/", -// //initialData: InAppWebViewInitialData("Data example

This is a \"p\" tag

"), -// initialHeaders: { +// body: new Center( +// child: new RaisedButton( +// onPressed: () async { +//// await chromeSafariBrowser.open("https://flutter.io/"); +//// +//// await InAppBrowser.openWithSystemBrowser("https://flutter.io/"); +//// +//// await inAppBrowserFallback.open(url: "http://localhost:8080/assets/index.html", options: { +//// "useOnLoadResource": true, +//// //"hidden": true, +//// //"toolbarTopFixedTitle": "Fixed title", +//// //"useShouldOverrideUrlLoading": true +//// //"hideUrlBar": true, +//// //"toolbarTop": false, +//// "toolbarBottom": false +//// }); +//// +//// await inAppBrowserFallback.openFile("assets/index.html", options: { +//// "useOnLoadResource": true, +//// //"hidden": true, +//// //"useShouldOverrideUrlLoading": true +//// //"hideUrlBar": true, +//// //"toolbarTop": false, +//// //"toolbarBottom": false +//// }); +//// +//// await CookieManager.setCookie("https://flutter.io/", "my_cookie2", "cookieValue2", domain: "flutter.io", expiresDate: 1540838864611); +//// await CookieManager.setCookie("https://flutter.io/", "my_cookie", "cookieValue", domain: "flutter.io", expiresDate: 1540838864611); // -// }, -// initialOptions: { +//// await inAppBrowserFallback.openData("Data example

This is a \"p\" tag

", options: {}); // -// }, -// onWebViewCreated: (InAppWebViewController controller) { -// webView = controller; -// }, -// onLoadStart: (InAppWebViewController controller, String url) { -// print("started $url"); -// setState(() { -// this.url = url; -// }); -// }, -// onProgressChanged: (InAppWebViewController controller, int progress) { -// setState(() { -// this.progress = progress/100; -// }); -// }, -// ), -// ), -// ), -// ButtonBar( -// alignment: MainAxisAlignment.center, -// children: [ -// RaisedButton( -// child: Icon(Icons.arrow_back), -// onPressed: () { -// if (webView != null) { -// webView.goBack(); -// } -// }, -// ), -// RaisedButton( -// child: Icon(Icons.arrow_forward), -// onPressed: () { -// if (webView != null) { -// webView.goForward(); -// } -// }, -// ), -// RaisedButton( -// child: Icon(Icons.refresh), -// onPressed: () { -// if (webView != null) { -// webView.reload(); -// } -// }, -// ), -// ], -// ), -// ].where((Object o) => o != null).toList(), +// await inAppBrowserFallback.open(url: "https://flutter.io/", options: { +// //"useOnLoadResource": true, +// //"hidden": true, +// //"toolbarTopFixedTitle": "Fixed title", +// "useShouldOverrideUrlLoading": true, +// //"hideUrlBar": true, +// //"toolbarTop": false, +// //"toolbarBottom": false +// }); +// +// }, +// child: Text("Open InAppBrowser") // ), // ), -// bottomNavigationBar: BottomNavigationBar( -// currentIndex: 0, -// items: [ -// BottomNavigationBarItem( -// icon: Icon(Icons.home), -// title: Text('Home'), -// ), -// BottomNavigationBarItem( -// icon: Icon(Icons.mail), -// title: Text('Item 2'), -// ), -// BottomNavigationBarItem( -// icon: Icon(Icons.person), -// title: Text('Item 3') -// ) -// ], -// ), // ), // ); // } //} + + +// Inline WebView Example + +import 'dart:async'; +import 'package:flutter/material.dart'; +import 'package:flutter_inappbrowser/flutter_inappbrowser.dart'; + +Future main() async { + runApp(new MyApp()); +} + +class MyApp extends StatefulWidget { + @override + _MyAppState createState() => new _MyAppState(); +} + +class _MyAppState extends State { + + InAppWebViewController webView; + String url = ""; + double progress = 0; + + @override + void initState() { + super.initState(); + } + + @override + void dispose() { + super.dispose(); + } + + @override + Widget build(BuildContext context) { + return MaterialApp( + home: Scaffold( + appBar: AppBar( + title: const Text('Inline WebView example app'), + ), + body: Container( + child: Column( + children: [ + Container( + padding: EdgeInsets.all(20.0), + child: Text("CURRENT URL\n${ (url.length > 50) ? url.substring(0, 50) + "..." : url }"), + ), + (progress != 1.0) ? LinearProgressIndicator(value: progress) : null, + Expanded( + child: Container( + margin: const EdgeInsets.all(10.0), + decoration: BoxDecoration( + border: Border.all(color: Colors.blueAccent) + ), + child: InAppWebView( + initialUrl: "https://flutter.io/", + //initialData: InAppWebViewInitialData("Data example

This is a \"p\" tag

"), + initialHeaders: { + + }, + initialOptions: { + + }, + onWebViewCreated: (InAppWebViewController controller) { + webView = controller; + }, + onLoadStart: (InAppWebViewController controller, String url) { + print("started $url"); + setState(() { + this.url = url; + }); + }, + onProgressChanged: (InAppWebViewController controller, int progress) { + setState(() { + this.progress = progress/100; + }); + }, + ), + ), + ), + ButtonBar( + alignment: MainAxisAlignment.center, + children: [ + RaisedButton( + child: Icon(Icons.arrow_back), + onPressed: () { + if (webView != null) { + webView.goBack(); + } + }, + ), + RaisedButton( + child: Icon(Icons.arrow_forward), + onPressed: () { + if (webView != null) { + webView.goForward(); + } + }, + ), + RaisedButton( + child: Icon(Icons.refresh), + onPressed: () { + if (webView != null) { + webView.reload(); + } + }, + ), + ], + ), + ].where((Object o) => o != null).toList(), + ), + ), + bottomNavigationBar: BottomNavigationBar( + currentIndex: 0, + items: [ + BottomNavigationBarItem( + icon: Icon(Icons.home), + title: Text('Home'), + ), + BottomNavigationBarItem( + icon: Icon(Icons.mail), + title: Text('Item 2'), + ), + BottomNavigationBarItem( + icon: Icon(Icons.person), + title: Text('Item 3') + ) + ], + ), + ), + ); + } +} diff --git a/flutter_inappbrowser.iml b/flutter_inappbrowser.iml index 4b6ef47a..175d8e0e 100644 --- a/flutter_inappbrowser.iml +++ b/flutter_inappbrowser.iml @@ -20,7 +20,6 @@ - diff --git a/ios/Classes/FlutterWebViewController.swift b/ios/Classes/FlutterWebViewController.swift new file mode 100644 index 00000000..e1bc1233 --- /dev/null +++ b/ios/Classes/FlutterWebViewController.swift @@ -0,0 +1,51 @@ +// +// FlutterWebViewController.swift +// flutter_inappbrowser +// +// Created by Lorenzo on 13/11/18. +// + +import Foundation + +public class FlutterWebViewController: NSObject, FlutterPlatformView { + + private weak var registrar: FlutterPluginRegistrar? + private var webView: InAppWebView? + private var viewId: Int64 = 0 + private var channel: FlutterMethodChannel? + + init(registrar: FlutterPluginRegistrar, withFrame frame: CGRect, viewIdentifier viewId: Int64, arguments args: NSDictionary) { + super.init() + self.registrar = registrar + self.viewId = viewId + webView = InAppWebView(frame: frame) + let channelName = String(format: "com.pichillilorenzo/flutter_inappwebview_%lld", viewId) + self.channel = FlutterMethodChannel(name: channelName, binaryMessenger: registrar.messenger()) + self.channel?.setMethodCallHandler(self.handle) + + var initialUrl = (args["initialUrl"] as? String)! + let initialFile = args["initialFile"] as? String + let initialData = args["initialData"] as? [String: String] + let initialHeaders = (args["initialHeaders"] as? [String: String])! + let initialOptions = (args["initialOptions"] as? [String: Any])! + + webView!.load(URLRequest(url: URL(string: initialUrl)!)) + } + + public func view() -> UIView { + return webView! + } + + public func handle(_ call: FlutterMethodCall, result: @escaping FlutterResult) { + let arguments = call.arguments as? NSDictionary + + switch call.method { + //case "open": + //self.open(uuid: uuid, arguments: arguments!, result: result) + //break + default: + result(FlutterMethodNotImplemented) + break + } + } +} diff --git a/ios/Classes/FlutterWebViewFactory.swift b/ios/Classes/FlutterWebViewFactory.swift new file mode 100644 index 00000000..e778883c --- /dev/null +++ b/ios/Classes/FlutterWebViewFactory.swift @@ -0,0 +1,28 @@ +// +// FlutterWebViewFactory.swift +// flutter_inappbrowser +// +// Created by Lorenzo on 13/11/18. +// + +import Flutter +import Foundation + +public class FlutterWebViewFactory: NSObject, FlutterPlatformViewFactory { + private weak var registrar: FlutterPluginRegistrar? + + init(registrar: FlutterPluginRegistrar?) { + super.init() + self.registrar = registrar + } + + public func createArgsCodec() -> FlutterMessageCodec & NSObjectProtocol { + return FlutterStandardMessageCodec.sharedInstance() + } + + public func create(withFrame frame: CGRect, viewIdentifier viewId: Int64, arguments args: Any?) -> FlutterPlatformView { + let arguments = args as? NSDictionary + let webviewController = FlutterWebViewController(registrar: registrar!, withFrame: frame, viewIdentifier: viewId, arguments: arguments!) + return webviewController + } +} diff --git a/ios/Classes/SwiftFlutterPlugin.swift b/ios/Classes/SwiftFlutterPlugin.swift index 1f27e8f1..096699b9 100644 --- a/ios/Classes/SwiftFlutterPlugin.swift +++ b/ios/Classes/SwiftFlutterPlugin.swift @@ -36,12 +36,12 @@ extension Dictionary where Key: ExpressibleByStringLiteral { public class SwiftFlutterPlugin: NSObject, FlutterPlugin { static var registrar: FlutterPluginRegistrar? + var channel: FlutterMethodChannel var webViewControllers: [String: InAppBrowserWebViewController?] = [:] var safariViewControllers: [String: Any?] = [:] var tmpWindow: UIWindow? - var channel: FlutterMethodChannel private var previousStatusBarStyle = -1 public init(with registrar: FlutterPluginRegistrar) { @@ -56,6 +56,8 @@ public class SwiftFlutterPlugin: NSObject, FlutterPlugin { let instance = SwiftFlutterPlugin(with: registrar) registrar.addMethodCallDelegate(instance, channel: channel) + registrar.register(FlutterWebViewFactory(registrar: registrar) as FlutterPlatformViewFactory, withId: "com.pichillilorenzo/flutter_inappwebview") + if #available(iOS 11.0, *) { MyCookieManager(registrar: registrar) } else { diff --git a/lib/flutter_inappbrowser.dart b/lib/flutter_inappbrowser.dart index 12481bc9..12d11b6b 100644 --- a/lib/flutter_inappbrowser.dart +++ b/lib/flutter_inappbrowser.dart @@ -694,6 +694,20 @@ class _InAppWebViewState extends State { creationParamsCodec: const StandardMessageCodec(), ), ); + } else if (defaultTargetPlatform == TargetPlatform.iOS) { + return UiKitView( + viewType: 'com.pichillilorenzo/flutter_inappwebview', + onPlatformViewCreated: _onPlatformViewCreated, + gestureRecognizers: widget.gestureRecognizers, + creationParams: { + 'initialUrl': widget.initialUrl, + 'initialFile': widget.initialFile, + 'initialData': widget.initialData?.toMap(), + 'initialHeaders': widget.initialHeaders, + 'initialOptions': widget.initialOptions + }, + creationParamsCodec: const StandardMessageCodec(), + ); } return Text( '$defaultTargetPlatform is not yet supported by the flutter_inappbrowser plugin');