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: ios - Transport security has blocked a cleartext HTTP - Stack Overflow

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.

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());
		}
	}
}

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());
        }
    }
}

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.