UIWebview not fetching the linked CSS resource - ios

I am trying to load a local html content into uiwebview. I already have the html content ready with me, and i am adding a reference a online css file to the content by the mechanism as shown in the code below. However, the problem i am facing is when i load the webview, there is no styling. Also, I verified in the Charles Proxy, there is no outgoing call for fetching iphone.css file to my base server. Ideally, while loading a html page, uiwebview should get all the referred resources, but for some reason unknown to me, it is not fetching the css file.
Please review and let me know if there is some issue with my code that i am not able to identify here?
NSString* htmlString = #"<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\"> <html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"he\" lang=\"he\"><head><link type=\"text/css\" href=\"http://<base_server>/resources/iphone.css\" /><meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" /></head><body>%#</body></html>";
UIWebView *webview = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];
NSLog(#"%#",[NSString stringWithFormat:htmlString,entry.body]);
[webview loadHTMLString:[NSString stringWithFormat:htmlString,entry.body] baseURL:[NSURL URLWithString:#"http://<base_server>/"]];
[self addSubview:webview];

It's hard to tell if there are any typos in the HTML with all of the double quote escaping going on in that string. Why don't you pull the HTML out of the string and into a file, read that file in and place your content in the body. There may be a mistake with the markup in that string. This would allow you to read it easier in a seperate file.
Something like:
NSError *error;
NSStringEncoding encoding;
NSString *htmlFilePath = [[NSBundle mainBundle] pathForResource: #"Sample"
ofType: #"html"];
NSString *htmlString = [NSString stringWithContentsOfFile:htmlFilePath
usedEncoding:&encoding
error:&error];
NSString *html = [NSString stringWithFormat:htmlString, entry.body];
NSString *path = [[NSBundle mainBundle] bundlePath];
NSURL *baseURL = [NSURL fileURLWithPath:path];
[webview loadHTMLString:html baseURL:baseURL];

Related

Loading html file in UIWebvView called 'index.html?mod=eurt'

Let me start by saying I know how to load up an html file, but I need to load up index.html?mod=eurt. So the file itself is called index.html, but I need to load it with ?mod=eurt at the end. (the html file is shared between different applications and changing "?mod=X", will tell the file exactly what to do.
The problem is, I cannot figure out how to do this with the way I am loading up the html into the webview (I'm rather new at iOS development, so there may exist an easy way I don't know about). Here's what I have to load a local html file so far:
NSString *htmlFile=[[NSBundle mainBundle] pathForResource:#"index" ofType:#"html"
inDirectory:nil];
NSString *htmlString=[NSString stringWithContentsOfFile:htmlFile
encoding:NSUTF8StringEncoding error:nil];
[webView loadHtmlString:htmlString baseURl:nil];
I tried changing the ofType:#"html" to ofType:#"html?mod=eurt", but I didn't really expect that to work, and it didn't.
I've come from the land of Android where I simply did webView.loadUrl("file:///android_asset/index.html?mod=eurt");
There must be an easy way to do this in iOS, right?
You can do something like this
NSString* bundlePath = [[NSBundle mainBundle] pathForResource:#"index" ofType:#"html"];
NSString* strUrl = [[urlToLoad absoluteString] stringByAppendingString:#"?mod=eurt"];
NSURL* urlToLoad = [NSURL URLWithString:strUrl];
[webView loadRequest:[NSURLRequest requestWithURL:urlToLoad]];

Load Image from UIwebview fail

I am using Webview to load a image file stored in my app Library Directory, first i tried use resourcePath, and bundle path
NSString * html = [[NSString alloc] initWithFormat:#"<img src=\"file://%#\"/>", filename];
[self.webView loadHTMLString:newhtml baseURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] resourcePath]]];
the problem is no matter what i set in the baseUrl, i can not load the image correctly , i also tried filePath:
[self.webView loadHTMLString:newhtml baseURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] filePath]]];
but if i set the absolute path of the image file in the html , all the things is ok, i wonder why?
Have a look at this post: Using HTML and Local Images Within UIWebView
The top answer clearly said that using file: paths to refer to images does not work with UIWebView.
All you need to do is to pass the basepath. Example:
NSString *path = [[NSBundle mainBundle] bundlePath];
NSURL *baseURL = [NSURL fileURLWithPath:path];
[webView loadHTMLString:htmlString baseURL:baseURL];
Then just need to reference it like this: <img src="myimage.png">

How to append javascript into UIWebView in iOS?

I'm trying to change Font in UIWebView.
I tried with the following codes to change Font.
NSString *path = [[NSBundle mainBundle] bundlePath];
NSString *cssPath = [path stringByAppendingPathComponent:#"test.css"];
if(!cssPath)
{
NSLog(#"NO");
}
NSString *js = [NSString stringWithFormat:#"var cssChild = document.createElement('link');"
"cssChild = 'text/css';"
"cssChild = 'stylesheet';"
"cssChild = '%#';", cssPath];
js = [NSString stringWithFormat:#"%# document.getElementsByTagName('head')[0].appendChild(cssChild);", js];
[webViewOfInitial stringByEvaluatingJavaScriptFromString:js];
I append css file into header of loaded page from internet.
my css file is following like that.
#font-face {
font-family: 'Zawgyi-One';
font-style: normal;
src: local('Zawgyi-One');
}
body {
font-family: 'Zawgyi-One';
}
However when i retrieve html tags when finished page loaded , i can't find my above css tags.
So how do i append that css file into current page of UIWebView's header tags?
Or is there any others tutorials for UIWebView Font change?
Thanks.
You are going in the right direction but you haven't mentioned , which delegate method you are trying to make changes in.
you can have your javascript in a .js file as you might make changes to it later and it will be really cumbersome to reflect the changes through code (instead of a js file).Try Using this :-
you have to load the HTML into the view with the correct baseURL parameter to use relative paths or files in UIWebView.
NSString *path = [[NSBundle mainBundle] bundlePath];
NSURL *baseURL = [NSURL fileURLWithPath:path];
[webView loadHTMLString:htmlString baseURL:baseURL];
baseURL accepts an NSURL object as the file reference.You can specify a local URL or a web one as your needs leads you to.Append the css file in webViewDidFinishLoad:(UIWebView *)webView as you have done.
NOTE:-Select the .js file and uncheck the bullseye column indicating it as compiled code.(You will just get a warning if you don't).
Here's aUIWebview Javascript Tutorial that might get you going.

How to localize html strings in iOS

I want to write html code locally and show it in UIWebview ,so for this ,I have taken a simple html code ,as below and created a .html extension file..
<html>
<body>
<h1>My First Heading</h1>
<p>My first paragraph.</p>
</body>
</html>
I added this file into my xcode project ,now I want to load this file inside webview ,so I have written this code
[m_websiteView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"test" ofType:#"html"]isDirectory:NO]]];
but the output I get when I run this code in UIWebView is same htmlcode, I cant figure out what the problem is..but when I tried using this code
NSString *path = [[NSBundle mainBundle] bundlePath];
NSURL *baseURL = [NSURL fileURLWithPath:path];
NSString *testString = #"<h1><i>This is an image using reference in HTML</i></h1>"
"<img src='active.png'></img>";
[m_websiteView loadHTMLString:testString baseURL:baseURL];
this works perfect.also I want to localize the strings in this html code,so in the above code,how can I localize this string "This is an image using reference in HTML"..
Regards
Ranjit
Add localization, use this
NSString *testString = [NSString stringWithFormat:
#"<h1><i>%#</i></h1><img src='active.png'></img>",
NSLocalizedString(#"This is an image using reference in HTML", #"htmlText")];
and specify the localize strings in the file Localizable.strings in the correct language directory.
You can also localized entire (HTML) files with the locale directory scheme.
HTML syntax is
<html>
<head>
</head>
<body>
</body>
</html>
after that you can preview your html file using something like that in your view controller
CGRect webFrame = CGRectMake(0.0, 0.0, 320.0, 460.0);
UIWebView *webView = [[UIWebView alloc] initWithFrame:webFrame];
[webView setBackgroundColor:[UIColor whiteColor]];
NSString *urlAddress = #"http://www.istoselidas.gr";
NSURL *url = [NSURL URLWithString:urlAddress];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[webView loadRequest:requestObj];
[self addSubview:webView];
[webView release];
just replace url with your html file

Run HTML5/CSS3 in UIWebView

I've a problem that i couldn't solve, i wanna run the Apple - HTML5 Showcase - Gallary
in UIWebView inside my ipad app.
I've tried a local html file:
NSString *strr = [[NSBundle mainBundle] pathForResource:#"photo-gallery" ofType:#"html"];
NSString *str = [[NSString alloc] initWithContentsOfFile:strr encoding:NSASCIIStringEncoding error:nil];
NSLog(#"%#",str);
[webView loadHTMLString:str baseURL:nil];
and also a server html file, but it didn't work, can anyone help me, i wanna show an html5/css3 in UIWebView.
thx in advance.
hi this is what you can do
UIWebView *aWebView = [[UIWebView alloc] initWithFrame:webFrame];
//getthe path of the local html file
NSURL *baseURL = [NSURL fileURLWithPath:fileNamePath];
NSURLRequest *aRequest = [NSURLRequest requestWithURL:baseURL];
//load the html file into the web view.
[aWebView loadRequest:aRequest];
//add the web view to the content view
[view addSubview:aWebView];
For server html you simply skip the second line, and put the address.
Hope it helps
You can take a look at phonegap. Its a complete framework for building html5 apps inside a UIWebview with native capabilities.
See: http://www.phonegap.com

Resources