如何将文件下载到iPhone的文件系统?

mad*_*max 0 iphone xcode objective-c ios

我想从我的应用程序中将一堆pdf从网站下载到iPhone文件系统.因为每次启动应用程序时我都不想下载文件.

在文档中,我找到了一个名为"dataWithContentsOfURL"的函数,但示例代码对我没有帮助.这是正确的方法,还是有更简单的解决方案?

有人可以给我一两个小费吗?;-)

迎接最大

Ale*_*lex 5

我建议使用ASIHTTPRequest,它编写得很好,文档化且易于使用,这是我的一个应用程序下载类的快速示例,该类使用ASIHTTPRequest下载JSON文件:

-(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
}
Run Code Online (Sandbox Code Playgroud)

我会看一下该如何使用的页面,因为它包含了你需要知道的一切.