kdbdallas
kdbdallas

Reputation: 4543

Best way to download large files from web to iPhone for writing to disk

In an iPhone app I currently have code for downloading a file from the Web to the iPhone and saving it to disk.

The problem is that if the file is large then the memory usage of the app skyrockets and the app crashes.

I am sure I am just not doing it the "proper" way.

Currently I have the following:

mediaData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:path]];
[mediaData writeToFile:fullPath atomically:YES];
[mediaData release];

As I mentioned this works for something like a picture, but not for something like say a video clip, as the app crashes.

What is the proper way to do this to keep my app from crashing? My thought was maybe sockets, but as I have not done much socket programming, I am not sure.

Thanks

Upvotes: 1

Views: 6325

Answers (2)

François P.
François P.

Reputation: 5166

As NSURLDownload is not available on the iPhone, you might want to use NSURLConnection and buffer some data in a NSMutableData using connection:didReceiveData: delegate method. This article describes some of this: http://dannyg.com/iapps/Blog/Entries/2009/2/16_The_Joy_in_Discovering_You_Are_an_Idiot.html

Upvotes: 2

Chris Lundie
Chris Lundie

Reputation: 6023

You can use NSURLConnection which runs asynchronously and delivers data in manageable chunks.

Upvotes: 5

Related Questions