我必须从NSURL读取文件并且必须在本地保存它

时间:2013-12-09 07:48:59

标签: ios macos

我是iOS编程的新手。谁能帮助我,我怎么能读取.PDF文件并在本地保存。

2 个答案:

答案 0 :(得分:0)

答案 1 :(得分:0)

如果您想阅读PDF,则可以在 UIWebView 中加载它。

NSURL *targetURL = [NSURL URLWithString:@"http://developer.apple.com/iphone/library/documentation/UIKit/Reference/UIWebView_Class/UIWebView_Class.pdf"];
NSURLRequest *request = [NSURLRequest requestWithURL:targetURL];
[webView loadRequest:request];

如果您需要下载PDF并在本地保存,

NSString *stringURL = URL_FOR_PDF;
NSURL  *url = [NSURL URLWithString:stringURL];
NSData *urlData = [NSData dataWithContentsOfURL:url];
if ( urlData )
{
  NSArray       *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  NSString  *documentsDirectory = [paths objectAtIndex:0];  

  NSString  *filePath = [NSString stringWithFormat:@"%@/%@", documentsDirectory,@"filename.png"];
  [urlData writeToFile:filePath atomically:YES];
}