Commit f3f0876f authored by pichillilorenzo's avatar pichillilorenzo

fix #56, some code cleanup

parent 8db2a6b2
...@@ -2,5 +2,6 @@ ...@@ -2,5 +2,6 @@
<project version="4"> <project version="4">
<component name="VcsDirectoryMappings"> <component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" /> <mapping directory="$PROJECT_DIR$" vcs="Git" />
<mapping directory="$PROJECT_DIR$/example/ios/.symlinks/plugins/flutter_inappbrowser" vcs="Git" />
</component> </component>
</project> </project>
\ No newline at end of file
This diff is collapsed.
## 1.0.1
- Fixed Unable to load initialFile on iOS #56
- Some code cleanup
## 1.0.0 ## 1.0.0
Breaking changes: Breaking changes:
- Fixed [Flutter AndroidX compatibility](https://flutter.dev/docs/development/packages-and-plugins/androidx-compatibility), the latest version that doesn't use `AndroidX` is `0.6.0`. - Fixed [Flutter AndroidX compatibility](https://flutter.dev/docs/development/packages-and-plugins/androidx-compatibility), the latest version that doesn't use `AndroidX` is `0.6.0` (thanks to [juicycleff](https://github.com/juicycleff)).
## 0.6.0 ## 0.6.0
......
...@@ -13,6 +13,8 @@ This plugin is inspired by the popular [cordova-plugin-inappbrowser](https://git ...@@ -13,6 +13,8 @@ This plugin is inspired by the popular [cordova-plugin-inappbrowser](https://git
### Requirements ### Requirements
- Dart sdk: ">=2.1.0-dev.7.1 <3.0.0" - Dart sdk: ">=2.1.0-dev.7.1 <3.0.0"
- Flutter: ">=0.10.1 <2.0.0" - Flutter: ">=0.10.1 <2.0.0"
- Android: `minSdkVersion 17`
- iOS: `--ios-language swift`
### Note for Android ### Note for Android
During the build, if Android fails with `Error: uses-sdk:minSdkVersion 16 cannot be smaller than version 17 declared in library`, it means that you need to update the `minSdkVersion` of your `build.gradle` file to at least `17`. During the build, if Android fails with `Error: uses-sdk:minSdkVersion 16 cannot be smaller than version 17 declared in library`, it means that you need to update the `minSdkVersion` of your `build.gradle` file to at least `17`.
......
...@@ -128,8 +128,7 @@ public class FlutterWebView implements PlatformView, MethodCallHandler { ...@@ -128,8 +128,7 @@ public class FlutterWebView implements PlatformView, MethodCallHandler {
case "injectScriptCode": case "injectScriptCode":
if (webView != null) { if (webView != null) {
source = call.argument("source").toString(); source = call.argument("source").toString();
jsWrapper = "(function(){return JSON.stringify(eval(%s));})();"; webView.injectScriptCode(source, result);
webView.injectDeferredObject(source, jsWrapper, result);
} }
else { else {
result.success(""); result.success("");
...@@ -138,24 +137,21 @@ public class FlutterWebView implements PlatformView, MethodCallHandler { ...@@ -138,24 +137,21 @@ public class FlutterWebView implements PlatformView, MethodCallHandler {
case "injectScriptFile": case "injectScriptFile":
if (webView != null) { if (webView != null) {
urlFile = call.argument("urlFile").toString(); urlFile = call.argument("urlFile").toString();
jsWrapper = "(function(d) { var c = d.createElement('script'); c.src = %s; d.body.appendChild(c); })(document);"; webView.injectScriptFile(urlFile);
webView.injectDeferredObject(urlFile, jsWrapper, null);
} }
result.success(true); result.success(true);
break; break;
case "injectStyleCode": case "injectStyleCode":
if (webView != null) { if (webView != null) {
source = call.argument("source").toString(); source = call.argument("source").toString();
jsWrapper = "(function(d) { var c = d.createElement('style'); c.innerHTML = %s; d.body.appendChild(c); })(document);"; webView.injectStyleCode(source);
webView.injectDeferredObject(source, jsWrapper, null);
} }
result.success(true); result.success(true);
break; break;
case "injectStyleFile": case "injectStyleFile":
if (webView != null) { if (webView != null) {
urlFile = call.argument("urlFile").toString(); urlFile = call.argument("urlFile").toString();
jsWrapper = "(function(d) { var c = d.createElement('link'); c.rel='stylesheet'; c.type='text/css'; c.href = %s; d.head.appendChild(c); })(document);"; webView.injectStyleFile(urlFile);
webView.injectDeferredObject(urlFile, jsWrapper, null);
} }
result.success(true); result.success(true);
break; break;
......
...@@ -198,7 +198,7 @@ public class InAppBrowserActivity extends AppCompatActivity { ...@@ -198,7 +198,7 @@ public class InAppBrowserActivity extends AppCompatActivity {
if (webView != null) { if (webView != null) {
webView.loadUrl(url, result); webView.loadUrl(url, result);
} else { } else {
result.error(LOG_TAG, "Cannot load url " + url, null); result.error(LOG_TAG, "webView is null", null);
} }
} }
...@@ -206,7 +206,7 @@ public class InAppBrowserActivity extends AppCompatActivity { ...@@ -206,7 +206,7 @@ public class InAppBrowserActivity extends AppCompatActivity {
if (webView != null) { if (webView != null) {
webView.loadUrl(url, headers, result); webView.loadUrl(url, headers, result);
} else { } else {
result.error(LOG_TAG, "Cannot load url " + url, null); result.error(LOG_TAG, "webView is null", null);
} }
} }
...@@ -214,7 +214,7 @@ public class InAppBrowserActivity extends AppCompatActivity { ...@@ -214,7 +214,7 @@ public class InAppBrowserActivity extends AppCompatActivity {
if (webView != null) { if (webView != null) {
webView.postUrl(url, postData, result); webView.postUrl(url, postData, result);
} else { } else {
result.error(LOG_TAG, "Cannot load url " + url, null); result.error(LOG_TAG, "webView is null", null);
} }
} }
...@@ -222,7 +222,7 @@ public class InAppBrowserActivity extends AppCompatActivity { ...@@ -222,7 +222,7 @@ public class InAppBrowserActivity extends AppCompatActivity {
if (webView != null) { if (webView != null) {
webView.loadData(data, mimeType, encoding, baseUrl, result); webView.loadData(data, mimeType, encoding, baseUrl, result);
} else { } else {
result.error(LOG_TAG, "Cannot load data", null); result.error(LOG_TAG, "webView is null", null);
} }
} }
...@@ -230,7 +230,7 @@ public class InAppBrowserActivity extends AppCompatActivity { ...@@ -230,7 +230,7 @@ public class InAppBrowserActivity extends AppCompatActivity {
if (webView != null) { if (webView != null) {
webView.loadFile(url, result); webView.loadFile(url, result);
} else { } else {
result.error(LOG_TAG, "Cannot load file " + url, null); result.error(LOG_TAG, "webView is null", null);
} }
} }
...@@ -238,7 +238,7 @@ public class InAppBrowserActivity extends AppCompatActivity { ...@@ -238,7 +238,7 @@ public class InAppBrowserActivity extends AppCompatActivity {
if (webView != null) { if (webView != null) {
webView.loadFile(url, headers, result); webView.loadFile(url, headers, result);
} else { } else {
result.error(LOG_TAG, "Cannot load file " + url, null); result.error(LOG_TAG, "webView is null", null);
} }
} }
...@@ -420,13 +420,28 @@ public class InAppBrowserActivity extends AppCompatActivity { ...@@ -420,13 +420,28 @@ public class InAppBrowserActivity extends AppCompatActivity {
return optionsMap; return optionsMap;
} }
public void injectDeferredObject(String source, String jsWrapper, MethodChannel.Result result) { public void injectScriptCode(String source, MethodChannel.Result result) {
if (webView != null) if (webView != null)
webView.injectDeferredObject(source, jsWrapper, result); webView.injectScriptCode(source, result);
else else
result.success(""); result.success("");
} }
public void injectScriptFile(String urlFile) {
if (webView != null)
webView.injectScriptFile(urlFile);
}
public void injectStyleCode(String source) {
if (webView != null)
webView.injectStyleCode(source);
}
public void injectStyleFile(String urlFile) {
if (webView != null)
webView.injectStyleFile(urlFile);
}
public HashMap<String, Object> getCopyBackForwardList() { public HashMap<String, Object> getCopyBackForwardList() {
if (webView != null) if (webView != null)
return webView.getCopyBackForwardList(); return webView.getCopyBackForwardList();
......
...@@ -212,25 +212,21 @@ public class InAppBrowserFlutterPlugin implements MethodCallHandler { ...@@ -212,25 +212,21 @@ public class InAppBrowserFlutterPlugin implements MethodCallHandler {
break; break;
case "injectScriptCode": case "injectScriptCode":
source = call.argument("source").toString(); source = call.argument("source").toString();
jsWrapper = "(function(){return JSON.stringify(eval(%s));})();"; injectScriptCode(uuid, source, result);
injectDeferredObject(uuid, source, jsWrapper, result);
break; break;
case "injectScriptFile": case "injectScriptFile":
urlFile = call.argument("urlFile").toString(); urlFile = call.argument("urlFile").toString();
jsWrapper = "(function(d) { var c = d.createElement('script'); c.src = %s; d.body.appendChild(c); })(document);"; injectScriptFile(uuid, urlFile);
injectDeferredObject(uuid, urlFile, jsWrapper, null);
result.success(true); result.success(true);
break; break;
case "injectStyleCode": case "injectStyleCode":
source = call.argument("source").toString(); source = call.argument("source").toString();
jsWrapper = "(function(d) { var c = d.createElement('style'); c.innerHTML = %s; d.body.appendChild(c); })(document);"; injectStyleCode(uuid, source);
injectDeferredObject(uuid, source, jsWrapper, null);
result.success(true); result.success(true);
break; break;
case "injectStyleFile": case "injectStyleFile":
urlFile = call.argument("urlFile").toString(); urlFile = call.argument("urlFile").toString();
jsWrapper = "(function(d) { var c = d.createElement('link'); c.rel='stylesheet'; c.type='text/css'; c.href = %s; d.head.appendChild(c); })(document);"; injectStyleFile(uuid, urlFile);
injectDeferredObject(uuid, urlFile, jsWrapper, null);
result.success(true); result.success(true);
break; break;
case "show": case "show":
...@@ -307,12 +303,33 @@ public class InAppBrowserFlutterPlugin implements MethodCallHandler { ...@@ -307,12 +303,33 @@ public class InAppBrowserFlutterPlugin implements MethodCallHandler {
} }
private void injectDeferredObject(String uuid, String source, String jsWrapper, final Result result) { private void injectScriptCode(String uuid, String source, final Result result) {
final InAppBrowserActivity inAppBrowserActivity = webViewActivities.get(uuid); final InAppBrowserActivity inAppBrowserActivity = webViewActivities.get(uuid);
if (inAppBrowserActivity != null) { if (inAppBrowserActivity != null) {
inAppBrowserActivity.injectDeferredObject(source, jsWrapper, result); inAppBrowserActivity.injectScriptCode(source, result);
} else { } else {
Log.d(LOG_TAG, "Can't inject code into the system browser"); Log.d(LOG_TAG, "webView is null");
}
}
private void injectScriptFile(String uuid, String urlFile) {
final InAppBrowserActivity inAppBrowserActivity = webViewActivities.get(uuid);
if (inAppBrowserActivity != null) {
inAppBrowserActivity.injectScriptFile(urlFile);
}
}
private void injectStyleCode(String uuid, String source) {
final InAppBrowserActivity inAppBrowserActivity = webViewActivities.get(uuid);
if (inAppBrowserActivity != null) {
inAppBrowserActivity.injectStyleCode(source);
}
}
private void injectStyleFile(String uuid, String urlFile) {
final InAppBrowserActivity inAppBrowserActivity = webViewActivities.get(uuid);
if (inAppBrowserActivity != null) {
inAppBrowserActivity.injectStyleFile(urlFile);
} }
} }
......
...@@ -406,6 +406,26 @@ public class InAppWebView extends WebView { ...@@ -406,6 +406,26 @@ public class InAppWebView extends WebView {
}); });
} }
public void injectScriptCode(String source, MethodChannel.Result result) {
String jsWrapper = "(function(){return JSON.stringify(eval(%s));})();";
injectDeferredObject(source, jsWrapper, result);
}
public void injectScriptFile(String urlFile) {
String jsWrapper = "(function(d) { var c = d.createElement('script'); c.src = %s; d.body.appendChild(c); })(document);";
injectDeferredObject(urlFile, jsWrapper, null);
}
public void injectStyleCode(String source) {
String jsWrapper = "(function(d) { var c = d.createElement('style'); c.innerHTML = %s; d.body.appendChild(c); })(document);";
injectDeferredObject(source, jsWrapper, null);
}
public void injectStyleFile(String urlFile) {
String jsWrapper = "(function(d) { var c = d.createElement('link'); c.rel='stylesheet'; c.type='text/css'; c.href = %s; d.head.appendChild(c); })(document);";
injectDeferredObject(urlFile, jsWrapper, null);
}
public HashMap<String, Object> getCopyBackForwardList() { public HashMap<String, Object> getCopyBackForwardList() {
WebBackForwardList currentList = copyBackForwardList(); WebBackForwardList currentList = copyBackForwardList();
int currentSize = currentList.getSize(); int currentSize = currentList.getSize();
......
...@@ -16,13 +16,13 @@ ...@@ -16,13 +16,13 @@
<img src="images/dart.svg" alt="dart logo"> <img src="images/dart.svg" alt="dart logo">
<div class="row"> <div class="row">
<div class="col-sm"> <div class="col-sm">
One of three columns <a href="page-1.html">PAGE 1</a>
</div> </div>
<div class="col-sm"> <div class="col-sm">
One of three columns <a href="page-2.html">PAGE 2</a>
</div> </div>
<div class="col-sm"> <div class="col-sm">
One of three columns <a href="page-3.html">PAGE 3</a>
</div> </div>
</div> </div>
</div> </div>
......
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css">
<link rel="stylesheet" href="css/style.css">
<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
</head>
<body>
<div class="container">
<div class="container">
<img src="images/dart.svg" alt="dart logo">
<div class="row">
<div class="col-sm">
<a href="index.html">GO BACK HOME</a>
</div>
<div class="col-sm">
<h1>PAGE 1</h1>
</div>
</div>
</div>
<script>
console.log("page 1");
</script>
</div>
</body>
</html>
\ No newline at end of file
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css">
<link rel="stylesheet" href="css/style.css">
<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
</head>
<body>
<div class="container">
<div class="container">
<img src="images/dart.svg" alt="dart logo">
<div class="row">
<div class="col-sm">
<a href="index.html">GO BACK HOME</a>
</div>
<div class="col-sm">
<h1>PAGE 2</h1>
</div>
</div>
</div>
<script>
console.log("page 2");
</script>
</div>
</body>
</html>
\ No newline at end of file
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css">
<link rel="stylesheet" href="css/style.css">
<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
</head>
<body>
<div class="container">
<div class="container">
<img src="images/dart.svg" alt="dart logo">
<div class="row">
<div class="col-sm">
<a href="index.html">GO BACK HOME</a>
</div>
<div class="col-sm">
<h1>PAGE 3</h1>
</div>
</div>
</div>
<script>
console.log("page 3");
</script>
</div>
</body>
</html>
\ No newline at end of file
...@@ -9,7 +9,6 @@ ...@@ -9,7 +9,6 @@
/* Begin PBXBuildFile section */ /* Begin PBXBuildFile section */
1498D2341E8E89220040F4C2 /* GeneratedPluginRegistrant.m in Sources */ = {isa = PBXBuildFile; fileRef = 1498D2331E8E89220040F4C2 /* GeneratedPluginRegistrant.m */; }; 1498D2341E8E89220040F4C2 /* GeneratedPluginRegistrant.m in Sources */ = {isa = PBXBuildFile; fileRef = 1498D2331E8E89220040F4C2 /* GeneratedPluginRegistrant.m */; };
25A517508F43E58C47090625 /* Pods_Runner.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = E8D91E403808A7540F18B75D /* Pods_Runner.framework */; }; 25A517508F43E58C47090625 /* Pods_Runner.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = E8D91E403808A7540F18B75D /* Pods_Runner.framework */; };
2D5378261FAA1A9400D5DBA9 /* flutter_assets in Resources */ = {isa = PBXBuildFile; fileRef = 2D5378251FAA1A9400D5DBA9 /* flutter_assets */; };
3B3967161E833CAA004F5970 /* AppFrameworkInfo.plist in Resources */ = {isa = PBXBuildFile; fileRef = 3B3967151E833CAA004F5970 /* AppFrameworkInfo.plist */; }; 3B3967161E833CAA004F5970 /* AppFrameworkInfo.plist in Resources */ = {isa = PBXBuildFile; fileRef = 3B3967151E833CAA004F5970 /* AppFrameworkInfo.plist */; };
3B80C3941E831B6300D905FE /* App.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 3B80C3931E831B6300D905FE /* App.framework */; }; 3B80C3941E831B6300D905FE /* App.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 3B80C3931E831B6300D905FE /* App.framework */; };
3B80C3951E831B6300D905FE /* App.framework in Embed Frameworks */ = {isa = PBXBuildFile; fileRef = 3B80C3931E831B6300D905FE /* App.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; }; 3B80C3951E831B6300D905FE /* App.framework in Embed Frameworks */ = {isa = PBXBuildFile; fileRef = 3B80C3931E831B6300D905FE /* App.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; };
...@@ -39,7 +38,6 @@ ...@@ -39,7 +38,6 @@
/* Begin PBXFileReference section */ /* Begin PBXFileReference section */
1498D2321E8E86230040F4C2 /* GeneratedPluginRegistrant.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = GeneratedPluginRegistrant.h; sourceTree = "<group>"; }; 1498D2321E8E86230040F4C2 /* GeneratedPluginRegistrant.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = GeneratedPluginRegistrant.h; sourceTree = "<group>"; };
1498D2331E8E89220040F4C2 /* GeneratedPluginRegistrant.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = GeneratedPluginRegistrant.m; sourceTree = "<group>"; }; 1498D2331E8E89220040F4C2 /* GeneratedPluginRegistrant.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = GeneratedPluginRegistrant.m; sourceTree = "<group>"; };
2D5378251FAA1A9400D5DBA9 /* flutter_assets */ = {isa = PBXFileReference; lastKnownFileType = folder; name = flutter_assets; path = Flutter/flutter_assets; sourceTree = SOURCE_ROOT; };
3B3967151E833CAA004F5970 /* AppFrameworkInfo.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; name = AppFrameworkInfo.plist; path = Flutter/AppFrameworkInfo.plist; sourceTree = "<group>"; }; 3B3967151E833CAA004F5970 /* AppFrameworkInfo.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; name = AppFrameworkInfo.plist; path = Flutter/AppFrameworkInfo.plist; sourceTree = "<group>"; };
3B80C3931E831B6300D905FE /* App.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = App.framework; path = Flutter/App.framework; sourceTree = "<group>"; }; 3B80C3931E831B6300D905FE /* App.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = App.framework; path = Flutter/App.framework; sourceTree = "<group>"; };
74858FAD1ED2DC5600515810 /* Runner-Bridging-Header.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "Runner-Bridging-Header.h"; sourceTree = "<group>"; }; 74858FAD1ED2DC5600515810 /* Runner-Bridging-Header.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "Runner-Bridging-Header.h"; sourceTree = "<group>"; };
...@@ -88,7 +86,6 @@ ...@@ -88,7 +86,6 @@
9740EEB11CF90186004384FC /* Flutter */ = { 9740EEB11CF90186004384FC /* Flutter */ = {
isa = PBXGroup; isa = PBXGroup;
children = ( children = (
2D5378251FAA1A9400D5DBA9 /* flutter_assets */,
3B80C3931E831B6300D905FE /* App.framework */, 3B80C3931E831B6300D905FE /* App.framework */,
3B3967151E833CAA004F5970 /* AppFrameworkInfo.plist */, 3B3967151E833CAA004F5970 /* AppFrameworkInfo.plist */,
9740EEBA1CF902C7004384FC /* Flutter.framework */, 9740EEBA1CF902C7004384FC /* Flutter.framework */,
...@@ -209,7 +206,6 @@ ...@@ -209,7 +206,6 @@
EDC1147F21735BC200D2247A /* Main.storyboard in Resources */, EDC1147F21735BC200D2247A /* Main.storyboard in Resources */,
3B3967161E833CAA004F5970 /* AppFrameworkInfo.plist in Resources */, 3B3967161E833CAA004F5970 /* AppFrameworkInfo.plist in Resources */,
97C146FE1CF9000F007C117D /* Assets.xcassets in Resources */, 97C146FE1CF9000F007C117D /* Assets.xcassets in Resources */,
2D5378261FAA1A9400D5DBA9 /* flutter_assets in Resources */,
); );
runOnlyForDeploymentPostprocessing = 0; runOnlyForDeploymentPostprocessing = 0;
}; };
......
...@@ -38,7 +38,7 @@ class _ChromeSafariExampleScreenState extends State<ChromeSafariExampleScreen> { ...@@ -38,7 +38,7 @@ class _ChromeSafariExampleScreenState extends State<ChromeSafariExampleScreen> {
return new Center( return new Center(
child: new RaisedButton( child: new RaisedButton(
onPressed: () async { onPressed: () async {
await widget.browser.open("https://flutter.io/"); await widget.browser.open("https://flutter.dev/");
}, },
child: Text("Open Chrome Safari Browser")), child: Text("Open Chrome Safari Browser")),
); );
......
...@@ -40,9 +40,12 @@ class _InlineExampleScreenState extends State<InlineExampleScreen> { ...@@ -40,9 +40,12 @@ class _InlineExampleScreenState extends State<InlineExampleScreen> {
decoration: decoration:
BoxDecoration(border: Border.all(color: Colors.blueAccent)), BoxDecoration(border: Border.all(color: Colors.blueAccent)),
child: InAppWebView( child: InAppWebView(
initialUrl: "https://flutter.io/", initialUrl: "https://flutter.dev/",
initialHeaders: {}, initialHeaders: {},
initialOptions: {}, initialOptions: {
"useShouldOverrideUrlLoading": true,
"useOnLoadResource": true
},
onWebViewCreated: (InAppWebViewController controller) { onWebViewCreated: (InAppWebViewController controller) {
webView = controller; webView = controller;
}, },
...@@ -52,12 +55,22 @@ class _InlineExampleScreenState extends State<InlineExampleScreen> { ...@@ -52,12 +55,22 @@ class _InlineExampleScreenState extends State<InlineExampleScreen> {
this.url = url; this.url = url;
}); });
}, },
onLoadStop: (InAppWebViewController controller, String url) {
print("stopped $url");
},
onProgressChanged: onProgressChanged:
(InAppWebViewController controller, int progress) { (InAppWebViewController controller, int progress) {
setState(() { setState(() {
this.progress = progress / 100; this.progress = progress / 100;
}); });
}, },
shouldOverrideUrlLoading: (InAppWebViewController controller, String url) {
print("override $url");
controller.loadUrl(url);
},
onLoadResource: (InAppWebViewController controller, WebResourceResponse response, WebResourceRequest request) {
print("resource " + request.url);
}
), ),
), ),
), ),
......
...@@ -5,7 +5,10 @@ import 'package:flutter_inappbrowser_example/chrome_safari_example.screen.dart'; ...@@ -5,7 +5,10 @@ import 'package:flutter_inappbrowser_example/chrome_safari_example.screen.dart';
import 'package:flutter_inappbrowser_example/inline_example.screen.dart'; import 'package:flutter_inappbrowser_example/inline_example.screen.dart';
import 'package:flutter_inappbrowser_example/webview_example.screen.dart'; import 'package:flutter_inappbrowser_example/webview_example.screen.dart';
// InAppLocalhostServer localhostServer = new InAppLocalhostServer();
Future main() async { Future main() async {
// await localhostServer.start();
runApp(new MyApp()); runApp(new MyApp());
} }
......
...@@ -65,7 +65,7 @@ class MyInappBrowser extends InAppBrowser { ...@@ -65,7 +65,7 @@ class MyInappBrowser extends InAppBrowser {
} }
class WebviewExampleScreen extends StatefulWidget { class WebviewExampleScreen extends StatefulWidget {
final InAppBrowser browser = new MyInappBrowser(); final MyInappBrowser browser = new MyInappBrowser();
@override @override
_WebviewExampleScreenState createState() => new _WebviewExampleScreenState(); _WebviewExampleScreenState createState() => new _WebviewExampleScreenState();
} }
...@@ -81,7 +81,10 @@ class _WebviewExampleScreenState extends State<WebviewExampleScreen> { ...@@ -81,7 +81,10 @@ class _WebviewExampleScreenState extends State<WebviewExampleScreen> {
return new Center( return new Center(
child: new RaisedButton( child: new RaisedButton(
onPressed: () { onPressed: () {
widget.browser.open(url:"https://flutter.io/"); widget.browser.open(url: "https://flutter.dev/", options: {
"useShouldOverrideUrlLoading": true,
"useOnLoadResource": true
});
}, },
child: Text("Open Webview Browser")), child: Text("Open Webview Browser")),
); );
......
...@@ -40,6 +40,9 @@ flutter: ...@@ -40,6 +40,9 @@ flutter:
assets: assets:
- assets/index.html - assets/index.html
- assets/page-1.html
- assets/page-2.html
- assets/page-3.html
- assets/css/ - assets/css/
- assets/images/ - assets/images/
......
...@@ -20,6 +20,8 @@ ...@@ -20,6 +20,8 @@
<excludeFolder url="file://$MODULE_DIR$/example/ios/.symlinks/plugins/flutter_inappbrowser/example/.dart_tool" /> <excludeFolder url="file://$MODULE_DIR$/example/ios/.symlinks/plugins/flutter_inappbrowser/example/.dart_tool" />
<excludeFolder url="file://$MODULE_DIR$/example/ios/.symlinks/plugins/flutter_inappbrowser/example/.pub" /> <excludeFolder url="file://$MODULE_DIR$/example/ios/.symlinks/plugins/flutter_inappbrowser/example/.pub" />
<excludeFolder url="file://$MODULE_DIR$/example/ios/.symlinks/plugins/flutter_inappbrowser/example/build" /> <excludeFolder url="file://$MODULE_DIR$/example/ios/.symlinks/plugins/flutter_inappbrowser/example/build" />
<excludeFolder url="file://$MODULE_DIR$/example/ios/.symlinks/plugins/flutter_inappbrowser/example/ios/Flutter/App.framework/flutter_assets/packages" />
<excludeFolder url="file://$MODULE_DIR$/example/ios/.symlinks/plugins/flutter_inappbrowser/example/ios/Flutter/flutter_assets/packages" />
<excludeFolder url="file://$MODULE_DIR$/example/ios/Flutter/App.framework/flutter_assets/packages" /> <excludeFolder url="file://$MODULE_DIR$/example/ios/Flutter/App.framework/flutter_assets/packages" />
<excludeFolder url="file://$MODULE_DIR$/example/ios/Flutter/flutter_assets/packages" /> <excludeFolder url="file://$MODULE_DIR$/example/ios/Flutter/flutter_assets/packages" />
</content> </content>
......
...@@ -24,7 +24,7 @@ public class FlutterWebViewController: NSObject, FlutterPlatformView { ...@@ -24,7 +24,7 @@ public class FlutterWebViewController: NSObject, FlutterPlatformView {
self.channel = FlutterMethodChannel(name: channelName, binaryMessenger: registrar.messenger()) self.channel = FlutterMethodChannel(name: channelName, binaryMessenger: registrar.messenger())
self.channel?.setMethodCallHandler(self.handle) self.channel?.setMethodCallHandler(self.handle)
var initialUrl = (args["initialUrl"] as? String)! let initialUrl = (args["initialUrl"] as? String)!
let initialFile = args["initialFile"] as? String let initialFile = args["initialFile"] as? String
let initialData = args["initialData"] as? [String: String] let initialData = args["initialData"] as? [String: String]
let initialHeaders = (args["initialHeaders"] as? [String: String])! let initialHeaders = (args["initialHeaders"] as? [String: String])!
...@@ -134,35 +134,29 @@ public class FlutterWebViewController: NSObject, FlutterPlatformView { ...@@ -134,35 +134,29 @@ public class FlutterWebViewController: NSObject, FlutterPlatformView {
webView!.injectScriptCode(source: source, result: result) webView!.injectScriptCode(source: source, result: result)
} }
else { else {
result(false) result("")
} }
break break
case "injectScriptFile": case "injectScriptFile":
if webView != nil { if webView != nil {
let urlFile = (arguments!["urlFile"] as? String)! let urlFile = (arguments!["urlFile"] as? String)!
webView!.injectScriptFile(urlFile: urlFile, result: result) webView!.injectScriptFile(urlFile: urlFile)
}
else {
result(false)
} }
result(true)
break break
case "injectStyleCode": case "injectStyleCode":
if webView != nil { if webView != nil {
let source = (arguments!["source"] as? String)! let source = (arguments!["source"] as? String)!
webView!.injectStyleCode(source: source, result: result) webView!.injectStyleCode(source: source)
}
else {
result(false)
} }
result(true)
break break
case "injectStyleFile": case "injectStyleFile":
if webView != nil { if webView != nil {
let urlFile = (arguments!["urlFile"] as? String)! let urlFile = (arguments!["urlFile"] as? String)!
webView!.injectStyleFile(urlFile: urlFile, result: result) webView!.injectStyleFile(urlFile: urlFile)
}
else {
result(false)
} }
result(true)
break break
case "reload": case "reload":
if webView != nil { if webView != nil {
......
...@@ -154,7 +154,7 @@ class InAppBrowserWebViewController: UIViewController, UIScrollViewDelegate, WKU ...@@ -154,7 +154,7 @@ class InAppBrowserWebViewController: UIViewController, UIScrollViewDelegate, WKU
loadUrl(url: self.initURL!, headers: self.initHeaders) loadUrl(url: self.initURL!, headers: self.initHeaders)
} }
else { else {
loadData(data: initData!, mimeType: initMimeType!, encoding: initEncoding!, baseUrl: initBaseUrl!) webView.loadData(data: initData!, mimeType: initMimeType!, encoding: initEncoding!, baseUrl: initBaseUrl!)
} }
navigationDelegate?.onBrowserCreated(uuid: uuid, webView: webView) navigationDelegate?.onBrowserCreated(uuid: uuid, webView: webView)
...@@ -225,16 +225,6 @@ class InAppBrowserWebViewController: UIViewController, UIScrollViewDelegate, WKU ...@@ -225,16 +225,6 @@ class InAppBrowserWebViewController: UIViewController, UIScrollViewDelegate, WKU
updateUrlTextField(url: (webView.currentURL?.absoluteString)!) updateUrlTextField(url: (webView.currentURL?.absoluteString)!)
} }
func loadData(data: String, mimeType: String, encoding: String, baseUrl: String) {
webView.loadData(data: data, mimeType: mimeType, encoding: encoding, baseUrl: baseUrl)
}
func postUrl(url: URL, postData: Data, result: @escaping FlutterResult) {
webView.postUrl(url: url, postData: postData, completionHandler: { () -> Void in
result(true)
})
}
// Load user requested url // Load user requested url
func textFieldShouldReturn(_ textField: UITextField) -> Bool { func textFieldShouldReturn(_ textField: UITextField) -> Bool {
textField.resignFirstResponder() textField.resignFirstResponder()
......
...@@ -279,8 +279,7 @@ public class InAppWebView: WKWebView, UIScrollViewDelegate, WKUIDelegate, WKNavi ...@@ -279,8 +279,7 @@ public class InAppWebView: WKWebView, UIScrollViewDelegate, WKUIDelegate, WKNavi
if assetURL == nil { if assetURL == nil {
throw NSError(domain: url + " asset file cannot be found!", code: 0) throw NSError(domain: url + " asset file cannot be found!", code: 0)
} }
let absoluteUrl = URL(string: url)!.absoluteURL loadUrl(url: assetURL!, headers: headers)
loadUrl(url: absoluteUrl, headers: headers)
} }
func setOptions(newOptions: InAppWebViewOptions, newOptionsMap: [String: Any]) { func setOptions(newOptions: InAppWebViewOptions, newOptionsMap: [String: Any]) {
...@@ -440,19 +439,19 @@ public class InAppWebView: WKWebView, UIScrollViewDelegate, WKUIDelegate, WKNavi ...@@ -440,19 +439,19 @@ public class InAppWebView: WKWebView, UIScrollViewDelegate, WKUIDelegate, WKNavi
injectDeferredObject(source: source, withWrapper: jsWrapper, result: result) injectDeferredObject(source: source, withWrapper: jsWrapper, result: result)
} }
public func injectScriptFile(urlFile: String, result: FlutterResult?) { public func injectScriptFile(urlFile: String) {
let jsWrapper = "(function(d) { var c = d.createElement('script'); c.src = %@; d.body.appendChild(c); })(document);" let jsWrapper = "(function(d) { var c = d.createElement('script'); c.src = %@; d.body.appendChild(c); })(document);"
injectDeferredObject(source: urlFile, withWrapper: jsWrapper, result: result) injectDeferredObject(source: urlFile, withWrapper: jsWrapper, result: nil)
} }
public func injectStyleCode(source: String, result: FlutterResult?) { public func injectStyleCode(source: String) {
let jsWrapper = "(function(d) { var c = d.createElement('style'); c.innerHTML = %@; d.body.appendChild(c); })(document);" let jsWrapper = "(function(d) { var c = d.createElement('style'); c.innerHTML = %@; d.body.appendChild(c); })(document);"
injectDeferredObject(source: source, withWrapper: jsWrapper, result: result) injectDeferredObject(source: source, withWrapper: jsWrapper, result: nil)
} }
public func injectStyleFile(urlFile: String, result: FlutterResult?) { public func injectStyleFile(urlFile: String) {
let jsWrapper = "(function(d) { var c = d.createElement('link'); c.rel='stylesheet', c.type='text/css'; c.href = %@; d.body.appendChild(c); })(document);" let jsWrapper = "(function(d) { var c = d.createElement('link'); c.rel='stylesheet', c.type='text/css'; c.href = %@; d.body.appendChild(c); })(document);"
injectDeferredObject(source: urlFile, withWrapper: jsWrapper, result: result) injectDeferredObject(source: urlFile, withWrapper: jsWrapper, result: nil)
} }
public func getCopyBackForwardList() -> [String: Any] { public func getCopyBackForwardList() -> [String: Any] {
......
This diff is collapsed.
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: 1.0.0 version: 1.0.1
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
......
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