views:

121

answers:

3

I am building an application where we do updates for images and videos and was wondering if it was possible to download videos/images from a web-service that will host these assets somewhere on the iphone programatically?

Maybe through a curl service or ftp?

+1  A: 

You can download anything you like onto the phone (subject, of course, to bandwidth and storage constraints). Only movies of certain formats (m4v, not wmv files, for example) will actually be PLAYABLE on the device, however.

Ben Gottlieb
A: 

yes this is possible, there are multiple places (tmp and Documents) that you can write files to - if you write the downloaded file to the Documents directory, the file will be available the next time you launch the app, and will even persist between versions of your app as it is upgraded. You can also write to and edit files in the Documents directory, unlike files in your app's Resource directory that cannot update after install. This will be tricky for you, as for any given file there will be the version that came with the app install (in the Resources) that you cannot overwrite or edit, and then a new version in Documents, so your logic ill need to see if there is a newer version of the content in Documents, and play that, and than fallback on the app's Resource directory.

For downloading, I recommend using the lovely all seeing I ASIHTTPRequest lib for doing nice async/background download of files to disk, with progress information and bandwidth throttling (this is key).

http://allseeing-i.com/ASIHTTPRequest/

The other part of this to consider is that Apple will reject apps for excessive bandwidth use when not on WiFi, so either you need to limit big downloads to only over WiFi, or you need to throttle bandwidth to about 1mb/minute. ASIHTTPRequest does this very well, and while it is no guarantee against being dinged by Apple, it is much better than not throttling.

It is also worth mentioning that for streamed video, you now have to use http live streaming, but if you are not playing as it downloads, and are only downloading it for playback later, I think all you need to worry about is not going over the bandwidth limits, using a format that works, and writing the file to Documents so it will be retained between launches and app updates.

Andrew Kuklewicz
A: 

There are many ways to download files from remote servers to the iPhone, some even require just one line of code.

The most simple way might be:

[NSData dataWithContentsOfURL:fileURL]

But this will block your code till the file is downloaded, a more usable solution is probably to use NSURLConnection

NSURLRequest *request = [NSURLRequest requestWithURL:fileURL];
[NSURLConnection connectionWithRequest:request delegate:self];

And implement the delegate methods for NSURLConnection

Saving files can be done in numerous ways as well, simplest might be:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *localFilePath = [documentsDirectory stringByAppendingPathComponent:@"newFileName.ext"];
[NSData writeToFile:localFilePath atomically:YES];

Which will save the data to the documents directory of your app

Ron Srebro