loading local html5 in uiwebview with anchor - ios

I have the following code to load a local html file in an iOS app.
[WebView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"index" ofType:#"html"]isDirectory:NO]]];
The above works, however I need to load an anchor as this is a single page web app embedded in a UIWebview.
Basically how do I load index.html#settings for instance instead of just index.html.

This worked for me.
NSString *basePath = [[NSBundle mainBundle] pathForResource:#"page.html" ofType:nil];
NSURL *baseURL = [NSURL fileURLWithPath:basePath];
NSURL *fullURL = [NSURL URLWithString:#"#anchor1" relativeToURL:baseURL];
// Now do whatever with this fullURL
The # is converted to %23 in [NSURL fileURLWithPath:basePath], but URLWithString:(NSString *)URLString relativeToURL:(NSURL *)baseURL; not.

Related

How to load HTML file in webview with query string in iOS app device using objective c

I have added html file in my project root, normally it is being loaded in web view easily. I am using below code:
NSString *filepath = [[NSBundle mainBundle] pathForResource:#"test" ofType:#"html"];
NSURL *url=[NSURL fileURLWithPath:filepath];
[webView loadRequest:[NSURLRequest requestWithURL:url]];
It works fine.
Now I am to set query strings with path, to do this I have added below code:
NSURL *url=[NSURL fileURLWithPath:[NSString stringWithFormat:#"%#?FirstName=dd&LastName=ee",filepath];
[webView loadRequest:[NSURLRequest requestWithURL:url]];
It works in Mac and Desktop but not working in iOS devices and simulator.
Thanks in advance.

Load text+pdf file in uiwebview

I want to show some text and pdf file in a webview at a time.
is there any possibility..
i had tried using
NSString *htmlString = [NSString stringWithFormat:#"<p>some text which i need to show above pdf</p><img src=\"file://%#\">",#"FR.pdf"];
NSString *bundleP = [[NSBundle mainBundle] resourcePath];
NSURL *fileURL = [NSURL fileURLWithPath:bundleP];
[pdfWebView loadHTMLString:htmlString baseURL:fileURL];
This shows the text and only the first page of pdf file,..
Thanks in advance
Use another way to load pdf and above the webView add a UILabel to show the text. As you see pdf is treated as a image so that only one page is showed.
NSString *filePath = [[NSBundle mainBundle] pathForResource:#"FR" ofType:#"pdf"];
NSURL *filePathURL = [NSURL fileURLWithPath:filePath];
[self.webView loadRequest:[NSURLRequest requestWithURL:filePathURL]];

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

How do you display a PDF file form the local directory on an iPad?

How do you display a PDF file form the local directory on an iPad?
Loading it into a WebView shows up blank?
NSString *filePath = [[NSBundle mainBundle] pathForResource:#"display" ofType:#"pdf"];
NSURL *pdfURL = [NSURL fileURLWithPath:filePath];
NSURLRequest *request = [NSURLRequest requestWithURL:pdfURL];
[self.webDisplay loadRequest:request];
Where "self.webDisplay" is a UIWebView. The PDF doesn't show up, just a blank webview.

Resources