Strong Like Bull
Strong Like Bull

Reputation: 11297

How to download large files in iOS?

I'm trying to download files [> 40MB] from web server over HTTP request. To do that I've used the SimpleURLConnection sample provided by the apple. In that sample they only download the image files, so I modified the code to download pdf files and stored it in application's document directory. This is working fine to download small files, but it only download 6.4MB of data if I trying to download large files [>40Mb]. please help me to fix this,

Thank you,

FYI:

code to write file with downloaded data

  - (void)connection:(NSURLConnection *)theConnection didReceiveData:(NSData *)data
    // A delegate method called by the NSURLConnection as data arrives.  We just 
    // write the data to the file.
{
    #pragma unused(theConnection)
    NSInteger       dataLength;
    const uint8_t * dataBytes;
    NSInteger       bytesWritten;
    NSInteger       bytesWrittenSoFar;

    assert(theConnection == self.connection);

    dataLength = [data length];
    dataBytes  = [data bytes];

    bytesWrittenSoFar = 0;
    do {
        bytesWritten = [self.fileStream write:&dataBytes[bytesWrittenSoFar] maxLength:dataLength - bytesWrittenSoFar];
        assert(bytesWritten != 0);
        if (bytesWritten == -1) {
            [self _stopReceiveWithStatus:@"File write error"];
            break;
        } else {
            bytesWrittenSoFar += bytesWritten;
        }
    } while (bytesWrittenSoFar != dataLength);
}

Upvotes: 3

Views: 8813

Answers (2)

ashack
ashack

Reputation: 1202

ASIHTTP is no longer in active development as of September 2011. I've found that RestKit is a good alternative in many cases, however it lacks many features of ASI such as download pausing and resuming.

Upvotes: 3

Blitz
Blitz

Reputation: 5671

Don't reinvent the wheel! ASIHTTP has you covered, see there. There's a specific option to download directly to file.

Upvotes: 8

Related Questions