12

I am using the following code to post an image to my server.

@IBAction func postButtonPressed(sender: UIButton) {
    let task = NSURLSession.sharedSession().dataTaskWithRequest(createRequest("http://xx.xx.xxx.xxx/xxxx/"), completionHandler: {
        data, response, error in
        println(NSString(data: data, encoding: NSUTF8StringEncoding))
    })
    task.resume()
}

where createRequest() creates the required NSURLRequest object.

This works fine when I use a simulator. The problem is that I am getting the following error when I run my app on an iPhone.

Error Domain=NSURLErrorDomain Code=-1001 "The operation couldn’t be completed. (NSURLErrorDomain error -1001.)" UserInfo=0x155e71f0 {NSErrorFailingURLKey=http://xx.xxx.xxx.xxx/xxxx/, NSErrorFailingURLStringKey=http://54.148.156.117/query/, NSUnderlyingError=0x155674d0 "The operation couldn’t be completed. (kCFErrorDomainCFNetwork error -1001.)"}

I have learned that this error is a timeout error. Restarting the app or phone didn't helped. Moreover I have tried posting an image to my server from a web browser and it worked with no problem.

What might be causing this timeout error?

EDIT: When I monitor my network activity, I realized the app sends 10 MB data even though the image that I am sending is 0.1 MB.

Berkan Ercan
  • 1,207
  • 3
  • 14
  • 31
  • Well, we may need to see the code that creates the data that you're sending along with the request. If the problem is that it's 100 times bigger than you're expecting, there's probably something wrong in that code. – Matt Gibson Nov 27 '14 at 08:17
  • I have found out that the problem occurs when I try to post an image that is taken by UIImagePicker. When I post an image that is previously added to images.xcassets folder, it works fine. Do you know any reason why would that happen? – Berkan Ercan Nov 28 '14 at 09:16
  • How *big* are these images? It would be helpful to see the code that's creating them. (Try logging the `length` property of the NSData object you're sending to the server...) – Matt Gibson Nov 28 '14 at 09:26
  • wow, an image taken by uiImagePicker is 14 MB whereas the image in my images.xcassets folder is 1.5 MB. Now things are becoming clearer – Berkan Ercan Nov 28 '14 at 12:02
  • ok, solved. The server accepts up to 2 MB. I will scale down my images to drop the image size down under 2 MB. – Berkan Ercan Nov 28 '14 at 13:22
  • 14MB? That's pretty big, even for a camera on one of the better iPhones. As well as scaling it, you may want to make sure you're encoding as JPEG with not-insane quality levels (you probably want to take the UIImage handed to you by the camera and use UIImageJPEGRepresentation on it, with a quality value of around 0.6, say.) – Matt Gibson Nov 28 '14 at 16:27

5 Answers5

12

Apparently my post request was getting a timeout error since I was sending an image that the server would find too large. I scaled down the images in order to make my images acceptable by the server.

As this post suggests, NSURLErrorDomain Code=-1001 error might be caused by many things including;

  • Server response time limit
  • Server rules about the incoming data
  • Authentication problems

Hope that helps to other people

Community
  • 1
  • 1
Berkan Ercan
  • 1,207
  • 3
  • 14
  • 31
  • If you have control of the server in question, it can be helpful to check the error logs—often you'll get something written there for an oversize upload, etc. – Matt Gibson Nov 28 '14 at 16:22
2

In my case issue was in 3rd party library - FirebasePerformance. Bug report URL: https://github.com/firebase/firebase-ios-sdk/issues/486

Vlad
  • 6,402
  • 1
  • 60
  • 74
2

It is late to reply but I recently face the error while running the new application assigned to me. The application is one of them whose URL Domains are blocked by the system administrator of my company.
I ask them to allow me the URL access after which the response came and the application run perfectly.
This may be not the actual solution for everyone but some of the developers may have the restricted environment can get help by this.

Er. Vihar
  • 1,495
  • 1
  • 15
  • 29
2

Well, in my case simply increasing the timeout of request solved the problem.

Ashutosh Shukla
  • 358
  • 5
  • 14
  • same here! in my case some mysql queries were failing but not all the time, only under certain conditions. doubling the timeout value allowed all the queries to finish – iRon111 Jul 07 '20 at 02:35
1

I fixed using headers with application/json and sending on the request:

let headers: HTTPHeaders = [
        .accept("application/json")
]
AF.request(url, method: .post, parameters: score, encoder: JSONParameterEncoder.default, headers: headers)
Gil Beyruth
  • 598
  • 1
  • 6
  • 11