在iOS中使用Swift保存PDF文件并显示它们

我想要构build一个应用程序,其中还包括在应用程序中显示和保存PDF的可能性, 在桌面视图中显示它们(作为文件系统),并在点击一个PDF时打开它们。

这是我的重要问题:

1.如何在我的应用程序中保存本地PDF(例如,如果用户可以inputurl)以及保存在哪里?

2.保存时,如何显示一个tableview中的所有本地存储文件来打开它们?

由于有几个人要求这个,所以这里等同于Swift的第一个答案:

//The URL to Save let yourURL = NSURL(string: "http://somewebsite.com/somefile.pdf") //Create a URL request let urlRequest = NSURLRequest(URL: yourURL!) //get the data let theData = NSURLConnection.sendSynchronousRequest(urlRequest, returningResponse: nil, error: nil) //Get the local docs directory and append your local filename. var docURL = (NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask)).last as? NSURL docURL = docURL?.URLByAppendingPathComponent( "myFileName.pdf") //Lastly, write your file to the disk. theData?.writeToURL(docURL!, atomically: true) 

此外,由于此代码使用同步networking请求,我强烈build议将其分派到后台队列:

 dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), { () -> Void in //The URL to Save let yourURL = NSURL(string: "http://somewebsite.com/somefile.pdf") //Create a URL request let urlRequest = NSURLRequest(URL: yourURL!) //get the data let theData = NSURLConnection.sendSynchronousRequest(urlRequest, returningResponse: nil, error: nil) //Get the local docs directory and append your local filename. var docURL = (NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask)).last as? NSURL docURL = docURL?.URLByAppendingPathComponent( "myFileName.pdf") //Lastly, write your file to the disk. theData?.writeToURL(docURL!, atomically: true) }) 

Swift第二个问题的答案是:

 //Getting a list of the docs directory let docURL = (NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask).last) as? NSURL //put the contents in an array. var contents = (NSFileManager.defaultManager().contentsOfDirectoryAtURL(docURL!, includingPropertiesForKeys: nil, options: NSDirectoryEnumerationOptions.SkipsHiddenFiles, error: nil)) //print the file listing to the console println(contents) 

我给出了一个在iOS中存储和检索PDF文档的例子。 我希望那是你正在寻找的。

1.如何在我的应用程序中保存本地PDF(例如,如果用户可以inputurl)以及保存在哪里?

 // the URL to save NSURL *yourURL = [NSURL URLWithString:@"http://yourdomain.com/yourfile.pdf"]; // turn it into a request and use NSData to load its content NSURLRequest *request = [NSURLRequest requestWithURL:result.link]; NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil]; // find Documents directory and append your local filename NSURL *documentsURL = [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject]; documentsURL = [documentsURL URLByAppendingPathComponent:@"localFile.pdf"]; // and finally save the file [data writeToURL:documentsURL atomically:YES]; 

2.保存时,如何显示一个tableview中的所有本地存储文件来打开它们?

您可以检查文件是否已经下载,或者您可以像这样列出Documents目录:

 // list contents of Documents Directory just to check NSURL *documentsURL = [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject]; NSArray *contents = [[NSFileManager defaultManager]contentsOfDirectoryAtURL:documentsURL includingPropertiesForKeys:nil options:NSDirectoryEnumerationSkipsHiddenFiles error:nil]; NSLog(@"%@", [contents description]);