I am developing an video sharing app. When I am trying to upload videos larger than 10MB NSURLConnection get lost and I got error NSURLErrorDomain Code=-1005 "The network connection was lost." . Video, less than 10 MB are uploading.Can resolution of video be an issue here?? Resolution of my video is 1280*720 .
Can anyone help me to understand issue here. This is my code for connection
NSString *urlString = [[NSString alloc]initWithString:[NSString stringWithFormat:WEBSERVICE_URL]];
urlString=[urlString stringByAddingPercentEscapesUsingEncoding:
NSUTF8StringEncoding];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:urlString]];
[request setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];
[request setHTTPShouldHandleCookies:NO];
[request setTimeoutInterval:100];
[request setHTTPMethod:#"POST"];
This is not duplicate of NSURLConnection GET request returns -1005, "the network connection was lost" because
I am not using third party library for connection and not using simulator. I had tried every thing from that answer.
You might be hitting various timeouts—either the NSURLConnection timeout or a timeout on the server side. iOS may ignore the timeout value you specify, BTW. Or you could just be losing the network connection because your device decided to power down the cellular or Wi-Fi hardware to conserve power. Hard to say which. Or your Wi-Fi connection could have dropped because it missed too many beacons from the access point for some reason. Or....
My advice would be to upload large files a piece at a time, and then assemble the pieces on the server. That way, when (not if) the connection fails, you can re-upload the chunk that only partially transferred, and then continue uploading additional chunks until you've uploaded the whole thing.
See also the Content-Range HTTP header. Obviously, this requires your server-side code to support that functionality, but it is worth the extra effort. :-)
BTW, you forgot to set the Content-Type header.
Related
I have been struggling with an issue where NSURLConnection calls instantly fail. The device needs to be rebooted entirely or Flight Mode needs to be toggled on/off to resolve the problem. Restarting the app (swipe up) alone does not help.
Some facts:
-All URLs are HTTPS, TLS 1.2 compatible with Forward Secrecy. There are no issues with ATS and iOS 9. The error has been present since iOS 7 and remains on 9.2.
-No third party frameworks are used by the app. I use only native NSURLConnection calls that always work, except for when this odd situation occurs.
-No infrastructure/network issues - other devices on same networks (same WiFi for instance) work in the same app at the same time. Going to/from 3G/Wifi makes no difference.
-I always implement willCacheResponse to return nil.
-The service is hosted on AWS Elastic Beanstalk, so some suggested that it might be a DNS caching issue in case of IP address changes - this seems unlikely to me and should trigger multiple errors at once on different devices, which I have never seen.
-The method called is didFailWithError, instantaneously, as if there were no Internet connection on the device at all - all other apps work, however.
-The website that hosts the API used by the app can be browsed with no problems at all times. The website actually makes the same requests to fetch data.
The error code returned is -1003, kCFURLErrorCannotFindHost. I've been following a thread on Git dealing with the same issue to no avail. https://github.com/AFNetworking/AFNetworking/issues/967
I tried using NSURLRequestReloadIgnoringCacheData for all my requests, but that did not help.
With this information, will anyone care to venture a guess what I might be doing wrong? I added the bounty because I have no idea how to approach this problem - especially because it's so inconsistent. And it is definitely not a legitimate error (that is, that the domain could not be found), as the service is operating fine while this happens on random clients.
I create my request with a static method that looks like this. It's been stripped of some non-public info, but basically it just performs a POST request with JSON data. [Controller getSQLHost] just returns a URL - the base domain.
+(NSURLConnection*)initiatePOSTwithJSONDictionary:(NSDictionary*)dictionary toURL:(NSString*)urllocation withDelegate:delegate {
NSMutableDictionary *connectionDictionary = [[NSMutableDictionary alloc] init];
if (dictionary) {
[connectionDictionary setObject:dictionary forKey:#"input"];
}
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:connectionDictionary options:kNilOptions error:nil];
NSURL *url = [NSURL URLWithString:[[Controller getSQLHost] stringByAppendingString:urllocation]];
NSString *postLength = [NSString stringWithFormat:#"%i", (int)[jsonData length]];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:30.0];
[request setHTTPMethod:#"POST"];
[request setValue:postLength forHTTPHeaderField:#"Content-Length"];
[request setHTTPBody:jsonData];
return [[NSURLConnection alloc] initWithRequest:request delegate:delegate];
}
Does you delegate implement connectionShouldUseCredentialStorage ? (or respond with YES)
I think the device's keychain is used when this method returns yes which may explain the persisting failure beyond the life time of the running application and why rebooting or otherwise resetting network connectivity "fixes" it. If an authentication failure has been recognized once, it may linger in the key chain for a little while which would then respond immediately without actually going to the server.
What would cause the authentication to register as a failure in the keychain in the first place may depend on a variety of factors. It could be as simple as a typo in the saved password or more convoluted such as some certificate expiration preventing the SSL layer from establishing a secure link.
You're creating NSURLConnections on the current runloop. How many are active at any one time? Have you considered using an NSOperationQueue so that you don't get bitten by load bugs?
Is your delegate thread-safe? If not, that could explain the sporadic-ness of the bug.
You say you don't see the problem often, but others do. Can you borrow their devices and maybe even them and their usage patterns and thus get to see the problem more often?
Before today's iOS 8.3 update I had my code working right, but after updating server started rejecting requests as it could not find JSON data.
I found that iOS is sending a wrong application/x-www-form-urlencoded text (not properly encoded as it seems a JSON object):
This is what I expected to be sent (and what was sent on 8.2):
As I said, this only happens on iOS 8.3 (I just tried on iOS simulator with 8.2 and it works).
I think the problem is in one of the classes that appear on these lines:
NSData *bodyData = [NSJSONSerialization dataWithJSONObject:requestDict options:0 error:nil];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
request.HTTPBody = bodyData;
I checked Apple documentation and none appears as modified recently.
Is someone suffering the same or knows what can lead to this?
Thanks.
I also have encountered this problem in one application.
It looks like it could be a bug introduced in iOS 8.3. From the official Framework Reference, it is said of the NSURLSessionConfiguration's HTTPAdditionalHeaders property:
This property specifies additional headers that are added to all tasks within sessions based on this configuration. For example, you might set the User-Agent header so that it is automatically included in every request your app makes through sessions based on this configuration.
From what I understand, a Content-Type set in this configuration should be automatically included in every request made through this session, which is no longer the case since 8.3. I've filed a radar.
UPDATE: The radar has been set as 'duplicate' of another so it's more likely a bug.
As Luca pointed out, the work around is to set the Content-Type directly on the request:
[request setValue:#"application/json" forHTTPHeaderField:#"Content-Type"];
Source: https://developer.apple.com/library/mac/documentation/Foundation/Reference/NSURLSessionConfiguration_class/#//apple_ref/occ/instp/NSURLSessionConfiguration/HTTPAdditionalHeaders
I put the comment as answer here:
Try to indicate the http method like
[request setHTTPMethod:#"POST"]
and / or, the request length
[request setValue:postLength forHTTPHeaderField:#"Content-Length"];
and / or the content type
[request setValue:#"application/<YOUR CONTENT TYPE>"forHTTPHeaderField:#"Content-Type"];
It seems that iOS 8.3 introduces this. At my site we got found the problem that the (json)body was correctly not set inside the request.
When debugging try to debug the server-side also
In my app I use a NSURLConnection to upload some content.
Everything works fine except when I reopen the app after like one hour.
The NSURLConnection will timeout every time at the first try. After that the app works fine.
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:delegate];
[connection start];
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 get this error when I try running the app on a device. I do not get this error when I run it on the simulator and the post request works fine. This is code snippet I have:
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLCacheStorageNotAllowed timeoutInterval:300.f];
[request setHTTPMethod:#"POST"];
NSData* data = [paramString dataUsingEncoding:NSUTF8StringEncoding];
[request setHTTPBody:data];
[request setValue:#"application/x-www-form-urlencoded" forHTTPHeaderField:#"Content-Type"];
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
[connection start];
Any help is appreciated. Thanks
With Xcode 11.2.1, working on a macOS project, I had the same issue.
The fix here was:
Click on project in project explorer
Select the Signing & Capabilities tab
Check "Network: Outgoing connections (client)" checkbox in the AppSandbox section
This error would typically suggest a DNS related issue. Check the DNS settings on your device and confirm you can browse the internet using Safari or another browser (double check by visiting a site you have not visited before to ensure it is not cached).
If there is a url you can GET (instead of POST) on the same server, try visiting it directly in Safari on the device.
The simulator uses your computers network connection, so I recommend checking the System Preferences > Network > Advanced > Proxies > Automatic Proxy Configuration : You must disable Automatic conf.
Requests don't seem to be sent when in 3G. The code is called and it returns to the callback with a -1001 error and the following message: "(The request timed out.)"
Protocol is HTTP, not HTTPs.
Code works fine in Wi-Fi.
Wireshark on the server does not show any incoming data when app is in 3G (but data is received when in Wi-Fi).
After network inspection in Instruments: request is shown but no data / packets out.
Code Below:
self.request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:self.route]];
[self.request setHTTPMethod:self.httpMethod];
self.request.timeoutInterval = XX.f;
[NSURLConnection sendAsynchronousRequest:self.request
queue:queue
completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
// Stuff actually getting called but with -1001 error code (only when NOT in Wi-Fi)
}
It seems that OSX 10.10 Beta can corrupt the network privileges of an app when installing from XCode.
The issue can be worked-around by completely uninstalling the app and rebooting the phone (pressing home and power button simultaneously).
The error code you get (-1001) resolves to NSURLErrorTimedOut, which means your request timed out. I recommend you check your firewall (at the server-end). Is your server accessible from your phone? Is port 80 accessible from your phone? Note that the IP address of your phone when you're using WiFi differs from the IP address you get using 3G.