Unity项目iOS构建后URI Scheme自动从http转为https问题排查
解决Unity iOS构建后Azure Table Storage自动切换到HTTPS的问题
我之前做类似项目时也踩过iOS ATS和Azure SDK的坑,结合你的情况,除了勾选Allow downloads over HTTP,还有几个关键配置你可能遗漏了:
配置iOS ATS(App Transport Security)例外
iOS默认的ATS机制会强制应用使用HTTPS通信,哪怕Unity里开了HTTP允许,也得在Info.plist里明确加例外规则。你可以选两种方式设置:- 全局允许HTTP(适合测试阶段):在Info.plist中添加
NSAppTransportSecurity字典,再添加NSAllowsArbitraryLoads并设为YES。 - 针对Azure Storage域名的例外(更安全):在
NSAppTransportSecurity下加NSExceptionDomains字典,给你的存储账户域名(比如youraccount.table.core.windows.net)设置NSIncludesSubdomains为YES、NSAllowsArbitraryLoads为YES。
你可以用Unity的PostProcessBuild脚本自动注入配置,避免手动修改plist:
using UnityEngine; using UnityEditor; using UnityEditor.Callbacks; using System.IO; using System.Xml; public class iOSPostBuildHandler { [PostProcessBuild(100)] public static void OnPostProcessBuild(BuildTarget target, string pathToBuiltProject) { if (target != BuildTarget.iOS) return; string plistPath = Path.Combine(pathToBuiltProject, "Info.plist"); XmlDocument doc = new XmlDocument(); doc.Load(plistPath); // 添加ATS配置 XmlElement root = doc.DocumentElement; XmlElement atsElement = doc.CreateElement("NSAppTransportSecurity"); XmlElement allowsArbitraryLoads = doc.CreateElement("NSAllowsArbitraryLoads"); allowsArbitraryLoads.InnerText = "YES"; atsElement.AppendChild(allowsArbitraryLoads); root.AppendChild(atsElement); doc.Save(plistPath); } }- 全局允许HTTP(适合测试阶段):在Info.plist中添加
强制Azure Storage SDK使用HTTP端点
部分版本的Azure Storage SDK会自动把HTTP升级为HTTPS,你需要在初始化客户端时手动指定HTTP的连接字符串或端点:// 替换连接字符串中的https为http string connectionString = "你的Azure存储连接字符串"; connectionString = connectionString.Replace("https://", "http://"); CloudStorageAccount storageAccount = CloudStorageAccount.Parse(connectionString); CloudTableClient tableClient = storageAccount.CreateCloudTableClient();或者直接构造HTTP的端点URI:
Uri tableEndpoint = new Uri("http://youraccount.table.core.windows.net/"); StorageCredentials credentials = new StorageCredentials("youraccount", "yourkey"); CloudTableClient tableClient = new CloudTableClient(tableEndpoint, credentials);检查Unity Player Settings的其他网络配置
确认Player Settings里的Internet Access设置为Require或Require and Use,避免网络权限不足导致协议被强制切换。另外,如果你用的是较新的Unity版本,检查是否有其他HTTP限制选项(比如仅允许localhost用HTTP的设置)。验证构建后的Info.plist
有时候Unity的设置不会正确同步到iOS的Info.plist里,你可以找到构建后的Xcode项目,打开Info.plist文件,确认NSAppTransportSecurity相关配置是否存在,避免配置丢失。
内容的提问来源于stack exchange,提问作者tillmannen




