Skip to content

[webview_flutter] Add option to allow mixed-mode content (http in https page) #43595

Closed
flutter/packages
#9586

Description

@caoxingyu

Internal: b/292548371

webview_flutter how to load https and http mixed content?

Activity

  1. danybuoy commented on Nov 7, 2019

    @danybuoy

    I got it working by adding this line in the WebviewManager.java inside the plugin:

    webView.getSettings().setMixedContentMode(WebSettings.MIXED_CONTENT_COMPATIBILITY_MODE);

    I'm working on a small PR to give us the option to choose how mixed content is enabled

  2. dotcink commented on Mar 15, 2020

    @dotcink

    @amirh What's the meaning of customer: dream (g3) label please?
    Cannot find a description at https://github.com/flutter/flutter/labels?q=dream

  3. songfei commented on Jul 29, 2020

    @songfei
  4. added
    P1High-priority issues at the top of the work list
    on Jul 29, 2020
  5. liraku commented on Dec 14, 2020

    @liraku
  6. godfatherdemon commented on Jan 15, 2021

    @godfatherdemon
  7. hanifmhd commented on Mar 16, 2022

    @hanifmhd
  8. iusn commented on Sep 7, 2022

    @iusn
  9. removed
    P1High-priority issues at the top of the work list
    on Jan 21, 2023
  10. 2 remaining items

  11. changed the title [-]webview_flutter how to load https and http mixed content?[/-] [+][webview_flutter] Add option to allow mixed-mode content (http in https page)[/+] on May 21, 2023
  12. wyhAcc commented on Dec 5, 2023

    @wyhAcc
  13. heyjunpenn commented on May 30, 2025

    @heyjunpenn

    在flutter webview 4.x 中,可以通过下面的方式实现https和http混合加载,在android目录下的MainActivity中添加下面的代码:

    import android.content.Intent
    import android.os.Build
    import android.os.Bundle
    import android.util.Log
    import android.webkit.WebSettings
    import androidx.annotation.NonNull
    import io.flutter.embedding.android.FlutterActivity
    import io.flutter.plugin.common.MethodCall
    import io.flutter.plugin.common.MethodChannel
    import io.flutter.plugin.common.MethodChannel.MethodCallHandler
    import io.flutter.plugins.GeneratedPluginRegistrant
    import io.flutter.embedding.engine.FlutterEngine
    import io.flutter.plugins.webviewflutter.WebViewFlutterAndroidExternalApi
    import java.util.HashMap
    
    abstract class BaseMainActivity: FlutterActivity(), MethodCallHandler {
        private val channelName = "nocturne.native.channel/helper"
        private var methodChannel: MethodChannel? = null
    
        override fun configureFlutterEngine(@NonNull flutterEngine: FlutterEngine) {
            GeneratedPluginRegistrant.registerWith(flutterEngine)
            methodChannel = MethodChannel(flutterEngine.dartExecutor, channelName)
            methodChannel?.setMethodCallHandler(this)
        }
    
        override fun onMethodCall(call: MethodCall, result: MethodChannel.Result) {
            when (call.method) {
                "setMixedContentMode" -> {
                    Log.d("MainActivity", "收到setMixedContentMode调用")
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                        val viewId = call.argument<Int>("viewId")
                        Log.d("MainActivity", "viewId: $viewId")
                        
                        try {
                            val webView = WebViewFlutterAndroidExternalApi.getWebView(flutterEngine!!, viewId!!.toLong())
                            if (webView != null) {
                                webView.settings.setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW)
                                Log.d("MainActivity", "设置混合内容模式完成")
                                result.success(true)
                            } else {
                                Log.d("MainActivity", "未找到WebView")
                            }
                        } catch (e: Exception) {
                            Log.e("MainActivity", "获取WebView失败: ${e.message}")
                        }
                    } else {
                        Log.d("MainActivity", "Android版本过低,不支持设置混合内容模式")
                    }
                    result.success(false)
                }
                else -> result.notImplemented()
            }
        }
    }
    

    然后在webviewcontroller初始化后,通过channel修改调用setMixedContentMode:

    if (webViewController.platform is AndroidWebViewController) {
          final channel = MethodChannel('nocturne.native.channel/helper');
          final setMixedRes =
              await channel.invokeMethod('setMixedContentMode', {'viewId': viewId});
        }
    
    
  14. github-actions commented on Jul 29, 2025

    @github-actions

    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, including the output of flutter doctor -v and a minimal reproduction of the issue.

  15. locked as resolved and limited conversation to collaborators on Jul 29, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    P2Important issues not at the top of the work listc: new featureNothing broken; request for a new capabilitycustomer: crowdAffects or could affect many people, though not necessarily a specific customer.customer: dream (g3)p: webviewThe WebView pluginpackageflutter/packages repository. See also p: labels.team-ecosystemOwned by Ecosystem teamtriaged-ecosystemTriaged by Ecosystem team

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions