I want to download a file using ASIHTTPRequest, with the following method:
ASIHTTPRequest *request;
request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:downloadServer]];
[request setUserInfo:[NSDictionary dictionaryWithObject:@"download" forKey:@"download"]];
[request setDidFinishSelector:@selector(downloadIsFinished:)];
[request setDidFailSelector:@selector(downloadIsFailed:)];
[request setDelegate:self];
[request setTimeOutSeconds:40];
[request setDownloadProgressDelegate:self];
[request setShowAccurateProgress:YES];
[request startAsynchronous];
If I’m on Wifi everything’s fine, but if I’m on 3G then my method tries to download the file but after 10 seconds the downloadIsFinish delegate fires, but my file hasn’t actually been downloaded….
This kind of thing is usually a bug caused by not implementing the
NSURLConnectionDelegatemethods correctly. One visible side-effect can be half-downloaded images appearing in your UI.For example, if the server disconnects before you have received the entire response, no error is raised by
NSURLConnection.connectionDidFinishLoading:is called as usual. You have to explicitly check that the number of bytes you received is equal to the number specified in theContent-Lengthheader (if it was present). If you haven’t received all the data, you need to raise an error yourself.Note that development of
ASIHTTPRequesthas stopped. See this blog post from its creator.Most people seem to recommend
AFNetworkingnowadays, although last time I checked they also didn’t handle this case of early disconnects.