我遇到此错误nsurlconnection完成了错误-Code -1002。我已将代码添加到我的info.plist中。有人知道为什么吗?
预先感谢
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
<key>NSExceptionMinimumTLSVersion</key>
<string>TLSv1.0</string>
</dict>
我认为这是关于应用程序的安全性。
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
您可以检查所有错误代码,并且含义以下链接
nserror by nshipster
错误的状态 - 代码-1002根据文档,请检查。,
https://developer.apple.com/documentation/foundation/1508628-URL_LOADING_SYSTEM_SYSTEM_ERROR_CODES/NSURLERLERRERRORRORNSUPPORTEDEREL?
请再次与Postman一起检查URL。
我遇到了同样的问题,但我的情况有所不同。这是生成错误的代码:
if let url = URL(string: imageUrl){
getDataFromUrl(url: url) { data, response, error in
DispatchQueue.main.async() {
if let imageFile = UIImage(data: data) {
self.createItem(forModule: module, title: title, description: description, date: date, image: imageFile, table: table)
} else {
self.createItem(forModule: module, title: title, description: description, date: date, image: nil, table: table)
}
}
}
} else { print("invalid url") }
private func getDataFromUrl(url: URL, completion: @escaping (Data?, URLResponse?, Error?) -> ()) {
URLSession.shared.dataTask(with: url) { data, response, error in
completion(data, response, error)
}.resume()
}
后来,我发现ImageUrl实际上并不是一个URL,它只是一个字符串,字面上是" false"。更糟糕的是,它没有在其他两种陈述中被抓住。
因此,我仅添加下面的后卫就解决了:
if let url = URL(string: imageUrl){
getDataFromUrl(url: url) { data, response, error in
guard let data = data, error == nil else {
self.createItem(forModule: module, title: title, description: description, date: date, image: nil, table: table)
return
}
DispatchQueue.main.async() {
if let imageFile = UIImage(data: data) {
self.createItem(forModule: module, title: title, description: description, date: date, image: imageFile, table: table)
} else {
self.createItem(forModule: module, title: title, description: description, date: date, image: nil, table: table)
}
}
}
}
现在,我可以调用CreateItem函数并保存我的项目,而无需成功。因此,如果您是从API获取的,请仔细检查您的URL。