iOS large file download ~1GB files - ios

For my bachelor thesis I have to implement an eLearning app for iOS. As you can see in my title, I have to download a file in the documents directory that has a size of nearly 1 or 2 GB. I'm very new at iOS-Development, so I would appreciate every tip how i can handle that.
Many thanks in advance!

Its good that you have read about NSURLConnection and ASIHTTP. But as you must be aware ASIHTTP is no longer actively developed/maintained. NSURLConnection is good to understand and learn the basics.
For day to day use, I'd suggest you use AFNetworking. It is simple to use and contains example for understanding how to use it.
For downloading large files it is recommended to write the downloaded data directly to a file rather than storing it in the memory. Using AFNetorking you can do this by,
operation.outputStream = [NSOutputStream outputStreamToFileAtPath:#"download.zip" append:NO];
I have not tried downloading that large data myself, but I'm sure this will be a good start point for you.
AFNetworking: https://github.com/AFNetworking/AFNetworking
API documentation: http://cocoadocs.org/docsets/AFNetworking/2.0.0-RC2/
Happy coding!

Well, since this will very likely take a while to download I would definitely recommend doing this asynchronously using NSURLRequests and NSURLConnections.
My biggest concern though, is the pure size of this download and how this will likely not fit into RAM so this download may not actually be as easy as it seems. You can give it a shot, but what I would recommend is breaking this into multiple files/downloads if possible.
I will post code in a minute when I find code I have previously written.
Added Code
First off, make your class that needs to do the downloading to be an NSURLConnectionDelegate and `NSURLConnectionDataDelegate.
Then you can implement the download request as follows.
NSMutableData *linkData = [[NSMutableData alloc] init];
NSURLRequest *linkRequest = [[NSURLRequest alloc] initWithURL:youURL cachePolicy:NSURLRequestReloadRevalidatingCacheData timeoutInterval:10.0];
NSURLConnection *connect = [[NSURLConnection alloc] initWithRequest:linkRequest delegate:self];
The variable linkData should really be declared in the interface file and then allocated in the implementation, but for brevity I just created it as above.
Then you need the following methods which will be called
-(void) connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
[linkData appendData:data];
}
-(void) connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{
// handle a fail
}
-(void) connectionDidFinishLoading:(NSURLConnection *)connection {
// do what you need when download finishes
}

Related

Xcode - iOS - Simply upload a file to FTP Server

I'm trying to work with FTP Servers.
I have googled around for everything and everything is hard to understand for beginners like me. SimpleFTPSample is hard to understand because it is so much at a time. views, buttons, labels, textflelds, upload, download, request, list, get. Same with BlackRaccoon and everything else.
How to Simply and programily upload "test.txt" to FTP Server: "192.168.1.111" in Xcode (iPhone app) without views or button. Just code that can be in the ViewDidLoad for example.
Maybe something like this?:
NSURL* url = [NSURL URLWithString:#"ftp://username:pw#189.92.32.34"];
CFReadStreamRef stream = CFReadStreamCreateWithFTPURL(NULL, (__bridge CFURLRef) url);
stream.delegate= self;
[stream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
[stream open];
but which file?
expand this, or write a new code. i don't know, this is new for me.
Thanks Jonathan
As the writer of Black Raccoon perhaps I'm biased (well, I KNOW I'm biased), but I've attempted to make it as simple and powerful as possible. Let's look at what you want to do, upload a file:
There are four things we need to upload a file - start up code, then four delegate methods: overwrite check, data, success and the fail. Let's assume that you read the entire file into memory (okay for small files less than 2 megs).
First, you need this in your header:
BRRequestUpload *uploadData; // Black Raccoon's upload object
NSData *uploadData; // data we plan to upload
Now for the code part:
- (IBAction) uploadFile :(id)sender
{
//----- get the file path for the item we want to upload
NSString *applicationDocumentsDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *filepath = [NSString stringWithFormat: #"%#/%#", applicationDocumentsDir, #"file.text"];
//----- read the entire file into memory (small files only)
uploadData = [NSData dataWithContentsOfFile: filepath];
//----- create our upload object
uploadFile = [[BRRequestUpload alloc] initWithDelegate: self];
//----- for anonymous login just leave the username and password nil
uploadFile.path = #"/home/user/myfile.txt";
uploadFile.hostname = #"192.168.1.100";
uploadFile.username = #"yourusername";
uploadFile.password = #"yourpassword";
//----- we start the request
[uploadFile start];
}
The first will be asking your code if you want to overwrite an existing file.
-(BOOL) shouldOverwriteFileWithRequest: (BRRequest *) request
{
//----- set this as appropriate if you want the file to be overwritten
if (request == uploadFile)
{
//----- if uploading a file, we set it to YES (if set to NO, nothing happens)
return YES;
}
}
Next, Black Raccoon will ask you for chunks of data to send. If you have a very large file you NEVER want to try to send it all in one shot - Apple's API will choke and drop data. However, we only have one small chunk so we do this:
- (NSData *) requestDataToSend: (BRRequestUpload *) request
{
//----- returns data object or nil when complete
//----- basically, first time we return the pointer to the NSData.
//----- and BR will upload the data.
//----- Second time we return nil which means no more data to send
NSData *temp = uploadData; // this is a shallow copy of the pointer
uploadData = nil; // next time around, return nil...
return temp;
}
Remember we can ONLY do this for a small file.
Next we have our completion handler (if things worked according to plan):
-(void) requestCompleted: (BRRequest *) request
{
if (request == uploadFile)
{
NSLog(#"%# completed!", request);
uploadFile = nil;
}
}
Lastly we have our failure handler:
-(void) requestFailed:(BRRequest *) request
{
if (request == uploadFile)
{
NSLog(#"%#", request.error.message);
uploadFile = nil;
}
}
It would be WONDERFUL if it was as simple as saying [BRFtpUploadTo: dest srcfile: srcFile destfile: dstFile] but there are many reasons why you SHOULDN'T. Part of it has to do with how Apple has implemented their internal FTP. There are also the issues of blocking, errors, etc. In the end, FTP sounds like it should be trivial but ends up being a bit of a nightmare.
FTP is non-trivial which is why there are so many implementations. I'm not arguing that Black Raccoon is the best, but it is maintained with response to issues being between minutes to a couple of days.
It may look daunting at first, but Black Raccoon is, in my opinion, one of the better FTP libraries. I've spent a lot of time and effort to make it a quality product with excellent response to issues. How do I do this for free? Volume. ;)
Good luck with whatever FTP software you end up with!
Upload path is required when uploading. That is the way FTP works.
The port is the standard FTP port. I know of no way to change this without violating the API. If you figure it out, you stand a good chance of not passing Apple's check.
This code will upload/download any file.
I do not know how to make this work under secure conditions. This uses Apple's FTP protocol. There are other FTP packages that have built this from scratch and are far more intelligent. I would look into them.
BR was designed because I needed simple FTP communication. White Raccoon didn't do this for me because at the time (it has since been modernized).

NSURLConnection always failing after restarting app

I realize this is a vague question, but I'm wondering if anyone else has observed this. Here is my code for calling the NSURLConnection
// Get data from server
NSString *host = #"www.hostname.com";
NSString *urlString = [NSString stringWithFormat:#"/theRestOfTheURL"];
NSURL *url = [[NSURL alloc] initWithScheme:#"http" host:host path:urlString];
DLog(#"URL is %#", url);
// Create the NSMutableData to hold the received data.
// receivedData is an instance variable declared elsewhere.
receivedData_ = [[NSMutableData data] retain];
NSURLRequest *theRequest=[NSURLRequest requestWithURL:url
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:15.0];
// create the connection with the request
// and start loading the data
self.powerPlantDataConnection = [[[NSURLConnection alloc] initWithRequest:theRequest delegate:self] autorelease];
[url release];
When I first load the app it works fine, and I can call it repeatedly without any problem. But if I close the app and reopen it, the
(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
delegate method gets called every time, with a request timed out error message. I have two different view controllers where I am making calls to two different URLS, and both of them fail every time after closing and reopening the app.
Can anyone think of any reason why this might be happening? I'm not sure where to start looking. What could be the cause of a request timed out error? There should be nothing wrong with the request since it works when I first run the app.
Edited to add that it seems I only have this problem on my device, not on the simulator.
Wish you had shared some chrash log(especially with clear definition like
[error localizedDescription] class method..)
As you have said it is going to timeout(your request). and since your way of creating the objects is too messy, you make the work bigger for your system. I suggest using GCD when downloading data and especially in situations like yours, having different interfaces and urls..
A suggestion
You can create your url object like this:
NSURL *url = [NSURL urlWithString:[NSString stringWithFormat:#"http://%#/%#?key1=%#&key2=%#", yourDomain, targetFile, value1, value2]];
I switched over to using ASIHTTPRequest, which improved things but I would still get stuck in situations where I was getting timed out errors every time I tried to refresh. I looked and asked around, and eventually found that disabling calls to TestFlight solved my issue. More information here:
ASIHTTPRequest request times out
and here:
github.com/pokeb/asi-http-request/issues/320

ASIHTTPRequest didReceiveData - how to use?

I want to download a file using ASIHTTPRequest, and I want it to behave like a regular direct-to-file download, except I want to encrypt the data as it comes in.
Because I need custom data handling, I need to have my delegate implement request:didReceiveData, and I found out that: "ASIHTTPRequest will not populate responseData or write the response to downloadDestinationPath - you must store the response yourself if you need to."
I can't find any examples of code that implements a custom didReceiveData, I'm not sure how to handle data as it comes in or how to set the download destination path. Is there example I can look at online?
To write data to a destination file I tried to define the function as simply:
-(void)request:(ASIHTTPRequest *)request didReceiveData:(NSData *)data {
[data writeToFile:request.downloadDestinationPath atomically:YES];
}
But when the request is complete, the file doesn't exist, verified by:
for (ASIHTTPRequest* req in queue.operations) {
NSLog(#"file at %#", req.downloadDestinationPath);
if ([[NSFileManager defaultManager] fileExistsAtPath:req.downloadDestinationPath]) {
NSLog(#"file exists!");
}
}
If anyone has experience with this library and could point me to a resource, example project, tutorial, or just has a simple answer, I would much appreciate it :)
EDIT: would it be better to use NSURLConnection?
The library comes with a sample project

How to test if a file at an external URL exists without downloading it?

I would like to test if a URL exists without loading any actual data.
I was thinking of initiating a NSURLConnection, and then using these two delegate methods to check the status:
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
// Connection failed ... presumably this is a server issue, and I don't know if the URL exists or not.
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
int status = [((NSHTTPURLResponse *)response) statusCode];
[connection cancel];
// Do something with the status
}
Would this be a sensible way? The files I'm testing could potentially be very large, so I want to make sure that the actual file is not downloaded.
Thanks,
Ron
p.s. I'm looking at adding this because Apple forced my App to stop backing up files downloaded from the internet. Instead, I store the files in non-backed up space, and in my backed up database I keep track of the original location of the file. Since the file is intended to be a permanent part of the user's library, I would like to periodically test if a file is not accessible and then move it into backed up space (so it will survive a restore to a new device, for example). I'm very annoyed at Apple for forcing me to make this change as I can imagine customers losing important data.
p.p.s. For some strange reason, these delegate methods no longer appear in the documentation for NSURLConnectionDelegate Protocol. I'm assuming the documentation is just messed up.
Personally, I would send a raw request or modify the head to use HEAD which is a standard HTTP header.
It has the same result type as GET, so if you can check the result headers, see if its 403,404 or 201 then you're good.
http://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html

What are alternatives to NSURLConnection for chunked transfer encoding

I've checked for other questions relevant to this, but the only answer is "Use ASIHTTPRequest" as this is no longer being developed I wanted to ask what alternatives people are using, whilst working on our SDK I came across a lot of strange behaviour in NSURLConnection when receiving data from the server.
We tracked it down to the fact that NSURLConnection doesn't deal well with responses in chunked-encoding. Or at least so we read in this question here NSURLConnection and "chunked" transfer-coding
Some developers we were talking to say it gets better in iOS 5, we need to make sure that our SDK is backwards compatible with iOS 4.3 at least.
I want to confirm this is infact an issue in NSURLConnection, and how people are dealing with it.
All the alternatives I've found so far are based off of NSURLConnection and I'm assuming as such will have the same flaw. ASIHTTPRequest did in fact work because it's based a little lower than NSURLConnection, but were looking for alternatives in the knowledge it's no longer supported.
A list of other libraries looked at are:
Restkit,
ShareKit,
LRResty,
AFNetworking,
TTURLRequest
I'm aware there are similar questions here Is RESTKit a good replacement for ASIHTTPRequest? and here ASIHTTPRequest alternative But both of the solutions are based off NSURLConnection.
EDIT: I noticed I pointed to the wrong question at the start of my post, so thats updated. It points to a thread from 2008, and i've seen similar ones but none that are recent.
Chunked transfers are supported by NSURLConnection. I use them.
Define some props:
NSMutableData * responseData;
NSURLConnection * connection;
Establish a connection
NSURL *url = [NSURL URLWithString:#"...."];
self.responseData = [[NSMutableData alloc] initWithLength:0] ;
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
self.connection = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:YES];
Register your callback method for connection established
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
// You may have received an HTTP 200 here, or not...
[responseData setLength:0];
}
Register your callback method for "chunk received"
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
NSString* aStr = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
NSLog(#"This is my first chunk %#", aStr);
}
Register your "connection finished" callback:
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
[connection release];
}
And finally, register you "connection failed" callback:
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
NSLog(#"Something went wrong...");
}
Just to chime in for the next person that gets here and still can't get NSURLConnection to work with chunk encoded data.
NSURLConnection will work with chunked encoding, but has non-disclosed internal behaviour such that it will buffer first 512 bytes before it opens the connection and let anything through IF Content-Type in the response header is "text/html", or "application/octet-stream". This pertains to iOS7 at least.
However it doesn't buffer the response if Content-Type is set to "text/json". So, whoever can't get chunked encoded NSURLConnection responses to work (i.e. callbacks aren't called) should check the response header and change it on the server to "text/json" if it doesn't break application behaviour in some other way.
There aren't any alternatives I'm aware of.
All the other libraries are built on top of NSURLConnection. Though you could use one of the non-iOS libraries, eg. libcurl.
ASIHTTPRequest is the only library I'm aware of that's built on top of the CFNetworking layer instead. This was (perhaps indirectly) the main reason the original developer stopped working on it - because it doesn't use NSURLConnection it has a lot of code.
It's probably not strictly correct to say that ASIHTTPRequest is no longer supported. It is true that the original developer no longer works on it, but if you look at the github commits you'll see it is still being worked on by other people. A lot of people still use it, for various reasons, myself included.
Having said all that, to go back to the problem you have: I'm not sure a 3 year old thread is necessarily a definitive reference to prove that a 1 year old release (ie. iOS 4.3) of NSURLConnection doesn't support chunked transfers. Chunked transfers are used so much on the web that it seems highly unlikely it would have a problem this large and obvious. It's possible there is something very particular to the server that you're using that is causing the issue.

Resources