show website in swift

To show a website in Swift, you can use either UIWebView or WKWebView. Here's an example of how to use UIWebView:

main.swift
import UIKit

class ViewController: UIViewController {
    
    @IBOutlet weak var webView: UIWebView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        // Load a website
        let url = URL(string: "https://www.example.com")
        let request = URLRequest(url: url!)
        webView.loadRequest(request)
    }
}
351 chars
16 lines

And here's an example of how to use WKWebView:

main.swift
import UIKit
import WebKit

class ViewController: UIViewController {
    
    @IBOutlet weak var webView: WKWebView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        // Load a website
        let url = URL(string: "https://www.example.com")
        let request = URLRequest(url: url!)
        webView.load(request)
    }
}
358 chars
17 lines

Note that WKWebView requires import WebKit to be added at the top of the file, and it's only available on iOS 8.0 and later.

gistlibby LogSnag