Can't pass headers in flutter_inappwebview & webview_flutter in iOS

Viewed 578

versions:

flutter_inappwebview: 5.3.2

webview_flutter: 2.3.1

I'm trying to pass headers to a webview URL, on Android everything is working fine but in ios, I'm getting an unauthorized error displayed.

I tried adding NSAppTransportSecurity in Info.plist

<key>NSAppTransportSecurity</key>
<dict>
    <key>NSAllowsArbitraryLoads</key>
    <true/>
    <key>NSAllowsArbitraryLoadsInWebContent</key>
    <true/>
</dict>

also enabled embedded preview

<key>io.flutter.embedded_views_preview</key>
<true/>

but unable to make it work. Below is my code.

code:

webview_flutter:

body: WebView(
javascriptMode: JavascriptMode.unrestricted,
onWebViewCreated: (c) {
c.loadUrl(
  url,
  headers: {'Authorization': 'Bearer ' + token},
);
},
),

flutter_inappwebview:

body: InAppWebView(
    initialUrlRequest: URLRequest(
      url: Uri.parse(url),
      headers: {'Authorization': 'Bearer ' + token},
    ),
  ),
1 Answers

initialize variable in the stateful class and try this solution

   bool _init = true;  

   onLoadStop: (controller, url) async {
                      if (Platform.isIOS) {
                        if (_isInit) {
                          
                          await webViewController!.loadUrl(
                            urlRequest: URLRequest(
                              url: url,
                              headers: headers,
                            ),
                          );
                          setState(() {
                            _isInit = false;
                          });
                        }
                      }
                      pullToRefreshController!.endRefreshing();

                      this.url = url.toString();
                      urlController.text = this.url;
                    },
Related