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 = @"http://blog.mugunthkumar.com";
[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

MKWebViewController.zip

Mugunth

Follow me on Twitter

If you enjoyed this post, make sure you subscribe to my RSS feed!

Related posts:

  1. iOS Tip: Display Names in iPhone Display Order If you have noticed the contacts app on your iPhone/iPad,...
  2. In App Purchases: Troubleshooting Code=0 “Cannot connect to iTunes Store” (SKErrorUnknown) When using MKStoreKit or any other equivalent framework or regular...
  3. iOS Control: MKHorizMenu Ok, I’m getting a bit aggressive here. I just finished...
  4. iOS Tutorial: Sending In App Tweets In iOS 5, Apple introduced tighter twitter integration with Twitter.framework...

  • 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?

    Thanks,
    Raj

  • 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.

    • http://meachware.com Meachware

      Try this…
      Update the closeView method as shown

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