UIWebView loadHTMLString:baseURL method does not work - iphone

I want to show a local html string content in a UIWebView using this :
[webView loadHTMLString:value baseURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]]];
I dont know what's going wrong, but the web view remains white
thank's for help

Try this
NSString *html = [[NSString alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"document" ofType:#"html"]];
[webView loadHTMLString:html baseURL:nil];

Related

uiwebview is not showing PDF properly as showing in safari

I am having a strange issue. UIwebview is not showing PDF file properly as it shows when I load the PDF file from web URL or in safari browser.
http://www.winsolz.com/temp/2.jpg
I am using the following code to open the PDF from local system.
NSString *pathFOrPDF = [[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:#"Page%#", strPDFNumber] ofType:#"pdf"];
NSURL *targetURL = [NSURL fileURLWithPath:pathFOrPDF];
NSURLRequest *request = [NSURLRequest requestWithURL:targetURL];
[myWebview loadRequest:request];
[myWebviewLandscape loadRequest:request];
Thanks.
I don't think it would fix your case but I had a pdf showing in safari and not showing in a UIWebView and my problem was that the file extension was not correct. I appended a .pdf at the end of the local file url and started working.
I've noticed that safari check also content types so maybe it is working on safari as you provide a remote url?
this might help.
UIWebView *webView = [[UIWebView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
webView.backgroundColor = [UIColor redColor];
NSString*fileName= #"About_Downloads";
NSString *path = [[NSBundle mainBundle] pathForResource:fileName ofType:#"pdf"];
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL fileURLWithPath:path isDirectory:NO]];
[webView setScalesPageToFit:YES];
[webView loadRequest:request];
[self.view addSubview:webView]

what is the wrong when loading local resource files in webview in iphone

I am trying to load local css and html files into WebView , but the web view displays the content of css file in webview as you see in the following image ..
objective-c code:
NSString *htmlPath= [[NSBundle mainBundle] pathForResource:#"index" ofType:#"html" inDirectory:NO];
//[webView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:htmlPath]]];
// load css styles
NSString *cssPath = [[NSBundle mainBundle] pathForResource:#"css" ofType:#"css"];
NSData *cssData = [NSData dataWithContentsOfFile:cssPath];
NSString *cssString = [[NSString alloc] initWithData:cssData encoding:NSASCIIStringEncoding];
// load js
NSString *jsPath = [[NSBundle mainBundle] pathForResource:#"MyJS" ofType:#"js"];
NSData *jsData = [NSData dataWithContentsOfFile:jsPath];
NSString *jsString = [[NSString alloc] initWithData:jsData encoding:NSASCIIStringEncoding];
// compose full html page
NSString *pageContent = [NSString stringWithFormat:#"%#%#%#", cssString, jsString, htmlPath];
[webView loadHTMLString:pageContent baseURL:[NSURL URLWithString:#""]];
what is the problem ?
You must not use
[webView loadHTMLString:pageContent baseURL:[NSURL URLWithString:#""]];
instead loadHTMLString try to use stringByEvaluatingJavaScriptFromString
And if you have any problem check the following post. I think you will get it easily
UIWebView and local css file
I have not used it for CSS but hope this will help you...Good Luck...

iPhone list style like UL LI

Is there a way to create a list style like the UL LI for HTML on the iPhone in a textView? Why i want this is because when i have a long text there will be minimal 2 rules text show.
Example:
Hello i'm a monkey
from the zoo!
Hello i'm a monkey
from the zoo!
Use UIWebView, you can pass it arbitrary HTML as a NSString.
the best way that worked for me is to use a UIWebView and add the text as HTML.
this is apple recommendation for rich text.
UIWebView *webview = [[UIWebView alloc] initWithFrame:CGRectMake(0, 130,300 ,230)];
webview.backgroundColor = [UIColor clearColor];
webview.opaque=NO;
webview.delegate=self;
NSString *text = #"<ul><li>item 1</li><li>item 2</li></ul>";
NSString *cssPath = [[NSBundle mainBundle] pathForResource:#"style" ofType:#"css"];
NSString *path = [[NSBundle mainBundle] bundlePath];
NSURL *baseURL = [NSURL fileURLWithPath:path];
NSString *html =[NSString stringWithFormat:#"<!DOCTYPE html><html lang=\"en\"><head><link rel=\"stylesheet\" href=\"%#\" type=\"text/css\" /></head><body>%#</body></html>",cssPath,text];
[webview loadHTMLString:html baseURL:baseURL];
[self addSubview:webview];
And don't forget to create the "style.css" file in your project.
good luck
shani

Browsing through folders in a UIWebView

My question is the following. I'm a bit confused about the path of resources to be loaded in a UIWebView. I'ld like to make my links active. For the moment the WebView isn't getting resources (.css, .html) that are in other folders while the link between them is correct. Do you know how to fix this ?
I'm currently doing like this
NSString* path = [[NSBundle mainBundle] pathForResource:#"index" ofType:#"html"];
NSString* htmlString = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
[WebView loadHTMLString:htmlString baseURL:[NSURL fileURLWithPath:path]];
Thanks for reading me !
Can you try like this, it may be useful:
[webview loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"webPage" ofType:#"html"]]]];

stringWithContentsOfFile works, but requestWithURL doesn't

For some reason, the first method doesn't display anything, while the second does:
//Common
NSString *path=[[NSBundle mainBundle] pathForResource:#"about" ofType:#"html"];
//Method1
[webView loadRequest:[NSURLRequest requestWithURL: [NSURL fileURLWithPath: path isDirectory: NO]]];
//Method 2
NSString *HTML=[NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:NULL];
[webView loadHTMLString:HTML baseURL:[NSURL URLWithString:[[NSBundle mainBundle] bundlePath]]];
//Common
UIViewController* controller=[webView controllerWithTitle:#"Help"];
[delegateRef.navController pushViewController:controller animated:YES];
On the other hand, I also tried loading it using a URL as well. Similarly, the first method didn't display anything while the second did.
//Common
NSURL *url=[[NSBundle mainBundle] URLForResource:#"about" withExtension:#"html"];
//Method 1
[webView loadRequest:[NSURLRequest requestWithURL:url]];
//Method 2
NSString *HTML=[NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:NULL];
[webView loadHTMLString:HTML baseURL:[NSURL URLWithString:[[NSBundle mainBundle] bundlePath]]];
Strangely, if I change the URL as follows, then both methods work (of course loading the string doesn't show css):
NSURL *url=[NSURL URLWithString:#"http://www.google.com"];
controllerWithTitle is defined in a category as follows:
-(UIViewController*) controllerWithTitle:(NSString*)title{
UIViewController *vc=[[[UIViewController alloc] init] autorelease];
vc.view=self;
vc.title=title;
return vc;
}
It turns out that the CSS was causing the entire page to not display!