晚上好,我正在开发一个基于 SWAPI 的应用程序(星球大战 API:https://swapi.co/documentation)
我收到了 ATS 错误:
应用程序传输安全性已阻止明文 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 协议。