开发者

How to download files to iPhone's filesystem?

i would like to download a bunch of pdf's from a website to the iPhone filesystem in my app. Because i don't want to download the files everytime i start the app.

In the documentation i found a function called "dataWithContent开发者_开发问答sOfURL" but the sample code didn't help me. Is this the right way, or is there an easier solution?

Can someone give me a tip or two ? ;-)

greets max


I recommend using ASIHTTPRequest, it's well written, documented and easy to use, heres a quick example from one of my applications download class that downloads a JSON file using ASIHTTPRequest:

-(void)downloadJSONData:(NSString *)path destination:(NSString *)destination secure:(BOOL)secure {

    if (![self queue]) {
        [self setQueue:[[[NSOperationQueue alloc] init] autorelease]];
    }

    NSFileManager *fileManager = [NSFileManager defaultManager];
    [fileManager removeItemAtPath:destination error:NULL];

    NSURL *url = [NSURL URLWithString:path];
    ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];

    if(secure){
        [request addRequestHeader:@"Authorization" value:[NSString stringWithFormat:@"Basic %@",[ASIHTTPRequest base64forData:[[NSString stringWithFormat:@"%@:%@",self.username,self.password] dataUsingEncoding:NSUTF8StringEncoding]]]];
    }
    [request setDelegate:self];
    [request setDidFinishSelector:@selector(requestDone:)];
    [request setDidFailSelector:@selector(requestWentWrong:)];
    [request setDownloadDestinationPath:destination];
    [[self queue] addOperation:request]; //queue is an NSOperationQueue
}

I would take a look at the how-to-use page as it contains everything you need to know.


You're on the right track

-(void)downloadURL:(NSURL *)theUrl toLocalFileURL:(NSURL *)localURL
{
     NSData *dlData = [NSData dataWithContentsOfURL:theURL];
     [dlData writeToURL:localURL atomically:YES];
}

So just research how NSURLs are created for local and remote objects and you're all set.


Yes you could download files only as you need(Lazy load).

When ever you want access the files. Check in the documents directory for the file. In the second answer it is being specified. Append file name with the douments path. Check If there is a readable file using NSFileManager's isReadableFileAtPath:instance method. if it returns false then initiate downloading the pdf from the website.

Please do care to create a class which downloads file asynchronously. You could use NSURLconnection to initiate the request and its delegate methods to process its content After downloading content write it to documents folder.

If you could create a class for asynchronous download , you could initiate parallel downloads and use maximum use of the bandwidth.

By making asynchronous downloads you could make sure that you application is responsive even while files are getting downloaded.

http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSFileManager_Class/Reference/Reference.html

http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSURLConnection_Class/Reference/Reference.html

Regards ,

Jackson Sunny Rodrigues


Do not use dataWithContentsOfURL: unless you're executing it somewhere other than the main thread. It is a blocking API as well as one that accesses the network. That is a recipe for bad user experience.

Think about this: You use a blocking API to access the network, but the network is down or really slow. The main thread is now blocked, so your user interface is not responding to user interaction. The user gets frustrated and tries to cancel the download using the handy button you put on the UI, but "OH NO!" it doesn't work because the UI is blocked.

Do not use blocking APIs on the main thread.

You should look at the documentation for NSURLConnection and it's asynchronous loading methods for downloading data.


You should use

NSData* pdf_data = [NSData dataWithContentsOfFile: @"file_path"];

where file_path is the path where you've saved your file (it is usually Documents or Library directories).

To save data there use:

[pdf_data writeToFile: @"file_path" atomically: YES];

To get path to your documents directory you can use:

- (NSString *)applicationDocumentsDirectory {
    NSArray *paths = 
      NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *basePath = ([paths count] > 0) ? [paths objectAtIndex:0] : nil;
    return basePath;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