Save PDF that is displayed locally by UIWebView

I have a UIViewController with a UIWebView that displays a PDF file, depending on which line was clicked before in the UITableView . Now I want to add a button so that the user can save this pdf file locally for offline use. Then a second UITableView appears, which should display the name of the saved pdf and, clicking on it, another UIViewController will appear and display the saved pdf in standalone UIWebView . What would be a good way to get started? Thanks

+3
source share
1 answer

You can try this way:
1) Add a View button containing a UIWebView
2) When you click the button, save the file shown in UIWebView
(note: in iOS 5 you must save data that can be easily recreated or loaded into the cache 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) When you start the application, you need to check which files are stored (iOS can delete the cache directory if there is not enough space on the iPhone)

+5
source

Source: https://habr.com/ru/post/1410884/


All Articles