iOS swift:App Transport Security阻止了一个明文HTTP(http://)资源

And*_*tto 3 api https ios swift

晚上,我正在开发一个基于SWAPI的应用程序(星球大战api:https://swapi.co/documentation )

我得到了ATS错误: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.

我无法理解原因.我baseURLhttps格式

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()
    }
}
Run Code Online (Sandbox Code Playgroud)

请给我并提示!! 我只是一个新手,我猜测SWAPI只支持http协议.

ron*_*ory 7

似乎你是对的,SWAPI只支持http协议.

为了支持不安全的连接,请执行以下操作:

  1. 打开info.plist文件
  2. 添加名为App Transport Security Settingsas 的键Dictionary(字典应为默认类型)
  3. 添加名为Allow Arbitrary Loadsas 的子键Boolean(布尔值应为默认类型).设置为YES

另见屏幕截图:

在此输入图像描述