user6078405
user6078405

Reputation:

NSURLConnection finished with error - code -1002 fix does not work

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>

Upvotes: 5

Views: 12529

Answers (3)

Wissa
Wissa

Reputation: 1592

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.

Upvotes: 0

siva kumar
siva kumar

Reputation: 2915

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.

Upvotes: 1

Harshal Bhavsar
Harshal Bhavsar

Reputation: 1673

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

Upvotes: 3

Related Questions