Save PDF which is displayed by UIWebView locally

橙三吉。 提交于 2019-12-03 03:59:22
Sergey Kuryanov

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) On application start you need to check what files are stored (iOS can delete cache directory if there is not enough space on iPhone)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!