pichillilorenzo / flutter_inappwebview

A Flutter plugin that allows you to add an inline webview, to use a headless webview, and to open an in-app browser window.
https://inappwebview.dev
Apache License 2.0
3.28k stars 1.62k forks source link

Can't pass headers in iOS #1049

Closed jitendrapurohit12 closed 2 years ago

jitendrapurohit12 commented 2 years ago

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 also enabled embedded preview.

Environment

Technology Version
Flutter version 2.5.3
Plugin version 5.3.2
iOS version 14.7.1
Xcode version 12.5

Device information: all iOS devices

Steps to reproduce

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

Stacktrace/Logcat

WF: === Starting WebFilter logging for process Runner WF: _userSettingsForUser mobile: { filterBlacklist = ( ); filterWhitelist = ( ); restrictWeb = 1; useContentFilter = 0; useContentFilterOverrides = 0; whitelistEnabled = 0; } WF: _WebFilterIsActive returning: NO

github-actions[bot] commented 2 years ago

👋 @jitendrapurohit12

NOTE: This comment is auto-generated.

Are you sure you have already searched for the same problem?

Some people open new issues but they didn't search for something similar or for the same issue. Please, search for it using the GitHub issue search box or on the official inappwebview.dev website, or, also, using Google, StackOverflow, etc. before posting a new one. You may already find an answer to your problem!

If this is really a new issue, then thank you for raising it. I will investigate it and get back to you as soon as possible. Please, make sure you have given me as much context as possible! Also, if you didn't already, post a code example that can replicate this issue.

In the meantime, you can already search for some possible solutions online! Because this plugin uses native WebView, you can search online for the same issue adding android WebView [MY ERROR HERE] or ios WKWebView [MY ERROR HERE] keywords.

Following these steps can save you, me, and other people a lot of time, thanks!

pconradjunior commented 2 years ago

Hey, I have the same problem here... In Android everything goes fine, but in iOS I need a clue on how to solve it.

jitendrapurohit12 commented 2 years ago

@pconradjunior no, didn't find any workaround for this issue.

pconradjunior commented 2 years ago

@jitendrapurohit12 I hope 2022 brings us a light on it. I'll keep looking for a solution, but I have some theories:

a) Passing the header value in a post or; b) Try to pass it in another subsequent request.

I think I'll try both techniques to see the most suitable for my case when I go through the iOS implementation process. Now I'm working primarily on Android

marinat commented 2 years ago

@pconradjunior did you find solution?

pconradjunior commented 2 years ago

@marinat not yet. Had to pause some of my iOS development (fortunately).

pichillilorenzo commented 2 years ago

Just tested using latest version and it works correctly.

Here is my code example tested on a real iPhone device in release mode:

import 'dart:async';
import 'dart:developer';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter_inappwebview/flutter_inappwebview.dart';

Future main() async {
  WidgetsFlutterBinding.ensureInitialized();

  if (defaultTargetPlatform == TargetPlatform.android) {
    await AndroidInAppWebViewController.setWebContentsDebuggingEnabled(true);
  }

  runApp(const MaterialApp(
      home: MyApp()
  ));
}

class MyApp extends StatefulWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {

  final GlobalKey webViewKey = GlobalKey();

  InAppWebViewController? webViewController;
  InAppWebViewGroupOptions options = InAppWebViewGroupOptions(
      android: AndroidInAppWebViewOptions(
        useHybridComposition: true,
      ));

  final bearerToken = "MY_TOKEN_EXAMPLE";

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(title: const Text("InAppWebView test")),
        body: SafeArea(
            child: Column(children: <Widget>[
              Expanded(
                child: Stack(
                  children: [
                    InAppWebView(
                      key: webViewKey,
                      initialUrlRequest: URLRequest(
                          url: Uri.parse('https://flutter-header-echo.herokuapp.com/'),
                          headers: {
                            'Authorization': "Bearer " + bearerToken,
                            'test_header_1': 'flutter_test_header_value_1',
                            'test_header_2': 'flutter_test_header_value_2',
                            'test_header_3': 'flutter_test_header_value_3'
                          }),
                      initialOptions: options,
                      onWebViewCreated: (controller) {
                        webViewController = controller;
                      },
                      onLoadStart: (controller, url) {
                        log("onLoadStart $url");
                      },
                      onLoadStop: (controller, url) async {
                        log("onLoadStop $url");
                      },
                    ),
                  ],
                ),
              ),
            ]))
    );
  }
}

https://flutter-header-echo.herokuapp.com/ is a simple Heroku app that gives back all headers, such as also the custom ones.

Screenshot:

This is the response:

{
   "host":"flutter-header-echo.herokuapp.com",
   "connection":"close",
   "test_header_3":"flutter_test_header_value_3",
   "accept-encoding":"gzip, deflate, br",
   "test_header_1":"flutter_test_header_value_1",
   "accept":"text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
   "user-agent":"Mozilla/5.0 (iPhone; CPU iPhone OS 15_4_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148",
   "test_header_2":"flutter_test_header_value_2",
   "authorization":"Bearer MY_TOKEN_EXAMPLE",
   "accept-language":"it-IT,it;q=0.9",
   "x-request-id":"7b6a3e4b-2d37-4235-9daa-9eb1efe677e2",
   "x-forwarded-for":"5.90.134.218",
   "x-forwarded-proto":"https",
   "x-forwarded-port":"443",
   "via":"1.1 vegur",
   "connect-time":"0",
   "x-request-start":"1651096019434",
   "total-route-time":"0"
}

As you can see, I passed some custom headers and also an Authorization header with a fake bearer token. So, maybe you are having issues with the backend service you are calling and you should check for any logs about it there.

pconradjunior commented 2 years ago

@pichillilorenzo Thanks for your reply. When using Android everything works fine. So, I think it may be not a backend problem. I'm comparing your example to see if I missed something on iOS. I'm using Simulator to test the requests.

pichillilorenzo commented 2 years ago

My example works also on iOS Simulator. Try my code example to see if your authorization header is sent 👍

marinat commented 2 years ago

@pichillilorenzo yeah, our backend uses cookies for authorization :)

pichillilorenzo commented 2 years ago

@marinat then you should use CookieManager to set cookies.

pconradjunior commented 2 years ago

@marinat Same problem, different solution :D

pconradjunior commented 2 years ago

@pichillilorenzo Just updating: I used your example and my data with the same result as before. It keeps not working in iOS.

pichillilorenzo commented 2 years ago

@pconradjunior Did you try calling https://flutter-header-echo.herokuapp.com/ to test if your authorization header is present with the correct value? If it is not, then maybe you are not sending it right. Try to log your headers to see if you are sending it right to the URLRequest on iOS. If possible, make a testable example that reproduces your issue, otherwise, I cannot understand what is going on because to me it works correctly and headers are sent.

pconradjunior commented 2 years ago

@pichillilorenzo It seems the problem is on my destination. With your example it works well.

github-actions[bot] commented 4 weeks ago

This thread has been automatically locked since there has not been any recent activity after it was closed. If you are still experiencing a similar issue, please open a new bug and a minimal reproduction of the issue.