Displaying a Web Page

Your app can display a web page by using the Web View object on your user interface and then by writing three lines of code. So first, place a Web View on your user interface and give it a unique name such as webView, although the exact name isn’t important.

Next, create a variable or constant using any name you wish. Then set this variable/constant to a string that defines the complete web site address like this:

        // The web address must be https:// and not just http://
        let showURL = NSURL(string: "https://www.apple.com")

The only restriction is that the web site address must use https:// and not just plain http:// or else it won’t work.

After creating a variable/constant to represent the web site address, you need to create a second variable/constant to request the web site address, known as a URL:

        let requestThis = NSURLRequest(URL: showURL!)

Finally you need to use the loadRequest method on your Web View to request a particular web site domain address:

        webView.loadRequest(requestThis)

So the complete code to work with a Web View object on your user interface looks like this:

        // The web address must be https:// and not just http://
        let showURL = NSURL(string: "https://www.apple.com")
        let requestThis = NSURLRequest(URL: showURL!)
        webView.loadRequest(requestThis)

Just paste a different string for the web site address and you’ll be able to display a different web site. Remember, make sure the web site address uses https:// and also make sure the web site is optimized for mobile devices so it adapts to the smaller screen of an iPhone or iPad.

May 2nd, 2016 by
HTML Snippets Powered By : XYZScripts.com