Reputation: 573
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
Upvotes: 3
Views: 8275
Reputation: 6114
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)
Upvotes: 5