Save PDF which is displayed by UIWebView locally

前端 未结 1 1098
我寻月下人不归
我寻月下人不归 2021-02-06 18:01

I have a UIViewController with an UIWebView which displays a pdf file depending which row was clicked before in an UITableView. Now I want

相关标签:
1条回答
  • 2021-02-06 18:14

    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)

    0 讨论(0)
提交回复
热议问题