我一直在使用
flutter_inappwebview
包,它是 flutter webview 的包装器。
我主要在 iOS 模拟器上开发应用程序,但将 Flutter 升级到最新版本后,我想在 Android 模拟器上测试它,但它不起作用。在升级之前,它曾经在 Android 和 iOS 上运行。
它在 iOS 上仍然有效,但仅在 Android 上当我尝试使用 webview 打开网页时才会出现 ERR_INVALID_RESPONSE 错误。
发出 HTTP 请求工作正常,甚至来自
flutter_inappwebview
的无头浏览器也工作正常。
只有 InAppWebView 不起作用。
请帮忙。
机器
苹果M3 Pro
14.4(23E214)
颤振版
Flutter 3.19.3 • 通道稳定 • https://github.com/flutter/flutter.git 框架 • 修订版 ba39319843(2 周前) • 2024-03-07 15:22:21 -0600 引擎 • 修订版 2e4ba9c6fb 工具 • Dart 3.3.1 • DevTools 2.31.1
Flutter 中 InAppWebView 的设置
InAppWebView(
initialSettings: InAppWebViewSettings(
useShouldInterceptRequest: true,
thirdPartyCookiesEnabled: true,
iframeAllowFullscreen: false,
userAgent: userAgent,
javaScriptCanOpenWindowsAutomatically: false,
useShouldOverrideUrlLoading: true,
mediaPlaybackRequiresUserGesture: true,
allowsPictureInPictureMediaPlayback: false,
allowsLinkPreview: false,
allowsBackForwardNavigationGestures: true,
),
)
pubspec.yaml
environment:
sdk: '>=3.1.2 <4.0.0'
dependencies:
collection: ^1.17.2
flutter:
sdk: flutter
flutter_inappwebview: ^6.0.0
// .. and so on
android/app/scr/main/AndroidManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:label="myapp"
android:name="${applicationName}"
android:usesCleartextTraffic="true"
android:icon="@mipmap/ic_launcher">
<activity
android:name=".MainActivity"
android:exported="true"
android:launchMode="singleTop"
android:theme="@style/LaunchTheme"
android:configChanges="orientation|keyboardHidden|keyboard|screenSize|smallestScreenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode"
android:hardwareAccelerated="true"
android:windowSoftInputMode="adjustResize">
<!-- Specifies an Android theme to apply to this Activity as soon as
the Android process has started. This theme is visible to the user
while the Flutter UI initializes. After that, this theme continues
to determine the Window background behind the Flutter UI. -->
<meta-data
android:name="io.flutter.embedding.android.NormalTheme"
android:resource="@style/NormalTheme"
/>
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<!-- Don't delete the meta-data below.
This is used by the Flutter tool to generate GeneratedPluginRegistrant.java -->
<meta-data
android:name="flutterEmbedding"
android:value="2" />
</application>
<!-- Required to query activities that can process text, see:
https://developer.android.com/training/package-visibility?hl=en and
https://developer.android.com/reference/android/content/Intent#ACTION_PROCESS_TEXT.
In particular, this is used by the Flutter engine in io.flutter.plugin.text.ProcessTextPlugin. -->
<queries>
<intent>
<action android:name="android.intent.action.PROCESS_TEXT"/>
<data android:mimeType="text/plain"/>
</intent>
</queries>
</manifest>
android/app/build.gradle
plugins {
id "com.android.application"
id "kotlin-android"
id "dev.flutter.flutter-gradle-plugin"
}
def localProperties = new Properties()
def localPropertiesFile = rootProject.file('local.properties')
if (localPropertiesFile.exists()) {
localPropertiesFile.withReader('UTF-8') { reader ->
localProperties.load(reader)
}
}
def flutterVersionCode = localProperties.getProperty('flutter.versionCode')
if (flutterVersionCode == null) {
flutterVersionCode = '1'
}
def flutterVersionName = localProperties.getProperty('flutter.versionName')
if (flutterVersionName == null) {
flutterVersionName = '1.0'
}
android {
namespace "com.example.myapp"
compileSdk flutter.compileSdkVersion
ndkVersion flutter.ndkVersion
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = '1.8'
}
sourceSets {
main.java.srcDirs += 'src/main/kotlin'
}
defaultConfig {
applicationId "com.example.myapp"
minSdkVersion 19
targetSdkVersion 34
versionCode flutterVersionCode.toInteger()
versionName flutterVersionName
multiDexEnabled true
}
buildTypes {
release {
signingConfig signingConfigs.debug
}
}
}
flutter {
source '../..'
}
dependencies {}
试过
flutter clean
/android
文件夹,然后运行 flutter create
8.2.2
升级到 7.4.2
,然后将其恢复,因为它无法编译。
在这里。果然找到了答案。这是由于 webview 和网站在每个操作系统上的行为不同。 ShouldIntercept 在 Android 上阻止了某些内容,但在 iOS 上却没有,所以我删除了该代码,现在它可以工作了