How to load HTML url in webview using swift

Well, it’s very easy to load HTML url in a web view using swift. Suppose we want to show in a web view in our iOS application. We’ll do that in 4 easy steps. Let me tell you them step by step.

  1. First create an iOS application in Xcode.
    Add a WebView in the ViewController of the storyboard. Do not forget to add auto layout to WebView, otherwise you may can’t see the WebView.screen-shot-2016-09-13-at-10-45-27-am
  2. Drag and drop WebView’s IBOutlet to our ViewController.swift and name it: webView
  3. Now we’ll create a new method in ViewController.swift
  4. In ViewDidLoad method call loadUrl method:

Run the app. You will see google website is loaded.

If you use any non-secure url, Xcode will log an error in error console.

App Transport Security has blocked a cleartext HTTP (http://) resource load since it is insecure. Temporary exceptions can be configured via your app’s Info.plist file.

The urls that start with http instead of https is called non-secured url. Due to security issue, iOS doesn’t load non https urls.
In such case we’ve to add some values in our Info.plist.  

Fix the issue of “App Transport Security has blocked a cleartext HTTP”


a. Open Info.plist file from left panel (File explorer).


b. Click on plus button (+) of any top level row. I’ll click the plus button beside Bundle Identifier.


c. A new row will be created. Type “App Transport Security Settings”  (without double quotes) in the row (key column) box. Hit Enter. It’s type should be a dictionary.


d.  Open the App Transport Security Settings row by clicking on Arrow head at the beginning of the row.


e. Click Plus button next to the row of “App Transport Security Settings”


f. In new row type “Allow Arbitrary Loads” (without double quotes)

g. Make it’s value to Yes.

Run the app again with any non-https link e.g.

Full code is given below:

Leave a Reply