The Miotz The Miotz - 1 month ago 11
Swift Question

iOS swift: App Transport Security has blocked a cleartext HTTP (http://) resource

Evening, I'm working on an app based on the SWAPI (Star Wars api: https://swapi.co/documentation)

And I got the ATS Error:

App Transport Security has blocked a cleartext HTTP (http://) resource load since it is insecure. Temporary exceptions can be configured via your app's Info.plist file.


I can't understand the reason. My
baseURL
is on https format

struct NetworkManager {
let baseURL = "https://swapi.co/api/"
let session = URLSession(configuration: .default)

func fetchEndpoint(endpoint: Endpoint, completion: @escaping (_ response: Result) -> Void) {
self.fetchURL(url: baseURL + endpoint.URL(), completion: completion)
}

func fetchURL(url: String, completion: @escaping (_ response: Result) -> Void) {
let url = URL(string: url)!

let request = URLRequest(url: url)

let task = session.dataTask(with: request) { data, response, error in
if error != nil {
completion(.Failure(error))
} else {
if let data = data {
if let json = try? JSONSerialization.jsonObject(with: data, options: []) {

OperationQueue.main.addOperation({
switch json {
case let objectResponse as JSONArray: completion(.Success(objectResponse as AnyObject?))
case let objectResponse as JSONDict: completion(.Success(objectResponse as AnyObject?))
default: break
}
})
}
}
}
}

task.resume()
}
}


Please give me and hint!!
I'm just a newbie, and I'm guessing that the SWAPI supports only the http protocol.

Answer

Seems to be that you are right, that SWAPI supports only the http protocol.

To support also an unsecure connection do this:

  1. Open the info.plist file
  2. Add the Key called App Transport Security Settings as Dictionary (Dictionary should be the default type)
  3. Add the Subkey called Allow Arbitrary Loads as Boolean (Boolean should be the default type). Set it to YES

See also the Screenshot:

enter image description here