NSURLConnection finished with error - code -1002 f

2019-07-14 03:25发布

I am getting this error NSURLConnection finished with error - code -1002. I have added the code below into my info.plist. Does anyone know why?

Thanks in advance

<key>NSAppTransportSecurity</key>
<dict>
    <key>NSAllowsArbitraryLoads</key>
    <true/>
    <key>NSExceptionMinimumTLSVersion</key>
    <string>TLSv1.0</string>
</dict>

标签: ios iphone swift
3条回答
爷的心禁止访问
2楼-- · 2019-07-14 03:46

I had the same issue but my case was different. Here is the code that generated the error:

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

Later I found out that imageUrl was not actually a url at all it was just a string, literally "false" thats it. and what made it worse that it was not caught in both else statements.

so I solved it by just adding the guard below:

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

Now I can call the createItem function and save my item without image successfully. So check your URLs carefully specially if you're getting them from an API.

查看更多
Juvenile、少年°
3楼-- · 2019-07-14 03:48

I think it is about App Transport Security.Because your url is not https.Try to change like this in the info.plist file

<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>

You can check all error codes and there meanings on following link

NSError by NSHipster

查看更多
男人必须洒脱
4楼-- · 2019-07-14 03:52

The status of error - code -1002 as per the documentation Please check. ,

https://developer.apple.com/documentation/foundation/1508628-url_loading_system_error_codes/nsurlerrorunsupportedurl?language=objc

please check the url once again with postman.

查看更多
登录 后发表回答