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

标签 iphone objective-c xcode ios

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

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

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

问候最大

最佳答案

我推荐使用 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
}

我会看一下 how-to-use page因为它包含了你需要知道的一切。

关于iphone - 如何将文件下载到 iPhone 的文件系统?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5355483/

相关文章:

在 KTPhotoScrollViewController 和 KTThumbsViewController 之间移动时,使用 KTPhotoBrowser 库的 iPhone 状态栏在应用程序中消失

iphone - 访问单例中的可变值?

objective-c - NSString 未初始化时检查 "empty"NSString 崩溃

xcode - NSTextField 的 "Value With Pattern"绑定(bind)中的条件显示模式

ios - XCode AutoLayout 保存问题

iphone - iOS 模拟器上的谷歌地图?

iphone - 在 iPhone 上通过 Mobclix 集成 iAd 和 AdMob

ios - 如何多次显示 admob 插页式广告?

ios - 使用 CABasicAnimations 和 CoreMotion 移动 UIView

Objective C 中的 C++ 数组给出 EXC_BAD_ACCESS