iOS - WebView and string - ios

i have a string called htmlString that contains some informations formatted in html. I need to put these info into a webView that load the entire html string, with color and fonts. And i need to know the string height. How can i do?

You want to do something like:
[_webView loadHTMLString:htmlStr
baseURL:[NSURL fileURLWithPath:path]];
You can view the docs here: http://developer.apple.com/library/ios/#documentation/uikit/reference/UIWebView_Class/Reference/Reference.html
This will load your HTML into the webview and use the path you provide as a root for other documents. In other words, your html string could reference other files (css, javascript, etc...) and the baseURL is used to locate the urls that use relative paths.
EDIT:
To get the height, you could assign the UIWebView's delegate as it has a webViewDidFinishLoad: method to tell you when the page is rendered. Then you could execute javascript on the page to determine the final height using UIWebView's method - stringByEvaluatingJavaScriptFromString:
This answer also seems pretty relevant: How to determine UIWebView height based on content, within a variable height UITableView?

NSString *htmlStrings="Hello I m here";
[_webView loadHTMLString:htmlStrings
baseURL:[NSURL fileURLWithPath:null]];

Related

How to set http header in uiwebview if loading view with loadHTMLString

I have a UIWebView that i am loading using loadHTMLString and i need to set cookies on the request header. I know how to do this using loadRequest but not loading the webview with loadHTMLString I don't have the request object. Has anyone done anything like this?
There will not be any difference between loading web view from loadRequest vs loadHTMLString method, shouldStartLoadWithRequest method will be called in both cases. You can override your headers there and add header as per your requirements.

Loading hidden/offscreen UIWebView

Actually I have two related questions here, about different use cases of loading requests in a UIWebView.
Is it safe to call - [UIWebView loadRequest:] on a web view that is inserted in the view hierarchy and its hidden property or the one of its superview is set to YES?
Is it safe to call - [UIWebView loadRequest:] on a web view that is not inserted in the view hierarchy?
In particular I'm interested whether it is considered to be a good practice to load request in a UIWebView that is not visible, and whether the delegate assigned to the instance of UIWebView will be notified once the request succeeds/fails. The reason I'm asking is that UIWebView class reference says "create a UIWebView object, attach it to a window, and send it a request to load web content", where the part telling that a UIWebView should be attached to a window makes me doubt if the above approaches are reliable.
I have successfully used [UIWebView loadRequest:] with objects that are not in the view hierarchy. I expect that the class reference just assumes that the view will be displayed as it's probably the most common use case.
Yes it is safe to call [UIWebView loadRequest:] on a web view that is inserted in the view hierarchy.Because you are going to use web in the view.Also if you just give the URL in the program,it is enough to get.
The following code for [UIWebView loadRequest] is
NSString *strurl =#"http:www.google.com";
NSURL *url=[NSURL urlWithString:strurl];
NSURLRequest *urlrequest =[NSURLRequest requestWithUrl:url];
[webView loadRequest:urlrequest];
Even it is safe to call [UIWebView loadRequest:] on a web view that is not inserted in the view hierarchy.Because you can dynamically create the view and write the code for web view through the program.
It works. The approach is completely reliable as long as you are not using any private API and following HIG. It is not a bad practice as long it suits to your requirement. If there is a hidden property available to you for UIWebView then of-course you can hide the webView as per your requirements.
About your below query, it is written in the documentation as per the sentence context.
The reason I'm asking is that UIWebView class reference says "create a
UIWebView object, attach it to a window, and send it a request to load
web content", where the part telling that a UIWebView should be
attached to a window makes me doubt if the above approaches are
reliable.
The full context is below, which clearly means that to display a webpage in your application using UIWebView, you have to do it in the mentioned way.
You use the UIWebView class to embed web content in your application.
To do so, you simply create a UIWebView object, attach it to a window,
and send it a request to load web content.

UIWebView's _UIWebViewScrollView

