App Transport Security has blocked a cleartext HTTP (http://) resource load since it is insecure
I need to access a HTTP request through amazonaws.com, which works on android but Xcode gives this error above "App Transport Security has blocked..."
I found that I need to modify the info.plist to add exceptions: https://stackoverflow.com/questions/31254725/transport-security-has-blocked-a-cleartext-http
And I found an example here of how it's possible to modify info.plist with post-process build: https://forum.unity.com/threads/how-can-you-add-items-to-the-xcode-project-targets-info-plist-using-the-xcodeapi.330574/
But can't find any examples of how I would construct the nested Dictionaries to make it happen.
Answer by Treegemmer · Nov 12, 2017 at 07:11 AM
I couldn't find any documentation on how to use these methods, but did get it working through trial and error.
Hopefully this will help others in the future.
public class AllowHTTPSupportCodes : MonoBehaviour
{
[PostProcessBuild]
public static void OnPostprocessBuild(BuildTarget buildTarget, string pathToBuiltProject)
{
if (buildTarget == BuildTarget.iOS) {
// Get plist
string plistPath = pathToBuiltProject + "/Info.plist";
PlistDocument plist = new PlistDocument();
plist.ReadFromString(File.ReadAllText(plistPath));
PlistElementDict allowsDict = plist.root.CreateDict("NSAppTransportSecurity");
allowsDict.SetBoolean("NSAllowsArbitraryLoads", true);
PlistElementDict exceptionsDict = allowsDict.CreateDict("NSExceptionDomains");
PlistElementDict domainDict = exceptionsDict.CreateDict("amazonaws.com");
domainDict.SetBoolean("NSExceptionAllowsInsecureHTTPLoads", true);
domainDict.SetBoolean("NSIncludesSubdomains", true);
// Write to file
File.WriteAllText(plistPath, plist.WriteToString());
}
}
}
Thank you so much!
I've been fiddling for days trying to figure out why my app cannot connect to server. Turns out unsecured HTTP is the culprit. this piece of code solved my problem :D
Answer by dimitris_baud · Jan 28, 2020 at 10:54 AM
Thanks so much for posting the above. Below is the modern version of this script. Make sure you put it inside an Editor
folder and it will run automatically on each build.
using UnityEditor;
using UnityEditor.Build;
using System.IO;
using UnityEditor.Build.Reporting;
using UnityEditor.iOS.Xcode;
// adds an ATS exception domain to the Info.plist
public class InfoPlistUpdater : IPostprocessBuildWithReport
{
public int callbackOrder { get { return 0; } }
public void OnPostprocessBuild(BuildReport report)
{
BuildTarget buildTarget = report.summary.platform;
string pathToBuiltProject = report.summary.outputPath;
if (buildTarget == BuildTarget.iOS)
{
// Get plist
string plistPath = pathToBuiltProject + "/Info.plist";
PlistDocument plist = new PlistDocument();
plist.ReadFromString(File.ReadAllText(plistPath));
PlistElementDict allowsDict = plist.root.CreateDict("NSAppTransportSecurity");
allowsDict.SetBoolean("NSAllowsArbitraryLoads", true);
PlistElementDict exceptionsDict = allowsDict.CreateDict("NSExceptionDomains");
PlistElementDict domainDict = exceptionsDict.CreateDict("your.domain.to.make.an.exception.for");
domainDict.SetBoolean("NSExceptionAllowsInsecureHTTPLoads", true);
domainDict.SetBoolean("NSIncludesSubdomains", true);
// Write to file
File.WriteAllText(plistPath, plist.WriteToString());
}
}
}
Answer by Bunny83 · Aug 10, 2018 at 09:09 AM
Apple requires your App to use https and does not allow http connections for quite some time now. HTTPS is the recommended protocol in general when you deal with HTTP.. Disabling https is generally not recommended. If your server does not support https you may want to change the server. Disabling https should only be use in rare cases.
Your answer
Follow this Question
Related Questions
Unity iOS - Wont compile in Xcode 2 Answers
expected unqualified id on libiconv.2.tbd 0 Answers
xcode - linker command failed with exit code 1 8 Answers
Crazy high RAM usage on IOS 0 Answers
WWW class doesn't work well. 1 Answer