Skip to content

Commit 07c3177

Browse files
authored
Merge pull request #123 from readytopark/master
Add headers when loading url
2 parents 4c44dae + 129f2db commit 07c3177

File tree

6 files changed

+37
-8
lines changed

6 files changed

+37
-8
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,8 @@ flutterWebviewPlugin.launch(url,
8484

8585
```dart
8686
Future<Null> launch(String url,
87-
{bool withJavascript: true,
87+
{Map<String, String> headers: null,
88+
bool withJavascript: true,
8889
bool clearCache: false,
8990
bool clearCookies: false,
9091
bool hidden: false,

android/src/main/java/com/flutter_webview_plugin/FlutterWebviewPlugin.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ private void openUrl(MethodCall call, MethodChannel.Result result) {
8383
boolean clearCookies = call.argument("clearCookies");
8484
boolean withZoom = call.argument("withZoom");
8585
boolean withLocalStorage = call.argument("withLocalStorage");
86+
Map<String, String> headers = call.argument("headers");
8687
boolean scrollBar = call.argument("scrollBar");
8788

8889
if (webViewManager == null || webViewManager.closed == true) {
@@ -99,6 +100,7 @@ private void openUrl(MethodCall call, MethodChannel.Result result) {
99100
clearCookies,
100101
userAgent,
101102
url,
103+
headers,
102104
withZoom,
103105
withLocalStorage,
104106
scrollBar
@@ -167,6 +169,8 @@ private void reloadUrl(MethodCall call, MethodChannel.Result result) {
167169
false,
168170
"",
169171
url,
172+
null,
173+
false,
170174
false,
171175
false
172176
);

android/src/main/java/com/flutter_webview_plugin/WebviewManager.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
import android.webkit.WebViewClient;
1717
import android.widget.FrameLayout;
1818

19+
import java.util.Map;
20+
1921
import io.flutter.plugin.common.MethodCall;
2022
import io.flutter.plugin.common.MethodChannel;
2123

@@ -175,7 +177,7 @@ private void clearCache() {
175177
webView.clearFormData();
176178
}
177179

178-
void openUrl(boolean withJavascript, boolean clearCache, boolean hidden, boolean clearCookies, String userAgent, String url, boolean withZoom, boolean withLocalStorage, boolean scrollBar) {
180+
void openUrl(boolean withJavascript, boolean clearCache, boolean hidden, boolean clearCookies, String userAgent, String url, Map<String, String> headers, boolean withZoom, boolean withLocalStorage, boolean scrollBar) {
179181
webView.getSettings().setJavaScriptEnabled(withJavascript);
180182
webView.getSettings().setBuiltInZoomControls(withZoom);
181183
webView.getSettings().setSupportZoom(withZoom);
@@ -196,12 +198,16 @@ void openUrl(boolean withJavascript, boolean clearCache, boolean hidden, boolean
196198
if (userAgent != null) {
197199
webView.getSettings().setUserAgentString(userAgent);
198200
}
199-
201+
200202
if(!scrollBar){
201203
webView.setVerticalScrollBarEnabled(false);
202204
}
203205

204-
webView.loadUrl(url);
206+
if (headers != null) {
207+
webView.loadUrl(url, headers);
208+
} else {
209+
webView.loadUrl(url);
210+
}
205211
}
206212

207213
void close(MethodCall call, MethodChannel.Result result) {

ios/Classes/FlutterWebviewPlugin.m

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,13 @@ - (void)navigate:(FlutterMethodCall*)call {
123123
@throw @"not available on version earlier than ios 9.0";
124124
}
125125
} else {
126-
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:url]];
126+
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:url]];
127+
NSDictionary *headers = call.arguments[@"headers"];
128+
129+
if (headers != nil) {
130+
[request setAllHTTPHeaderFields:headers];
131+
}
132+
127133
[self.webview loadRequest:request];
128134
}
129135
}

lib/src/base.dart

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ class FlutterWebviewPlugin {
6363
Stream<WebViewHttpError> get onHttpError => _onHttpError.stream;
6464

6565
/// Start the Webview with [url]
66+
/// - [headers] specify additional HTTP headers
6667
/// - [withJavascript] enable Javascript or not for the Webview
6768
/// iOS WebView: Not implemented yet
6869
/// - [clearCache] clear the cache of the Webview
@@ -80,7 +81,8 @@ class FlutterWebviewPlugin {
8081
/// Allow local files on iOs > 9.0
8182
/// - [scrollBar]: enable or disable scrollbar
8283
Future<Null> launch(String url,
83-
{bool withJavascript,
84+
{Map<String, String> headers,
85+
bool withJavascript,
8486
bool clearCache,
8587
bool clearCookies,
8688
bool hidden,
@@ -104,6 +106,11 @@ class FlutterWebviewPlugin {
104106
"withLocalUrl": withLocalUrl ?? false,
105107
"scrollBar": scrollBar ?? true
106108
};
109+
110+
if (headers != null) {
111+
args["headers"] = headers;
112+
}
113+
107114
if (rect != null) {
108115
args["rect"] = {
109116
"left": rect.left,
@@ -136,10 +143,10 @@ class FlutterWebviewPlugin {
136143
/// Navigates forward on the Webview.
137144
/// This is only available on Android for now.
138145
Future goForward() => _channel.invokeMethod("forward");
139-
146+
140147
// Hides the webview
141148
Future hide() => _channel.invokeMethod("hide");
142-
149+
143150
// Shows the webview
144151
Future show() => _channel.invokeMethod("show");
145152

lib/src/webview_scaffold.dart

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import 'package:flutter/material.dart';
66
import 'base.dart';
77

88
class WebviewScaffold extends StatefulWidget {
9+
910
final PreferredSizeWidget appBar;
1011
final String url;
1112
final bool withJavascript;
@@ -21,10 +22,13 @@ class WebviewScaffold extends StatefulWidget {
2122
final bool withLocalUrl;
2223
final bool scrollBar;
2324

25+
final Map<String, String> headers;
26+
2427
WebviewScaffold(
2528
{Key key,
2629
this.appBar,
2730
@required this.url,
31+
this.headers,
2832
this.withJavascript,
2933
this.clearCache,
3034
this.clearCookies,
@@ -64,6 +68,7 @@ class _WebviewScaffoldState extends State<WebviewScaffold> {
6468
if (_rect == null) {
6569
_rect = _buildRect(context);
6670
webviewReference.launch(widget.url,
71+
headers: widget.headers,
6772
withJavascript: widget.withJavascript,
6873
clearCache: widget.clearCache,
6974
clearCookies: widget.clearCookies,

0 commit comments

Comments
 (0)