I created an instance of UIWebView called myWebView. I populated myWebView with the content from loadHTMLString, added several UIImageViews and UIViews. It worked as expected: UIImageViews and UIViews are all on top of the webview's content from html string.
I then archived and un-archived myWebView using NSKeyedArchiver and NSKeyedUnarchiver. After un-archived, only the html string content is visible. All other UIViews and UIImageViews were moved to back. So they are not visible. An NSLog of myWebView.subviews before archiving and after un-archiving showed that there a _UIWebViewScrollView was moved from the first object in the subviews to the last object. Note that only the _UIWebViewScrollView order was changed.
Not sure if it was by design by the SDK but to restore the views order and make them visible as normal, I did this:
[myWebView sendSubviewToBack:[myWebView.subviews lastObject]];
Question: Will doing this violate any of Apple's rule as I am not sure what if the _UIWebViewScrollView is considered as Apple's private thing. But since I did not touch that _UIWebViewScrollView.
Well, as far as rules go, you're fine.
HOWEVER: Since the UIWebViewScrollView is an internal member of the UIWebView, if they change the workings of UIWebView in a different iOS version, your code might get screwed up.
To play it a little on the safer side, you should try to set tags on each of the subviews you add, and then do:
[myWebView bringSubviewToFront:[myWebView viewWithTag:100]];
[myWebView bringSubviewToFront:[myWebView viewWithTag:101]];
[myWebView bringSubviewToFront:[myWebView viewWithTag:102]];
... etc (remember to use the tags that you used for your subviews)
On another note, messing with the UIWebView might result in troubles later on, there might be other ways to accomplish what you're trying to do, ways that are less risky

UIWebView won't goBack after loading HTML in iOS

I'm having this problem in my iPhone app: I have a webView which is first loaded with an HTML string. After the user clicks a link and it loads the requested page, the webView won't go back when I call the method [UIWebView goBack]; I suppose webView doesn't cache the HTML string. Is there any way I can make webView cache my HTML string without having to save it in a NSString myself?
You can use the canGoBack property and if you can't go back, reload the UIWebView with the original html. If the user has navigated forward using links then the canGoBack property will return YES and a goBack can be initiated on the UIWebView. The _htmlString is a member variable that is set when the UIWebView is initialized using an HTML string. -rrh
- (void)goBack
{
if (_htmlString && ![_browserWebView canGoBack]) {
[_browserWebView loadHTMLString:_htmlString baseURL:nil];
return;
}
[_browserWebView goBack];
}
Try to create a NSURLRequest from the file URL and use loadRequest instead of loadhtmlString
NSURL *htmlFileUrl = [[NSBundle mainBundle] URLForResource:#"index" withExtension:#"html"];
NSURLRequest *localRequest = [NSURLRequest requestWithURL:htmlFileUrl];
self.webView.delegate = self;
[self.webView loadRequest:localRequest];
This sounds like a your webview variable isn't properly linked up to the webview instance that you are using. Breakpoint at this call and check whether your webView variable is 'nil'.
If it is, make sure your webView in your XIB file is linked to an IBOutlet variable in Interface Builder. This is a common mistake and something I tend to forget when designing a new page for the first time.
This tutorial covers a LOT on how to build interfaces using Interface builder which i'm sure you're familliar with but for those that aren't it's also useful. It has some good screenshots which help illustrate what I mean by 'linking' better than me typing "click the little + icon and drag the little thingy on to the UI element" :)
http://www.icodeblog.com/2008/07/30/iphone-programming-tutorial-connecting-code-to-an-interface-builder-view/
EDIT
The only other thing i can think of is that you are overwriting your webView variable by re-initialising it somewhere (it is already initialised by the XIB) and therefore you're calling goBack on a webview that doesn't exist on the screen.

"loadHTMLString:baseURL:" iPhone SDK

I'm coding up my firs iPhone app..
How can I use "loadHTMLString:baseURL:" to send the user to a view called "pagetwo.m"?
To navigate from one view to another or to insert subview given solution is correct and following is the sample code to let you know, how to use delegate method of UIWebView loadHTMLString:baseURL:...
NSString *embedHTML = [NSString stringWithFormat:#"<HTML><HEAD><TITLE>Page 1</TITLE></HEAD><BODY><H2>HTML Code Editor</H2><H3>p1 - The Basic Page</H3>"
"<P>This is the basic webpage. The HTML tags, BODY, H2, H4, P and A were used, but since no attributes were added to these tags, they are left at their defaults. The links below are shown using their default colors.</P>"
"Google.com<BR>Yahoo<BR></BODY></HTML>"];
[webView loadHTMLString:embedHTML baseURL:nil];
I assume pagetwo.m is a UIViewController subclass, right? You can't send a user to view with loadHTMLString:baseURL: since that's a UIWebView's method to load html page into webview. You will need something like:
[self presentModalViewController:pagetwo animated:YES];
Or if it's a subclass of UIView, you need:
[self.view addSubview:pagetwo];
But before that you need to alloc/init pagetwo (and release it later) which will be hard to do since your class is also named "pagetwo" and you instance variable can't be called the same. you could call it (the instance variable) pagetwoview or something, but preferred way would be to follow Objective-C naming conventions and always name your classes starting with a capital letter.

Resources