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了。

相关推荐
子春一27 分钟前
Flutter 2025 可访问性(Accessibility)工程体系:从合规达标到包容设计,打造人人可用的无障碍应用
javascript·flutter·microsoft
名字被你们想完了15 分钟前
Flutter 实现一个容器内部元素可平移、缩放和旋转等功能(七)
flutter
百***787518 分钟前
gpt-image-1.5极速接入指南:3步上手+图像核心能力解析+避坑手册
android·java·gpt
名字被你们想完了22 分钟前
Flutter 实现一个容器内部元素可平移、缩放和旋转等功能(八)
前端·flutter
renke336424 分钟前
Flutter 2025 测试工程体系:从单元测试到生产验证,构建高可靠、可交付、零回归的工程质量防线
flutter
kirk_wang29 分钟前
Flutter 鸿蒙项目 Android Studio 点击 Run 失败 ohpm 缺失
flutter·android studio·harmonyos
撩得Android一次心动31 分钟前
Android 四大组件——Service(服务)【基础篇2】
android·java·服务·四大组件·android 四大组件
是垚不是土1 小时前
MySQL8.0数据库GTID主从同步方案
android·网络·数据库·安全·adb
cnxy1881 小时前
MySQL地理空间数据完整使用指南
android·数据库·mysql
Swuagg1 小时前
Flutter 数据存储之 SharedPreferences 键值对存储
flutter·sp