Flutter配置Android和IOS允许http访问

默认情况下,Android和IOS只支持对https的访问,如果需要访问不安全的连接,也就是http,需要做以下配置。

Android

在res目录下的xml目录中(如果不存在,先创建xml目录),创建一个xml文件network_security_config.xml

xml 复制代码
<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
    <base-config cleartextTrafficPermitted="true" />
</network-security-config>

然后在manifest.xml中,application标签上添加android:networkSecurityConfig,值为@xml/network_security_config

同时添加meta-data,name的值是io.flutter.network-policy

xml 复制代码
    <application
        android:name="${applicationName}"
        android:icon="@mipmap/ic_launcher"
        android:label="flutter_app"
        android:networkSecurityConfig="@xml/network_security_config">
    	<activity>
    		.....
    	</activity>
    	<meta-data android:name="io.flutter.network-policy"
             android:resource="@xml/network_security_config"/>
    </application>

IOS

如果不允许http,在IOS的webview加载网页时,如果img标签的图片url是http,可能会无法显示,出现以下的错误,图片直接变成一个问号。

以下配置只选择一种方式即可。

配置1

打开Info.plist,添加以下内容

xml 复制代码
<key>NSAppTransportSecurity</key>
<dict>
    <key>NSAllowsArbitraryLoads</key>
    <true/>
    <key>NSAllowsArbitraryLoadsInWebContent</key>
    <true/>
</dict>

配置2

在Xcode中打开Info.plist

在右边选中Information Property List,右键Add Row,添加一行新的属性。

选择App Transport Security Settings,然后添加2个子item,NSAllowsArbitraryLoads NSAllowsArbitraryLoadsInWebContent,value都选择YES,也就是True了。

相关推荐
恋猫de小郭6 分钟前
Android 将强制应用使用主题图标,你怎么看?
android·前端·flutter
jctech16 分钟前
这才是2025年的插件化!ComboLite 2.0:为Compose开发者带来极致“爽”感
android·开源
用户20187928316717 分钟前
为何Handler的postDelayed不适合精准定时任务?
android
叽哥31 分钟前
Kotlin学习第 8 课:Kotlin 进阶特性:简化代码与提升效率
android·java·kotlin
Cui晨32 分钟前
Android RecyclerView展示List<View> Adapter的数据源使用View
android
氦客32 分钟前
Android Doze低电耗休眠模式 与 WorkManager
android·suspend·休眠模式·workmanager·doze·低功耗模式·state_doze
玲珑Felone1 小时前
从flutter源码看其渲染机制
android·flutter
诺诺Okami1 小时前
Android Framework-Launcher-数据的加载
android
诺诺Okami1 小时前
Android Framework-Launcher-Partner
android
2501_915918411 小时前
iOS 上架全流程指南 iOS 应用发布步骤、App Store 上架流程、uni-app 打包上传 ipa 与审核实战经验分享
android·ios·小程序·uni-app·cocoa·iphone·webview