保存由UIWebView本地显示的PDF [英] Save PDF which is displayed by UIWebView locally
问题描述
我有一个带有 UIWebView
的 UIViewController
,它显示了一个pdf文件,该文件取决于之前在的UITableView
。现在我想为用户添加一个按钮,以便在本地保存此pdf文件以供离线使用。
然后还有第二个 UITableView
,它应该显示已保存的pdf的名称,然后点击另一个 UIViewController
出现并在 UIWebView
离线显示保存的pdf。
什么是好的开始?
谢谢
I have a UIViewController
with an UIWebView
which displays a pdf file depending which row was clicked before in an UITableView
. Now I want to add a button for the user to save this pdf file locally for offline use.
Then there is a second UITableView
which should display the name of the saved pdf and by clicking on it another UIViewController
appears and displays the saved pdf on a UIWebView
offline.
What would be a good way to start?
Thanks
推荐答案
您可以尝试这种方式:
1)添加一个按钮到查看
包含 UIWebView
2)按下按钮保存<$ c $中显示的文件c> UIWebView
(注意:在iOS 5中,您必须保存可以轻松重新创建或下载到缓存目录的数据)
You can try this way:
1) Add a button to the View
containing UIWebView
2) At button press save the file shown in UIWebView
(note: in iOS 5 you must save data that can be easily recreated or downloaded to the caches directory)
- (IBAction)buttonPress:(id)sender
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *cachePath = [paths objectAtIndex:0];
BOOL isDir = NO;
NSError *error;
//You must check if this directory exist every time
if (! [[NSFileManager defaultManager] fileExistsAtPath:cachePath isDirectory:&isDir] && isDir == NO)
{
[[NSFileManager defaultManager] createDirectoryAtPath:cachePath withIntermediateDirectories:NO attributes:nil error:&error];
}
NSString *filePath = [cachePath stringByAppendingPathComponent:@"someName.pdf"]
//webView.request.URL contains current URL of UIWebView, don't forget to set outlet for it
NSData *pdfFile = [NSData dataWithContentsOfURL:webView.request.URL];
[pdfFile writeToFile:filePath atomically:YES];
}
3)在应用程序启动时,您需要检查存储的文件(iOS可以如果iPhone上没有足够的空间,请删除缓存目录)
3) On application start you need to check what files are stored (iOS can delete cache directory if there is not enough space on iPhone)
这篇关于保存由UIWebView本地显示的PDF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!