store internally a PDF from internet and display into WebView - ios

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.

Related

How do I fix my webview loading the second time and not the first?

This question is kind of self-explanatory but I'll explain it anyways. For some reason when I try and load a new web page it will see the web page URL except it won't load it the first time you press the button. Only when you press the button again will it actually load. How do I fix this?
Here's the code for my button:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *filePath = [NSString stringWithFormat:#"%#/%#", [paths objectAtIndex:0],#"index.html"];
// Download and write to file
NSURL *url = [NSURL URLWithString:_varString];
NSData *urlData = [NSData dataWithContentsOfURL:url];
[urlData writeToFile:filePath atomically:YES];
//Load file in UIWebView
[_Hoot loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:filePath]]];
*Also note, even though I'm copying the web page here and then loading it from a file, I still get the same problem if I'm just loading it from a URL.
Try this:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *filePath = [NSString stringWithFormat:#"%#/%#", [paths objectAtIndex:0],#"index.html"];
// Download and write to file
NSURL *url = [NSURL URLWithString:_varString];
NSData *urlData = [NSData dataWithContentsOfURL:url];
[urlData writeToFile:filePath atomically:YES];
//Load file in UIWebView
NSURL *urlen = [NSURL fileURLWithPath:filePath];
NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:urlen];
[_Hoot loadRequest:urlRequest];

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.

UIWebView displaying blank screen when loading

My app has to be able to download a file and display it. The format could be any of the major format types. I have been doing this with a UIWebView but when iOS 8 rolled out it broke this feature. I am wondering if this an issue with iOS 8 or is there a work around that I can use?
Here is my code:
//To show how I create the file path
NSString *resourceDocPath = [[NSString alloc] initWithString:[[[[NSBundle mainBundle] resourcePath] stringByDeletingLastPathComponent] stringByAppendingPathComponent:#"Documents"]];
//Name is the file's name that I get from the web service I use.
NSString *filePath = [resourceDocPath stringByAppendingPathComponent:name];
- (void) showFile:(NSString *)path andFileName:(NSString *)name
{
dispatch_async(dispatch_get_main_queue(), ^{
self.webView = [[UIWebView alloc] initWithFrame:self.view.frame];
NSURL *targetURL = [NSURL fileURLWithPath:self.filePath];
NSURLRequest *request = [NSURLRequest requestWithURL:targetURL];
[self.webView loadRequest:request];
self.webView.delegate = self;
[self.view addSubview:self.webView];
});
}
Thank you in advance.
I ran into the same problem rendering a pdf document on iOS 8.
So far, I've noticed this only for pdfs. So i check and see if the file is a pdf or not.
This is what i'm doing as a workaround.
NSData *pdfData = [[NSData alloc] initWithContentsOfFile:fileLocation];
[self.webView loadData:pdfData MIMEType:#"application/pdf" textEncodingName:#"utf-8" baseURL:nil];
I found out what the issue was in my case and so I wanted to post the answer.
I was using a direct file path:
NSString *resourceDocPath = [[NSString alloc] initWithString:[[[[NSBundle mainBundle] resourcePath] stringByDeletingLastPathComponent] stringByAppendingPathComponent:#"Documents"]];
When I should have been using a relative path like so:
NSString *resourceDocPath;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
resourceDocPath = [paths objectAtIndex:0];
They changes the file structure for iOS 8 which is why my code wasn't working since I was looking in the wrong place. I hope this helps someone else.

Save NSURL's PDF Locally - Retrieve

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];

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