I have a header string, footer string and body of the HTML page. I need to programmatically include some text in the body and then I need to input all this data in a UIWebView to load the page. I need to input the final HTML string into a UIWebView controler, so that it will launch the page I designed. Could someone please share your ideas how can I achieve this?
Thanks!
You mean something like:
NSString *html = [NSString stringWithFormat: #"<html><head><style>body{background-color:black}</style></head><body>the body goes here</body>"];
UIWebView *myWebView = [[UIWebView alloc] initWithFrame:CGRectMake(0.0,76.0,320.0,404.0)];
[myWebView loadHTMLString:html baseURL:nil];
If you, for example, have head stored in headString and body in bodyString you could combine them with:
[NSString stringWithFormat: #"<html>%#%#</html>", headString, bodyString];
Related
I have a list of RSS feeds and I need to display the detail of each feed in iPhone. I got all RSS feeds from the server which I'm displaying in tableview. Now on selecting a row I need to display the discription of RSS Feed which is coming from Server in HTML content like:-
<img border=\"0\" hspace=\"10\" align=\"left\" style=\"margin-top:3px;margin-right:5px;\" src=\"http://timesofindia.indiatimes.com/photo/4881843.cms\" />Cadila Pharmaceutical will seek the govt's nod in two days for initiating clinical trials for a vaccine against swine flu.<img width='1' height='1' src='http://timesofindia.feedsportal.com/c/33039/f/533968/s/1f181b11/mf.gif' border='0'/><div class='mf-viral'><table border='0'><tr><td valign='middle'><img src=\"http://res3.feedsportal.com/images/emailthis2.gif\" border=\"0\" /></td><td valign='middle'><img src=\"http://res3.feedsportal.com/images/bookmark.gif\" border=\"0\" /></td></tr></table></div><br/><br/><img src=\"http://da.feedsportal.com/r/133515347892/u/0/f/533968/c/33039/s/1f181b11/a2.img\" border=\"0\"/><img width=\"1\" height=\"1\" src=\"http://pi.feedsportal.com/r/133515347892/u/0/f/533968/c/33039/s/1f181b11/a2t.img\" border=\"0\"/>
How do I display this HTML Content in our iPhone UI, as this will contain text,hyperlink and images.
Is it proper way to use UIWebview in this case, as UIWebView is heavy weight.
Please read this blog : http://www.raywenderlich.com/2636/how-to-make-a-simple-rss-reader-iphone-app-tutorial,
It is useful for you.
you can do something like below.
first of all
descLbl.text=[self flattenHTML:descLbl.text];//descLbl.text is a text in which you are getting that HTML description...
now in flattenHTML method write like below...
- (NSString *)flattenHTML:(NSString *)html {
NSScanner *thescanner;
NSString *text = nil;
thescanner = [NSScanner scannerWithString:html];
while ([thescanner isAtEnd] == NO) {
[thescanner scanUpToString:#"<" intoString:NULL];
// find end of tag
[thescanner scanUpToString:#">" intoString:&text];
html = [html stringByReplacingOccurrencesOfString:[NSString stringWithFormat:#"\n"] withString:#" "];
// replace the found tag with a space
//(you can filter multi-spaces out later if you wish)
html = [html stringByReplacingOccurrencesOfString:[NSString stringWithFormat:#"%#>", text] withString:#" "];
} // while //
return html;
}
let me know it is working or not..
Happy coding!!!!
If you are planning to show HTML entities as it is, i would suggest to go with UIWebView. You can pass this HTML as a string in method loadHTMLString:
If you want the user to view the content as a web page, then loading the url in UIWebView is the best and easiest way.
Like this:
UIWebView *webView = [[UIWebView alloc] init];
NSURL *pageurl = [NSURL URLWithString:http://www.google.com];
NSURLRequest *request = [NSURLRequest requestWithURL:pageurl];
[webView loadRequest:request];
You can simply use RSSKit library.
Is it possible to cache a html page at applicationDidFinishLauchingOptions() method so that when I want to implement UIWebView the delay taken for loading that html page can be avoided?
Any help would be appreciated.
You can get the data at the time of loading of app itself, and then convert that to NSString or keep that as NSData. After that later when you want to load the page you use like -
For NSString -
NSString *htmlString = // string content
[webView loadHTMLString:htmlString baseURL:baseURLString];
for NSData -
NSData *htmlData = // data content
[webView loadData: MIMEType:#"" textEncodingName:#"" baseURL:#""];
In my application I am loading HTML String which is parsed from the json file the string already contain font color and font size but I need to change the color and size of the font. To replace that I have already use the followings code but its not working
NSString *webString = [[NSString alloc] initWithFormat:#"document.getElementsByTagName('body')[0].style.webkitTextSizeAdjust= '%d%%'",
textFontSize];
[web stringByEvaluatingJavaScriptFromString:webString];
Is there is any other way to change it.If yes means please suggest me the answer.
try this,
[webView loadHTMLString:[NSString stringWithFormat:#"<div id ='foo' align='justify' style='font-size:14px; font-family:helvetica; color:#ffffff';>%#<div>",yourString] baseURL:nil];
it may helps you....
You can try this java script in to your webview's delegate webViewDidFinishLoad method
NSString *setTextSizeRule = [NSString stringWithFormat:#"addCSSRule('body', '-webkit-text-size-adjust: %d%%;')", currentTextSize];
NSString *changeColor = [NSString stringWithFormat:#"addCSSRule('html, body, div, p, span, a', 'color: #000000;')"];
[webView stringByEvaluatingJavaScriptFromString:setTextSizeRule];
[webView stringByEvaluatingJavaScriptFromString:changeColor];
You can do so many changes using java script in webview's did finish load method.
Let me know if you still able to not solve
i have searched the net, but the given solutions doesn't fit my needs..
So i have a webview, and my webview loads data like this:
NSString *path = [[NSBundle mainBundle] bundlePath];
NSURL *baseURL = [NSURL fileURLWithPath:path];
[_webView loadHTMLString:htmString baseURL:baseURL];
That html string has anchor tags with href e.g - < a href=http://url.com/ ... /> and i trigger some events on shouldStartLoadWithRequest.
Now the problem is i don't know how to remove the grey selection when this attribute is pressed. I saw people are doing this:
<a href=http://yourlink.com/ style = "-webkit-tap-highlight-color:rgba(0,0,0,0);">
and some similar things, but i want to do it other way. By other way i'm talking that i don't want to change my htmlString, because it may have a lot of them, and injecting style in every seems not the perfect solution..
So i tried doing like this:
[_webView stringByEvaluatingJavaScriptFromString:#"document.documentElement.style.webkitTapHighlightColor = \"rgba(0,0,0,0);\""];
after i load html string, but it doesn't work.
So anybody has some suggestions, or knows what am i doing wrong?
EDIT:
Or maybe a simple injection of javascrypt for the behavior i want?
Thanks in advance!!!
This code should do the trick:
- (void)webViewDidFinishLoad:(UIWebView *)webView
NSString *js = #"var styleNode = document.createElement('style');\n"
"styleNode.type = 'text/css';\n"
"var styleText = document.createTextNode('a {-webkit-tap-highlight-color:rgba(0,0,0,0)}');\n"
"styleNode.appendChild(styleText);\n"
"document.getElementsByTagName('head')[0].appendChild(styleNode);\n";
[_webView stringByEvaluatingJavaScriptFromString:js];
}
I'm trying to extract some XML from a UIWebView, and not having much luck. This is what the entire document looks like:
<?xml version="1.0" encoding="utf-8"?>
<authResponse xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<firstName>Name here</firstName>
<lastName>Name here</lastName>
<membershipTier>Membership level</membershipTier>
<errorMessage />
<isValid>1</isValid>
</authResponse>
I have tried a number of variations of the stringByEvaluatingJavaScriptFromString method, but this is the only one that will return anything for me:
[webView stringByEvaluatingJavaScriptFromString:#"document.documentElement.textContent"]
However, I need the raw XML, not the text content. Any suggestions?
EDIT: I have control over the XML, so it can be modified if need be.\
EDIT 2: As requested, code I am using to load the request is below. Note that this request is for a login screen. Once the user has entered their credentials, the page after that is the one I am trying to extract the results from. I determine which is the current page in webViewDidFinishLoad by testing the current mainDocumentURL for the UIWebView object.
NSString* urlString = kLoginURL;
NSURL* url = [NSURL URLWithString:urlString];
NSURLRequest* request = [NSURLRequest requestWithURL:url];
UIWebView* tmpWebView = [[UIWebView alloc] init];
self.webView = tmpWebView;
[tmpWebView release];
self.webView.delegate = self;
[self.webView loadRequest:request];
Just wrote a little test program to figure this out (fun!). It loads the UIWebView contents via:
NSString * data = #"<?xml version=\"1.0\" encoding=\"utf-8\"?><authResponse xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"><firstName>Name here</firstName><lastName>Name here</lastName><membershipTier>Membership level</membershipTier><errorMessage /><isValid>1</isValid></authResponse>";
[self.webview loadHTMLString: data baseURL: nil];
I then passed the string document.getElementsByTagName('body')[0].innerHTML to stringByEvaluatingJavaScriptFromString
and it returns what you want:
<authresponse xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><firstname>Name here</firstname><lastname>Name here</lastname><membershiptier>Membership level</membershiptier><errormessage><isvalid>1</isvalid></errormessage></authresponse>
i'd grab the whole html as a string on a seperate thread, or use this as the basis to load the webview:
NSString *xmlString = [NSString stringWithContentsOfURL:yourURL]
You can try using kissxml to retrieve the whole xml structure.
Or you can just save the response into a string from your NSURLRequest or if you are using other libraries to do a http connection
NSURL *url = [[NSURL alloc] initWithString:#"http://www.w3schools.com/xml/note.xml"];
NSString *myString = [[NSString alloc] initWithData:[NSData dataWithContentsOfURL:(NSURL*)url] encoding:NSISOLatin1StringEncoding];
The code above is a sample on how you can retrieve the xml structure. This is much easier than using uiwebview. The sample xml encoding is using ISO-8859-1 so you have to set the correct encoding so that the data will be readable
I would suggest going with Nik's suggestion - get the request URL and get its content in an NSString.
OR you can send a request to the login URL (you are already intercepting the requests being sent) and get a response in a string.
If you want to read the entire body tag's content or the html and body tag's content, you might just do this
NSString *html = [yourWebView stringByEvaluatingJavaScriptFromString:#"document.body.innerHTML"];
or
NSString *html = [yourWebView stringByEvaluatingJavaScriptFromString:#"document.all[0].innerHTML"];
You can use either of these as per your need:
NSString *html1 = [webView stringByEvaluatingJavaScriptFromString:
#"document.body.innerHTML"];
NSString *html2 = [webView stringByEvaluatingJavaScriptFromString:
#"document.documentElement.outerHTML"];