How to load a URL in a UIWebView in Swift?

To load a URL in a UIWebView in Swift, you can follow these steps:

  1. Create an instance of UIWebView in your view controller.
let webView = UIWebView()
  1. Add the UIWebView to your view hierarchy.
view.addSubview(webView)
  1. Set the frame of the UIWebView to the desired size and position.
webView.frame = CGRect(x: 0, y: 0, width: view.frame.width, height: view.frame.height)
  1. Create a URLRequest object with the URL you want to load.
let url = URL(string: "https://www.example.com")!
let request = URLRequest(url: url)
  1. Load the URL request in the UIWebView.
webView.loadRequest(request)

Here’s the complete code:

import UIKit

class ViewController: UIViewController {
    let webView = UIWebView()

    override func viewDidLoad() {
        super.viewDidLoad()

        view.addSubview(webView)
        webView.frame = CGRect(x: 0, y: 0, width: view.frame.width, height: view.frame.height)

        let url = URL(string: "https://www.example.com")!
        let request = URLRequest(url: url)

        webView.loadRequest(request)
    }
}
A pat on the back !!