Webview Tutorial in iOS8 with Swift

In iOS you can load a URL into a native app through the UIWebView Control. This tutorial is written in Swift so you will need Xcode 6. It can be downloaded at Apple's developer portal.

Open Xcode and create a new Single View Application. For product name, use iOS8SwiftWebViewTutorial and then fill out the Organization Name and Organization Identifier with your customary values. Enter Swift as Language and make sure only iPhone is selected in Devices.

Go to the Storyboard and change the width to compact. This will change the view to a iPhone portrait mode. 

Add a Web View to the main view. The storyboard should look like this.

Select the assistant editor and open the ViewController.swift file. Ctrl and drag from the button to the class section and create the following outlet.

We will present the Apple website inside the Web View, so in the ViewController.swift file add the following constant property.

let url = "http://apple.com"

Change the viewDidLoad method to

override func viewDidLoad() {
    let requestURL = NSURL(string:url)
    let request = NSURLRequest(URL: requestURL!)

The following steps are needed to load the URL into the web view

  1. Create a String which contains our URL
  2. Convert the String to a NSURL object.
  3. Place the URL in a URL Request.

Build and Run, you should see the Apple website displayed into the web view.

You can download the source code of the IOS8SwiftWebViewTutorial at the ioscreator repository on github.