【问题标题】:Download pdf file from server, save it in ApplicationSupport directory. iOS从服务器下载 pdf 文件,保存在 ApplicationSupport 目录中。 iOS
【发布时间】:2011-09-14 11:58:49
【问题描述】:
我是 iOS 开发的新手,我想知道如何从服务器下载文件,并将其保存在我的 Application Support 文件夹中。我想将它保存为 .pdf 文件,以便能够在 UIWebView 中显示它。
在不同的网站呆了很长时间,我想我应该使用 NSURLConnection (异步)来下载它。或者 NSData (我已经试过了,但没用)。
那么,有人可以帮我看看这个示例代码吗?
非常感谢:)
【问题讨论】:
标签:
ios
pdf
download
nsurlconnection
【解决方案1】:
[self.productListArray enumerateObjectsUsingBlock:^(NSDictionary *productDictionary, NSUInteger idx, BOOL *stop)
{
NSFileManager *fileManger=[NSFileManager defaultManager];
if(![fileManger fileExistsAtPath:pdfString])
{
dispatch_async(serialQueue, ^()
{
NSURLRequest *request = [[NSURLRequest alloc]initWithURL:url cachePolicy:NSURLCacheStorageNotAllowed timeoutInterval:120];
NSURLResponse *response = nil;
NSError *connectionError = nil;
NSData *data = [NSURLConnection sendSynchronousRequest:request
returningResponse:&response
error:&connectionError];
if(connectionError)
{
NSLog(@"Pdf Connection Error==>%@",connectionError.userInfo);
[AMSharedClass showAlertMessge:@"Request timeout"];
}
else if ([response.MIMEType isEqualToString:@"application/pdf"])
{
NSLog(@"pdfFilePathURLString==>%@",pdfString);
[data writeToFile:pdfString atomically:YES];
}
else
{
[AMSharedClass showAlertMessge:@"Pdf not found."];
if (idx+1 == [self.productListArray count])
{
[self.btnSetting setEnabled:NO];
}
}
if (idx+1 == [self.productListArray count])
{
[[[AMSharedClass object]sharedHUD]hideOnWindow];
self.pdfURLString = [self joinPDF:self.productFilePathUrlArray WithDetails:self.pdfInfoArray];
[self initialConfiguration];
NSLog(@"%@",self.productFilePathUrlArray);
}
});
// Long running task
}
else
{
if (idx+1 == [self.productListArray count])
{
self.pdfURLString = [self joinPDF:self.productFilePathUrlArray WithDetails:self.pdfInfoArray];
[self initialConfiguration];
NSLog(@"%@",self.productFilePathUrlArray);
[[[AMSharedClass object]sharedHUD]hideOnWindow];
}
}
}];