Skip to content

Add headers when loading url #123

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Aug 1, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,8 @@ flutterWebviewPlugin.launch(url,

```dart
Future<Null> launch(String url,
{bool withJavascript: true,
{Map<String, String> headers: null,
bool withJavascript: true,
bool clearCache: false,
bool clearCookies: false,
bool hidden: false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ private void openUrl(MethodCall call, MethodChannel.Result result) {
boolean clearCookies = call.argument("clearCookies");
boolean withZoom = call.argument("withZoom");
boolean withLocalStorage = call.argument("withLocalStorage");
Map<String, String> headers = call.argument("headers");
boolean scrollBar = call.argument("scrollBar");

if (webViewManager == null || webViewManager.closed == true) {
Expand All @@ -99,6 +100,7 @@ private void openUrl(MethodCall call, MethodChannel.Result result) {
clearCookies,
userAgent,
url,
headers,
withZoom,
withLocalStorage,
scrollBar
Expand Down Expand Up @@ -167,6 +169,8 @@ private void reloadUrl(MethodCall call, MethodChannel.Result result) {
false,
"",
url,
null,
false,
false,
false
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
import android.webkit.WebViewClient;
import android.widget.FrameLayout;

import java.util.Map;

import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;

Expand Down Expand Up @@ -175,7 +177,7 @@ private void clearCache() {
webView.clearFormData();
}

void openUrl(boolean withJavascript, boolean clearCache, boolean hidden, boolean clearCookies, String userAgent, String url, boolean withZoom, boolean withLocalStorage, boolean scrollBar) {
void openUrl(boolean withJavascript, boolean clearCache, boolean hidden, boolean clearCookies, String userAgent, String url, Map<String, String> headers, boolean withZoom, boolean withLocalStorage, boolean scrollBar) {
webView.getSettings().setJavaScriptEnabled(withJavascript);
webView.getSettings().setBuiltInZoomControls(withZoom);
webView.getSettings().setSupportZoom(withZoom);
Expand All @@ -196,12 +198,16 @@ void openUrl(boolean withJavascript, boolean clearCache, boolean hidden, boolean
if (userAgent != null) {
webView.getSettings().setUserAgentString(userAgent);
}

if(!scrollBar){
webView.setVerticalScrollBarEnabled(false);
}

webView.loadUrl(url);
if (headers != null) {
webView.loadUrl(url, headers);
} else {
webView.loadUrl(url);
}
}

void close(MethodCall call, MethodChannel.Result result) {
Expand Down
8 changes: 7 additions & 1 deletion ios/Classes/FlutterWebviewPlugin.m
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,13 @@ - (void)navigate:(FlutterMethodCall*)call {
@throw @"not available on version earlier than ios 9.0";
}
} else {
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:url]];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:url]];
NSDictionary *headers = call.arguments[@"headers"];

if (headers != nil) {
[request setAllHTTPHeaderFields:headers];
}

[self.webview loadRequest:request];
}
}
Expand Down
13 changes: 10 additions & 3 deletions lib/src/base.dart
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ class FlutterWebviewPlugin {
Stream<WebViewStateChanged> get onStateChanged => _onStateChanged.stream;

/// Start the Webview with [url]
/// - [headers] specify additional HTTP headers
/// - [withJavascript] enable Javascript or not for the Webview
/// iOS WebView: Not implemented yet
/// - [clearCache] clear the cache of the Webview
Expand All @@ -78,7 +79,8 @@ class FlutterWebviewPlugin {
/// Allow local files on iOs > 9.0
/// - [scrollBar]: enable or disable scrollbar
Future<Null> launch(String url,
{bool withJavascript,
{Map<String, String> headers,
bool withJavascript,
bool clearCache,
bool clearCookies,
bool hidden,
Expand All @@ -102,6 +104,11 @@ class FlutterWebviewPlugin {
"withLocalUrl": withLocalUrl ?? false,
"scrollBar": scrollBar ?? true
};

if (headers != null) {
args["headers"] = headers;
}

if (rect != null) {
args["rect"] = {
"left": rect.left,
Expand Down Expand Up @@ -134,10 +141,10 @@ class FlutterWebviewPlugin {
/// Navigates forward on the Webview.
/// This is only available on Android for now.
Future goForward() => _channel.invokeMethod("forward");

// Hides the webview
Future hide() => _channel.invokeMethod("hide");

// Shows the webview
Future show() => _channel.invokeMethod("show");

Expand Down
5 changes: 5 additions & 0 deletions lib/src/webview_scaffold.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import 'package:flutter/material.dart';
import 'base.dart';

class WebviewScaffold extends StatefulWidget {

final PreferredSizeWidget appBar;
final String url;
final bool withJavascript;
Expand All @@ -21,10 +22,13 @@ class WebviewScaffold extends StatefulWidget {
final bool withLocalUrl;
final bool scrollBar;

final Map<String, String> headers;

WebviewScaffold(
{Key key,
this.appBar,
@required this.url,
this.headers,
this.withJavascript,
this.clearCache,
this.clearCookies,
Expand Down Expand Up @@ -64,6 +68,7 @@ class _WebviewScaffoldState extends State<WebviewScaffold> {
if (_rect == null) {
_rect = _buildRect(context);
webviewReference.launch(widget.url,
headers: widget.headers,
withJavascript: widget.withJavascript,
clearCache: widget.clearCache,
clearCookies: widget.clearCookies,
Expand Down