Create UIWebView Programmatically and Render HTML Code

The following Swift code example demonstrates how to create a UIWebView programmatically and how to make it render HTML code. To break it into smaller steps, the below code snippet demonstrates how to:

  • Create UIWebView programmatically,
  • Implement UIWebViewDelegate functions webViewDidStartLoad and webViewDidFinishLoad,
  • Make UIWebView render HTML Code.

If you are interested in video lessons on how to write Unit tests and UI tests to test your Swift mobile app, check out this page: Unit Testing Swift Mobile App

Render HTML in UIWebView

import UIKit
import WebKit
class ViewControllear: UIViewController, WKUIDelegate {
    override func viewDidLoad() {
        // Do any additional setup after loading the view, typically from a nib.
        let myWebView:WKWebView = WKWebView(frame: CGRect(x:0, y:0, width: UIScreen.main.bounds.width, height:UIScreen.main.bounds.height))
        myWebView.uiDelegate = self
        let htmlString:String = "<b>Bold string</b>"
        myWebView.loadHTMLString(htmlString, baseURL: nil)
    override func didReceiveMemoryWarning() {
        // Dispose of any resources that can be recreated.


For more Swift code examples and tutorials, please check the Swift Code Examples page on this website.

Leave a Reply

Your email address will not be published. Required fields are marked *