Save NSURL's PDF Locally - Retrieve - ios

Hello,
How would I retrieve an NSUrl's PDF locally from a NSDictionary. I save it like so:
NSData *pdfData = [[NSData alloc] initWithContentsOfURL:[
NSURL URLWithString:#"http://www.example.com/info.pdf"]];
// Store the Data locally as PDF File
NSString *resourceDocPath = [[NSString alloc] initWithString:[
[[[NSBundle mainBundle] resourcePath] stringByDeletingLastPathComponent]
stringByAppendingPathComponent:#"Documents"
]];
NSString *filePath = [resourceDocPath
stringByAppendingPathComponent:#"myPDF.pdf"];
[pdfData writeToFile:filePath atomically:YES];
// Now create Request for the file that was saved in your documents folder
NSURL *url = [NSURL fileURLWithPath:filePath];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[webView setDelegate:self];
[webView loadRequest:requestObj];
But how would I retrieve the file and display it in a UIWebView??
Cheers,
SebOH

Try this solution. Once you downloaded the file you don't need to download every time. you can access the directory where the file has been stored.
// Store the Data locally as PDF File
NSString *resourceDocPath = [[NSString alloc] initWithString:[
[[NSBundle mainBundle] resourcePath]
stringByAppendingPathComponent:#"Documents"
]];
NSString *filePath = [resourceDocPath
stringByAppendingPathComponent:#"myPDF.pdf"];
if ([[NSFileManager defaultManager] fileExistsAtPath:filePath]) {
NSData *pdfData = [[NSData alloc] initWithContentsOfURL:[
NSURL URLWithString:#"http://www.example.com/info.pdf"]];
[pdfData writeToFile:filePath atomically:YES];
}
// Now create Request for the file that was saved in your documents folder
NSURL *url = [NSURL fileURLWithPath:filePath];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[webView setDelegate:self];
[webView loadRequest:requestObj];

You are doing the exact thing, "retrieve the file and display it in a UIWebView??" using the below code
// Now create Request for the file that was saved in your documents folder
NSURL *url = [NSURL fileURLWithPath:filePath];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[webView setDelegate:self];
[webView loadRequest:requestObj];
Is there some problem you are facing with the above lines of code, is the PDF loading in the webview?
So if you don't want to download the file every time do this check,
if (![[NSFileManager defaultManager] fileExistsAtPath:filePath]) {
//download the file
}
So the whole solution would be
// Store the Data locally as PDF File
NSString *resourceDocPath = [[NSString alloc] initWithString:[
[[[NSBundle mainBundle] resourcePath] stringByDeletingLastPathComponent]
stringByAppendingPathComponent:#"Documents"
]];
NSString *filePath = [resourceDocPath
stringByAppendingPathComponent:#"myPDF.pdf"];
if (![[NSFileManager defaultManager] fileExistsAtPath:filePath]) {
//download the file
NSData *pdfData = [[NSData alloc] initWithContentsOfURL:[
NSURL URLWithString:#"http://www.example.com/info.pdf"]];
[pdfData writeToFile:filePath atomically:YES];
}
// Now create Request for the file that was saved in your documents folder
NSURL *url = [NSURL fileURLWithPath:filePath];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[webView setDelegate:self];
[webView loadRequest:requestObj];

Related

how to read pdf file from url in ios

i have pdf file name now i want to open that pdf file from my ipad device with compatable application...
NSString *fileurl=[FILE_URL_Array objectAtIndex:indexPath.row];
NSArray *parts = [fileurl componentsSeparatedByString:#"/"];
NSString *filename = [parts lastObject];
NSString *extn =[filename pathExtension];
NSLog(#"file extn is %#",extn);
NSURL *url = [NSURL URLWithString:fileurl];
NSLog(#"not existing url is %#",url);
NSData *urlData = [NSData dataWithContentsOfURL:url];
now i want to open this file in my ipad device..when i click on it..can any one have idea?...Thanks in advance
You can use UIwebview to load it. Check out the answers on this post a while back. opening pdf
you can load your pdf on webview
NSURLRequest *req = [[NSURLRequest alloc] initwithUrl:url];
[self.webview loadRequest:req];
Might be help you...
// Your code
NSString *fileurl=[FILE_URL_Array objectAtIndex:indexPath.row];
NSArray *parts = [fileurl componentsSeparatedByString:#"/"];
NSString *filename = [parts lastObject];
NSString *extn =[filename pathExtension];
NSLog(#"file extn is %#",extn);
NSURL *url = [NSURL URLWithString:fileurl];
// Take a web view and load the url in the web view
NSURLRequest *request = [NSURLRequest url];
dispatch_async(dispatch_get_main_queue(), ^{
[_webView loadRequest:request]; // UIWebView object declared globally
});
Happy coding...
PDF file can open by UIWebView and WKWebView, you just load the PDF file by a NSURL.

iOS Load and view .docx file in UIWebview

How to load and View the .docx file in UIWebView? WPS can do it.
enter image description here
NSString *path = [[NSBundle mainBundle] pathForResource:#"data.docx" ofType:nil];
NSURL *url = [NSURL fileURLWithPath:path];
NSData *data = [NSData dataWithContentsOfFile:path];
[self.webview loadData:data MIMEType:#"application/vnd.openxmlformats-officedocument.wordprocessingml.document" textEncodingName:#"UTF-8" baseURL:url];

store internally a PDF from internet and display into WebView

I'm developing an app for iOS 8.1 using Xcode 6.1.1
And I want to download a PDF from internet, store in to my iPad and then load it into a webview; this is the PDF test:
this is the result I get on iPad after trying to display it on my WebView:
this is my code so far...
// Get the PDF Data from the url in a NSData Object
NSData *pdfData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:#"http://www.ferc.gov/docs-filing/elibrary/larg-file.pdf"]];
// Store the Data locally as PDF File
NSArray *searchPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentFolderPath = searchPaths[0];
NSString *archivePath = [documentFolderPath stringByAppendingPathComponent:#"larg-file.pdf"];
[pdfData writeToFile:archivePath atomically:YES];
// Now create Request for the file that was saved in your documents folder
NSURL *url = [NSURL fileURLWithPath:archivePath];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[_webView setUserInteractionEnabled:YES];
[_webView setDelegate:self];
[_webView loadRequest:requestObj];
How do I make this work?
EDIT: this is the answer
NSArray *searchPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentFolderPath = searchPaths[0];
NSString *archivePath = [documentFolderPath stringByAppendingPathComponent:#"larg-file.pdf"];
if (![Functions isAlreadyStoredinDeviceFileWithName:#"larg-file.pdf"]) {
NSData *pdfData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:#"http://www.ferc.gov/docs-filing/elibrary/larg-file.pdf"]];
[pdfData writeToFile:archivePath atomically:YES];
}
// Now create Request for the file that was saved in your documents folder
NSURL *url = [NSURL fileURLWithPath:archivePath];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[_webView setUserInteractionEnabled:YES];
[_webView setDelegate:self];
[_webView loadRequest:requestObj];
This code:
NSString *resourceDocPath = [[NSString alloc] initWithString:[
[[[NSBundle mainBundle] resourcePath] stringByDeletingLastPathComponent]
stringByAppendingPathComponent:#"Documents"]];
Attempts to formulate a directory within the application bundle. You do not have write access to the application bundle. Your file therefore isn't saved.
Check out -[NSFileManager URLsForDirectory:inDomains:] or NSSearchPathForDirectoriesInDomains for how to get the path to the user's documents folder.

Show downloaded PDF file with webView ios

I'm making an iPhone app, that will download a PDF file and display it in a webView.
However my script will not show the downloaded PDF. It does download it and save it in Documents, but the webView will not show it.
Here's my script:
NSString *path = [[NSBundle mainBundle] pathForResource:#"3" ofType:#"pdf"];
NSURL *urlen = [NSURL fileURLWithPath:path];
NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:urlen];
[webView loadRequest:urlRequest];
[webView setScalesPageToFit:YES];
From the official documentation on NSURL official documentation on NSURL.
Sending nil as the argument to fileURLWithPath: produces an exception.
The problem then is actually with [[NSBundle mainBundle] pathForResource:ofType:]. This is returning nil, rather than an actual path to a file.
The problem here is actually that [NSBundle mainBundle] refers to files that are bundle with your app. You need to look in your app's document directory, which is where it stores files it has downloaded.
This method will give you the path to your app's document directory:
NSString* documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
Now, just append the file name to this path:
NSString *pdfPath = [documentsPath stringByAppendingPathComponent:#"3.pdf"];
And for good measure (because crashes are always bad), make sure the file exists as such:
BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:pdfPath];
And finish as such:
if (fileExists) {
NSURL *urlen = [NSURL fileURLWithPath:pdfPath];
NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:urlen];
[webView loadRequest:urlRequest];
[webView setScalesPageToFit:YES];
} else {
// probably let the user know there's some sort of problem
}

Download / retrieve pdf with asihttprequest

I'm trying to download content with an URL to a local file system in my iPad (cachedDir ? )
with this function:
- (IBAction)download:(id)sender {
NSURL *url = [NSURL URLWithString:#"http:www.google.de"];
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request setDelegate:self];
[request startAsynchronous];
[request setDownloadDestinationPath:#"/Users/ben/Desktop/my_file.pdf"]; // Which //directory??
}
Which directory do I have to choose for my path if I want to store the data as long as possible without getting rejected by Apple,
and how do I retrieve the data I've saved?
Can somebody help?
The best place to store documents that you want to keep around is your application's Documents directly. You can find the path to it like so:
// Returns the URL to the application's Documents directory.
- (NSURL *)applicationDocumentsDirectory
{
return [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
}
Apple have a useful piece of documentation about the iOS file system and where to store particular types of files: http://developer.apple.com/library/mac/#documentation/FileManagement/Conceptual/FileSystemProgrammingGUide/FileSystemOverview/FileSystemOverview.html
Try out this my code for save pdf in document using below code
NSString *downloadUrl=[NSURL URLWithString:#"http:www.google.de"];
NSData *pdfData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:downloadUrl]];
//Store the Data locally as PDF File
NSString *resourceDocPath = [[NSString alloc] initWithString:[[[[NSBundle mainBundle] resourcePath] stringByDeletingLastPathComponent] stringByAppendingPathComponent:#"Documents"]];
NSString *pdf1 = #"title.pdf";
NSString *filePath = [resourceDocPath stringByAppendingPathComponent:pdf1];
[pdfData writeToFile:filePath atomically:YES];
and retrive your file using
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSURL *pdfURL = [[NSBundle bundleWithPath:[paths objectAtIndex:0]] URLForResource:[NSString stringWithFormat:#"title.pdf"] withExtension:nil];
NSLog(#"pDF URl %#", pdfURL);
pdf = CGPDFDocumentCreateWithURL((CFURLRef)pdfURL);

Resources