Displaying Web Content

UIWebView is used to display web content. UIWebView is simply an extension of UIView with URL loading and displaying capabilities, so it can be used wherever you would use a UIView. UIWebView is built using the WebKit framework, which is the same set of libraries used in Safari, making it surprisingly fast.

You can add a web view directly in the Storyboard. You can also set whether or not the web view scales pages to fit on the device screen. This can also be done in code by setting the scalesPagesToFit property.

Web pages are loaded using NSURLRequest.

Header Source

UIWebView has some built-in navigation methods you can call to control page display:

  • goBack loads the previous page in the stack. canGoBack determines if we are at the bottom of the stack.
  • goForward loads the next page in the stack. canGoForward determines if we are at the top of the stack.
  • reload reloads the current page.
  • stopLoading stops loading whatever content it’s loading.

These actions can be linked directly in the Storyboard.

The web view delegate can receive information on when page loads starts and ends. It must conform to the UIWebViewDelegate protocol. We can use this information to update button states in our user interface.

Header Source

You can also implement webView:didFailLoadWithError: to handle any errors.

You can add a UIActivityIndicatorView to give more feedback to the user.

Header Source


Comments are closed.