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

相关推荐
爱勇宝10 小时前
我做了一个只用来搜歌词的小 App
android·前端·后端
壹方秘境12 小时前
我用Go语言开发了一个跨平台的HTTPS抓包和调试工具
前端·后端·ios
众少成多积小致巨14 小时前
JNI (Java Native Interface) 技术手册中文参考指南
android·java·c++
Coffeeee20 小时前
如何使用Glide和Coil加载WebP动图
android·kotlin·glide
Kapaseker21 小时前
5 分钟搞懂 Kotlin DSL
android·kotlin
恋猫de小郭1 天前
AI Agent 开发究竟是啥?如何用 AI 开发 Agent ?深入浅出给你一套概念
android·前端·ai编程
黄林晴1 天前
Android 17 正式发布!target 37 一大批旧代码直接不能用了
android
Carson带你学Android1 天前
Android 17 正式发布:AI 终于成了系统能力
android·前端·ai编程
三少爷的鞋1 天前
当 UseCase 开始长期监听,它可能已经不是 UseCase 了
android
恋猫de小郭1 天前
Android 限制侧载新进展,谷歌联合国内厂商推验证计划
android·前端·flutter