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.23k stars 1.58k forks source link

Webview Proxy Controller not working #1760

Closed josuva1211 closed 6 days ago

josuva1211 commented 1 year ago

Environment

Technology Version
Flutter version 3.7.11
Plugin version 6.0.0-beta.24+1
Android version 19
iOS version
macOS version
Xcode version

Device information:

Description

I am trying to load a web app through local assets using inapp localhost server, but due to the assets making external API calls I am getting CORS issue. To overcome that, I have used latest feature of InAppWebView, Proxy Controller. After using that also I am getting the CORS issue. Please help what I am doing wrong in the below code Expected behavior:

Current behavior:

Steps to reproduce

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

final InAppLocalhostServer localhostServer = new InAppLocalhostServer();

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

  // start the localhost server
  await localhostServer.start();

  if (!kIsWeb && defaultTargetPlatform == TargetPlatform.android) {
    var proxyAvailable =
        await WebViewFeature.isFeatureSupported(WebViewFeature.PROXY_OVERRIDE);

    if (proxyAvailable) {
      ProxyController proxyController = ProxyController.instance();

      await proxyController.clearProxyOverride();
      await proxyController.setProxyOverride(
          settings: ProxySettings(proxyRules: [
        ProxyRule(
            url: "MY_API_URL")
      ], bypassRules: []));
    }
  }

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

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => new _MyAppState();
}

class _MyAppState extends State<MyApp> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('InAppLocalhostServer Example'),
      ),
      body: Container(
          child: Column(children: <Widget>[
        Expanded(
          child: InAppWebView(
            initialUrlRequest: URLRequest(
                url: WebUri("http://localhost:8080/asset/en/index.html")),
            onWebViewCreated: (controller) {},
            onLoadStart: (controller, url) {},
            onLoadStop: (controller, url) {},
          ),
        )
      ])),
    );
  }
}

Images

Stacktrace/Logcat

[InAppWebViewController] (android) WebView ID 0 calling "onReceivedError" using {request: {headers: {Origin: http://localhost:8080, Referer: http://localhost:8080/asset/en/index.html, Accept: /, Access-Control-Request-Method: GET, Access-Control-Request-Headers: requesttype,scope, Sec-Fetch-Mode: cors}, isRedirect: false, method: OPTIONS, hasGesture: false, isForMainFrame: false, url: MY_API_URL}, error: {description: net::ERR_PROXY_CONNECTION_FAILED, type: -5}}

github-actions[bot] commented 1 year ago

👋 @josuva1211

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!

github-actions[bot] commented 6 days ago

This issue is stale and has been automatically closed because it has been open for more than 365 days with no activity. Please reopen a new issue if you still have it.