当前位置 : 主页 > 手机开发 > ios >

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

来源:互联网 收集:自由互联 发布时间:2021-06-11
晚上,我正在开发基于SWAPI的应用程序(星球大战api: https://swapi.co/documentation) 我得到了ATS错误:App Transport Security阻止了一个明文HTTP(http://)资源加载,因为它不安全.可以通过应用程序的
晚上,我正在开发基于SWAPI的应用程序(星球大战api: https://swapi.co/documentation)

我得到了ATS错误:App Transport Security阻止了一个明文HTTP(http://)资源加载,因为它不安全.可以通过应用程序的Info.plist文件配置临时例外.

我无法理解原因.我的baseURL是https格式

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

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

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

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

>打开info.plist文件
>将名为App Transport Security Settings的密钥添加为Dictionary(Dictionary应为默认类型)
>将名为Allow Arbitrary Loads的子项添加为布尔值(布尔值应为默认类型).将其设置为YES

另见屏幕截图:

enter image description here

网友评论