Load text+pdf file in uiwebview - ios

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

Related

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

UIWebView can't find local images

NSString *htmlFile = [[NSBundle mainBundle] pathForResource:#"index" ofType:#"html"];
NSURL *baseURL = [NSURL fileURLWithPath:htmlFile];
NSString *htmlString = [NSString stringWithContentsOfFile:htmlFile
encoding:NSUTF8StringEncoding
error:nil];
myWebView = [[UIWebView alloc] init];
[myWebView loadHTMLString:htmlString baseURL:baseURL];
My code is like this. In index.html, I have
<img src="dog.gif"/>
<h1>index.html loaded</h1>
Both the index.html and dog.gif are stored in the PROJECT_ROOT/Resources/.
But my app works OK on the simulator, but fails to load dog.gif on my iPad. The text is showed on both. I cleaned the project, but that didn't work.

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.

loading local html5 in uiwebview with anchor

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.

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