Disable Apple Transport Security Tutorial

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

Open Xcode and create a new Single View Application. For product name, use IOS9DisableATSTutorial and then fill out the Organization Name and Organization Identifier with your customary values. Enter Swift as Language and make sure only iPhone is selected in Devices.

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 -m SimpleHTTPServer

The simpleHTTPServer listen 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)!)

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


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. Name the Key "NSAppTransportSecurity" and choose Type Dictionary. Add another row inside the new key and name it "NSAllowsArbitraryLoads". 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 IOS9DisableATSTutorial at the ioscreator repository on Github.