NSURLConnection times out after reopening app

Hello,


In my app I use a NSURLConnection to upload some content. Everything works fine except when I reopen the app after like one hour.


On my personal wifi, everything is fine, but when I connect using my operator data plan (I never observed any issue like this on other apps), the first NSURLConnection times out. If I try again, the operation succeeds.


I use a standard NSURLConnection.

NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:delegate];


Using

[NSURLConnection sendAsynchronousRequest:request...]

also leads to a timeout.


On another thread on the Apple developer forum somebody suggested to run a packet trace, which I did. The request does not seem to be sent.

I'm a bit out of ideas, do you have one ?


Thanks a lot!

I also reproduced the bug while doing this:

  • reopen the app after a while
  • make a request on wifi that works
  • switch to 4G, try to make the same request => fail


I'm really not sure about what's going on here, any workaround?

I'm wondering if it could come from the request itself. Here it is:


NSMutableURLRequest *request = [Services postRequest];
   
// set URL
NSURL *url = [NSURL URLWithString:SERVICE_URL];
[request setURL:url];
   
NSString *boundary = [Services generateBoundary];
   
// set Content-Type in HTTP header
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary];
[request setValue:contentType forHTTPHeaderField: @"Content-Type"];
   
// post body
NSMutableData *body = [NSMutableData data];
   
// JSON parameters
NSError* JSONSerializationError;
NSData *json = [NSJSONSerialization dataWithJSONObject:defaultParameters options:kNilOptions error:&JSONSerializationError];
   
[body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"\r\n\r\n", @"json_param"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"%@\r\n", [[NSString alloc] initWithData:json encoding:NSUTF8StringEncoding]] dataUsingEncoding:NSUTF8StringEncoding]];
   

// add image data
if (imageData) {
   
[body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
   
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"; filename=\"image.jpg\"\r\n", @"uploaded_file"] dataUsingEncoding:NSUTF8StringEncoding]];
   
[body appendData:[@"Content-Type: image/jpeg\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
   
[body appendData:imageData];
   
[body appendData:[[NSString stringWithFormat:@"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
}
   
[body appendData:[[NSString stringWithFormat:@"--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];

There doesn't seem to be anything obviously fishy with that request composition. Can you show us what your asynchronous method call looks like?

Hello Laurent,


it seem our app CoachGuitar has the exact same issue.. we are going crazy about it.

We tried to put KeepAlive= Off server side but with no better results.


Do you still have the issue, or did you found any work around ?


Appreciate any help on this,


Best regards,

Julien

NSURLConnection times out after reopening app
 
 
Q