iOS: Displaying an html file in a UIWebView

I seem to be using web views a fair bit in my apps. I guess the reason being that you have control of the formatting for what you want displayed and how you want it to look, over the other ways to get text into your apps.
This quick code snippet will allow you to feed into your apps an html page, from the internet.
First make sure you have defined the webView in your code once you have added it to your view in Storyboard.
Then I added to
- (void)viewDidLoad{
the following code (which is on one line)
[self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://200ml.co.uk/about.html"]]];
You can break it down and use the following if you want to have code that you may find easier to read:
NSURL *url = [NSURL URLWithString:@"http://200ml.co.uk/about.html"];
NSURLRequest *requestURL = [NSURLRequest requestWithURL:url];
[webView loadRequest:requestURL];
If you use the code (either) then you will have your view loading the page you choose. If you are using that code then your information is loading from a webpage. It is helpful if you add a few more lines of code so that the user can see if there is a page loading, or an error message. To have a nice spinner loading in the activity bar you will need the following.
This code manages Network activity:
- (void)webViewDidStartLoad:(UIWebView *)webView
{
// starting the load, show the activity indicator in the status bar
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
}
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
// finished loading, hide the activity indicator in the status bar
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
}
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
// load error, hide the activity indicator in the status bar
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
// report the error inside the webview
NSString* errorString = [NSString stringWithFormat:
@"<html><center><font size=+1 color='black'><br><br><br>An error occurred:<br>%@</font></center></html>",
error.localizedDescription];
[self.webView loadHTMLString:errorString baseURL:nil];
}
Don’t forget that you then need to assign the delegate so that the webview understands, so go back into Storyboard and click on the webView, holding control so that you can drag the connection, and release it over the view Controller choosing Delegate.
For further explanation and an example of the code being used you can read up on the Apple Developer site.
No related posts.

Hey, I just made an app with the storyboard first I made the tab controller then each of them shows a new page, but one of those pages I added a UIButton and want to link it with a UIWebViewer to open an HTML but I really don’t get how do to that, I followed you’re instructions and copied your code but it didn’t worked for me. Hope you understand what I mean. Hear from you soon
If you are making it open with a button, be sure to write your button code, and link it to that action. So the code will be within something like
- (IBAction)buttonOpensWebPage:(id)sender {
NSURL *url = [NSURL URLWithString:@"http://200ml.co.uk/about.html"];
NSURLRequest *requestURL = [NSURLRequest requestWithURL:url];
[webView loadRequest:requestURL];
}
And also you linked it all up in the interface? It knows what webview to load the page into? Hope that helps. (In my example the UIWebView is called webView, you may have called yours something different.)
May I ask how to load “https://” web page in your program
just the same but it will need log in information