Sfoglia il codice sorgente

Merge pull request #123 from readytopark/master

Add headers when loading url
Hadrien Lejard 7 anni fa
parent
commit
07c3177063

+ 2 - 1
README.md

@@ -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,

+ 4 - 0
android/src/main/java/com/flutter_webview_plugin/FlutterWebviewPlugin.java

@@ -83,6 +83,7 @@ public class FlutterWebviewPlugin implements MethodCallHandler, PluginRegistry.A
         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) {
@@ -99,6 +100,7 @@ public class FlutterWebviewPlugin implements MethodCallHandler, PluginRegistry.A
                 clearCookies,
                 userAgent,
                 url,
+                headers,
                 withZoom,
                 withLocalStorage,
                 scrollBar
@@ -167,6 +169,8 @@ public class FlutterWebviewPlugin implements MethodCallHandler, PluginRegistry.A
                     false,
                     "",
                     url,
+                    null,
+                    false,
                     false,
                     false
             );

+ 9 - 3
android/src/main/java/com/flutter_webview_plugin/WebviewManager.java

@@ -16,6 +16,8 @@ import android.webkit.WebView;
 import android.webkit.WebViewClient;
 import android.widget.FrameLayout;
 
+import java.util.Map;
+
 import io.flutter.plugin.common.MethodCall;
 import io.flutter.plugin.common.MethodChannel;
 
@@ -175,7 +177,7 @@ class WebviewManager {
         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);
@@ -196,12 +198,16 @@ class WebviewManager {
         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) {

+ 7 - 1
ios/Classes/FlutterWebviewPlugin.m

@@ -123,7 +123,13 @@ static NSString *const CHANNEL_NAME = @"flutter_webview_plugin";
                     @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];
             }
         }

+ 10 - 3
lib/src/base.dart

@@ -63,6 +63,7 @@ class FlutterWebviewPlugin {
   Stream<WebViewHttpError> get onHttpError => _onHttpError.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
@@ -80,7 +81,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,
@@ -104,6 +106,11 @@ class FlutterWebviewPlugin {
       "withLocalUrl": withLocalUrl ?? false,
       "scrollBar": scrollBar ?? true
     };
+
+    if (headers != null) {
+      args["headers"] = headers;
+    }
+
     if (rect != null) {
       args["rect"] = {
         "left": rect.left,
@@ -136,10 +143,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");
 

+ 5 - 0
lib/src/webview_scaffold.dart

@@ -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;
@@ -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,
@@ -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,