UIWebView and UIWebViewDelegate example in Swift

In this video tutorial I am showing how to create a new UIWebView and how to load information into it in three different ways:

1. Load a web page from a remote URL
2. Load short HTML string
3. Load html file stored which is a part of your mobile app.

Also I am showing you how to add UIActivityIndicator to let user know that web page started loading and stop activity indicator animating when web page has finished loading.

Another nice to have example in this video is a Refresh button. I am showing you how to add a refresh button which when tapped will refresh the web page and load its content again. This is sometimes needed because internet connection is not always fast and sometimes web pages load partially only and it is good to be able to reload the page.

I hope this video tutorial is helpful to you guys and if it does not work for you for some reason, please drop me a line.

Thank you!


Source code

// ViewController.swift
// MyWebViewExample
// Created by Sergey Kargopolov on 2015-03-14.
// Copyright (c) 2015 Sergey Kargopolov. All rights reserved.
import UIKit

class ViewController: UIViewController, UIWebViewDelegate {

@IBOutlet weak var myWebView: UIWebView!
@IBOutlet weak var myActivityIndicator: UIActivityIndicatorView!

override func viewDidLoad() {
// Do any additional setup after loading the view, typically from a nib.

myWebView.delegate = self

//1. Load web site into my web view

let myURL = NSURL(string: “http://www.swiftdeveloperblog.com”);
let myURLRequest:NSURLRequest = NSURLRequest(URL: myURL!);

//2. Load html string into web view

let htmlString:String = ”
Bold string
myWebView.loadHTMLString(htmlString, baseURL: nil)


//3. Load local html file into web view
let myProjectBundle:NSBundle = NSBundle.mainBundle();

let filePath:String = myProjectBundle.pathForResource(“my-html-file”, ofType: “html”)!

let myURL = NSURL(string: filePath);
let myURLRequest:NSURLRequest = NSURLRequest(URL: myURL!);



override func didReceiveMemoryWarning() {
// Dispose of any resources that can be recreated.

func webViewDidStartLoad(webView: UIWebView)
func webViewDidFinishLoad(webView: UIWebView)

@IBAction func refreshButtonTapped(sender: AnyObject) {


  • fidel

    is there any way to do this to a pdf url and have a UISearchbar able to pass searches to the pdf url and then the pdf will zoom in to the search and highlight it as well?

    • I do not think you can redirect it to a PDF file… Most probably you will need to redirect to a script that will generate PDF file with searched word highlighted before the file gets generated… something like this.

  • Glenroy Matthews

    What if I have several local html files that I want to load dynamically, like when a tableview row is clicked, how do I do that?

    • Here is how you can do it: let url = NSBundle.mainBundle().URLForResource(“localHTMLFileName”, withExtension:”html”)
      let request = NSURLRequest(URL: url!)

      • Glenroy Matthews

        Hi Thanks for your speedy response, i really appreciate it. i tried what you suggested but it will only load one html file, which is the “localHTMLFileName file. I am looking to use a tableview that will load let say file1.html when a table row is clicked, and load file2.html when another tableview row is clicked.

        • So your question is how to pass the selected row’s value into the next ViewController which is going to be a WebView?

          • Glenroy Matthews

            Yes, that is basically what I am asking.

      • Ranjith Kumar

        can you explain with Example Please!!

  • Is the whole project available to download somewhere?

  • Is the whole project available to download somewhere?

  • timbojill

    I really need some help. I am a real newbie. I have a simple project on the app store. I need to add two buttons to the home screen that will load two predetermined websites. Kindly email me at timbojill@gmail.com if you can help or seen an example that those this. Nothing fancy. Just a button on one screen that when you click on it it loads a website. No input from the use required. Creating/modifying an app I already created. The way it is now each button on start screen takes then to an UI View via segues. I want to change it so it takes them to a webpage so I can just update the webpage so I don’t need to modify the project every time information changes.

  • iAhmadSA ツ

    Hi Sir, i have error can you help me please

  • venkata phaneendra

    How can I get current url of a page i swift