Show downloaded PDF file with webView ios - 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
}

Related

Load file to application local storage

Is there an ability to load files like documents, articles from server and store them in application local storage using Filemanager? And how then users can read these files? Do they have access to that folder?
Follow these steps for downloading and saving file in NSDocumentDirectory
// Determine local file path
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *filePath = [NSString stringWithFormat:#"%#/%#", [paths objectAtIndex:0],#"fileName.doc"]; //it could be any file type
// Download and write to file
NSURL *url = [NSURL URLWithString:#"http://www.filePathFromServerLocation.com"];
//and write to file
NSData *urlData = [NSData dataWithContentsOfURL:url];
[urlData writeToFile:filePath atomically:YES];
This way your file will be save at filePath and u can use it in future whatever the way u like.
Opening a file in iOS:
Using a webview
NSString *path = [[NSBundle mainBundle] pathForResource:#"document" ofType:#"pdf"];
NSURL *targetURL = [NSURL fileURLWithPath:path];
NSURLRequest *request = [NSURLRequest requestWithURL:targetURL];
[webView loadRequest:request];
using a UIDocumentInteractionController.
Have a look at this Apple sample code for ZoomingPDFViewer

Can not load file using [[NSBundle mainBundle] pathForResource ofType inDirectory]

I have a directory with a website I need to add to my project. I will load it using a uiwebview.
I've tried several ways of adding it, all unsuccessful. This is the last way I tried:
Copied the whole website directory to my project directory (the actual physical folder).
After that, dragged the directory into my xcode project, and ticked "Create folder references for any added folders"
Then I looked at "build phases -> copy bundle resources ->..." and I see that it is there
When loading my webpage:
-(void)loadUrl
{
NSString *str = [[NSBundle mainBundle] pathForResource:#"index" ofType:#"html" inDirectory:#"websiteDir"]; // THIS IS RETURNING NIL!
NSURL *url = [NSURL URLWithString:str];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[self.webView loadRequest:request];
}
What am i doing wrong?
this is wrong:
you get str as path BUT then call URLWithString... but str is no URLString but a path
EITHER use url = [NSURL fileUrlWithPath:str]
OR use the method - (NSURL *)URLForResource:(NSString *)name withExtension:(NSString *)extension subdirectory:(NSString *)subpath
sample
-(void)loadUrl {
NSURL *url = [[NSBundle mainBundle] URLForResource:#"index" withExtension:#"html" subdirectory:#"websiteDir"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[self.webView loadRequest:request];
}

Crash when trying to open downloaded pdf in new view

My app downloads a pdf and then on a button press brings it up in a new view.
I get the error:
-[NSURL initFileURLWithPath:]: nil string parameter'
After some troubleshooting I pinned the problem to somewhere in this code snippet. The path that is being pointed to is in the /Documents folder where the downloaded pdf is placed. Thus the document is not in the main bundle.
NSString *path = [[NSBundle mainBundle] pathForResource:PDFpathwithextension ofType:#"pdf"];
NSURL *url = [NSURL fileURLWithPath:path];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
Here's the download code:
//Start an NSURL connection to download from the remotepath
NSData *pdfData = [[NSData alloc] initWithContentsOfURL:remotepathURL];
//Store the Data locally as PDF File
NSString *resourceDocPath = [[NSString alloc] initWithString:[[[[NSBundle mainBundle] resourcePath] stringByDeletingLastPathComponent] stringByAppendingPathComponent:#"Documents"]];
NSString *filePath = [resourceDocPath stringByAppendingPathComponent:[newdata.ThirdPickerName stringByAppendingFormat:#".pdf"]];
pdfData writeToFile:filePath atomically:YES];
As NSURL is telling you, you've handed it nil instead of a valid path.
nil here means no such resource could be found by that name. Indeed, your question suggests you're well aware of this.
Since you claim your app already downloads a PDF, does it actually write that out to disk? If so, you should know where the resulting file is from doing that. If not, you first need to write the actual download code!

UIWebView not loading local webpage

I have a super simple webpage that I just want to load a single local file. The file is /Resources/About.html
Here is my code:
NSString *urlPath = [[NSBundle mainBundle] URLForResource:#"About" withExtension:#"html"];
NSURL *url = [NSURL fileURLWithPath:urlPath];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
[webView loadRequest:urlRequest];
What's wrong?
The method URLForResource:withExtension returns an NSURL, not an NSString. You can use the result of that directly in your request.
NSURL *URL = [[NSBundle mainBundle] URLForResource:#"About" withExtension:#"html"];
[webView loadRequest:[NSURLRequest requestWithURL:URL]];
NSString * urlPath = [[NSBundle mainBundle] pathForResource:#"About" ofType:#"html"];
NSString *content = [NSString urlPath encoding:NSUTF8StringEncoding error:nil];
[webView content baseURL:[NSURL urlPath]];
I bet URLForResource is returning nil (Can you confirm that it is not?). It will not perform a recursive search, and will fail if your "Resources" folder is an actual folder (blue in XCode) as opposed to a group (yellow in Xcode). Try using the main bundle's resourceURL and appending your path to the end of it.

NSURLCache don't support file://?

I would like to load [NSURL fileURLWithPath:#"2.html"] in UIWebView use NSURLCache, the cachedResponseForRequest: function is executed and return a NSCachedURLResponse object which is performed correctly at http:// scheme.
But The UIWebView load failed finally. Did I miss some optional?
I want to use UIWebView load html file in a compressed file, and I do not expect to uncompress the files to disk. is there any suggestion.
Do this:
NSURL *url = [[NSBundle mainBundle] URLForResource:#"2" withExtension:#"html"];
NSString *html = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil];
NSURL *baseUrl = [NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]];
[webView loadHTMLString:html baseURL:baseUrl];

Resources