To load a URL in a UIWebView
in Swift, you can follow these steps:
- Create an instance of
UIWebView
in your view controller.
let webView = UIWebView()
- Add the
UIWebView
to your view hierarchy.
view.addSubview(webView)
- Set the
frame
of theUIWebView
to the desired size and position.
webView.frame = CGRect(x: 0, y: 0, width: view.frame.width, height: view.frame.height)
- Create a
URLRequest
object with the URL you want to load.
let url = URL(string: "https://www.example.com")! let request = URLRequest(url: url)
- 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) } }