Disable Apple Transport Security iOS Tutorial

Apple Transport Security(ATS) requires network requests to be sent over a secure connection. Apple Transport Security is enabled by default, so when it is still needed to make HTTP requests to an external source out of your control, ATS needs to be disabled. In this tutorial a request to a local server will be made and ATS will be disabled to make the connection. This tutorial is made with Xcode 10 and built for iOS 12.

Open Xcode and create a new Single View App.

For product name, use IOSDisableATSTutorial and then fill out the Organization Name and Organization Identifier with your customary values. Enter Swift as Language and choose Next.

In Mac OS X, the Python language including a web server module is installed by default. Open a terminal and enter the following commands to start the web server

mkdir web
cd web
echo "testing Web Server" > index.html
python http.server

The http.server listens at port 8000 by default. Test this connection by opening a new terminal to enter the following command

wget -qO- localhost:8000

This will give the output "testing Web Server" to indicate the Web Server is serving requests. While leaving the terminal open go back to the Xcode Project and navigate to the ViewController.swift file and add the following method.

func webRequest() {
    let url = NSURL(string: "http://localhost:8000")
    let task = NSURLSession.sharedSession().dataTaskWithURL(url!) {
        (data, response, error) in
        if let dataReturned = data {
           print(NSString(data: dataReturned, encoding: NSUTF8StringEncoding)!)
        }
    }
        
    task.resume()
    }

This method creates an HTTP GET requests at the specified URL and prints the result in the console. Add the following line at the end of the viewDidLoad method

webRequest()

Build and Run the program. This will result in the following error in the console.

To avoid the error Apple Transport Security needs to be disabled. Go to the Info.plist. Right-click on  the Information Property List at the top of the file and choose Add Row. Select the Key "App Transport Security Settings" and choose Type Dictionary. Add another row inside the new key and select "Allow Arbitrary Loads". The type will be Boolean with a value of YES. The info.plist should now look like this.

Build and Run the project again. Now the web request will succeed.

You can download the source code of the IOSDisableATSTutorial at the ioscreator repository on Github.