How many lines of code do you have to write for displaying a webpage ‘in-app”.
3 methinks.

Wouldn’t be great if there is a built in class for presenting a webpage using “presentModalViewController”? Meet MKWebViewController.

Step 1:

Drop the attached three files into your project and write the following three lines on your action callback (buttonDidTap) or equivalent methods

MKWebViewController *controller = [[[MKWebViewController alloc] initWithNibName:@"MKWebViewController" bundle:nil] autorelease];
controller.url = @"";
[self presentModalViewController:controller animated:YES];

Step 2:

Nah, no step 2.

That’s it. On landscape mode, the controller even rotates and hides the toolbar. On portrait mode it brings back the toolbar and provides you a close button. The controller automatically handles any errors, and displays appropriate error messages, close the window when the user taps on the “Dismiss” button on the error message. What more, the controller can also display any iOS supported file format that includes, PDF, Office, iWork documents.

Give it a try, and do let me know your feedback.

Source Code


Follow me on Twitter

  • Raj

    Hi Mugunth,

    Hi Mugunth,

    Thanks for sharing the code. I'm trying your code in my app , the app is not showing the full web page screen . some how it shows half of the screen. Any thing needs to change in XIB?


  • Anonymous

    It’s great, apart from one small problem. It crashes the application if you close it if it hasn’t finished loading the webpage. Possible fixes in the future?

    Many thanks

    • Anonymous

      Can you send me a crash report?
      Thanks for notifying me.

    • Try this…
      Update the closeView method as shown

      -(IBAction) closeView:(id)sender
      [webView stopLoading];
      webView.delegate = nil;
      [self dismissModalViewControllerAnimated:YES];