1

I have been trying to make a basic app for just a few days and I would like the Webkit View only to popup when a button is pressed. I have put the code I was using but it just showed the site and nothing else.

class ViewController: UIViewController {

    var webView: WKWebView!

    override func loadView() {
        webView = WKWebView(frame: .zero, configuration: WKWebViewConfiguration())
        self.view = webView
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        webView.load(URLRequest(url: URL(string: "https://www.apple.com/")!))
    }
}
bad_coder
  • 5,829
  • 13
  • 26
  • 41

1 Answers1

0

You can use it if you wanna make it in two separate views and show the 2nd viewController after clicking on the button:

import UIKit
import WebKit

class ViewController: UIViewController, WKUIDelegate {
    
var webview: WKWebView!
    
    override func loadView() {
        let webconfig = WKWebViewConfiguration()
        webview = WKWebView(frame: .zero, configuration: webconfig)
        webview.uiDelegate = self
        view = webview
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        let myurl = URL(string: "https://google.com/")
        let myRequest = URLRequest(url: myurl!)
        webview.load(myRequest)
    } 

enter image description here

but if you wanna it in the same view you can use the below by using button action:

import UIKit
import WebKit

class ViewController: UIViewController, WKUIDelegate {
    
var webview: WKWebView!

    override func viewDidLoad() {
        super.viewDidLoad()
       
    }
    
    func loadMyView() {
        let webconfig = WKWebViewConfiguration()
        webview = WKWebView(frame: .zero, configuration: webconfig)
        webview.uiDelegate = self
        view = webview
    }
    func viewWeb()  {
        let myurl = URL(string: "https://google.com/")
        let myRequest = URLRequest(url: myurl!)
        webview.load(myRequest)
    }
    
    @IBAction func buttonWebViewed(_ sender: Any) {
        loadMyView()
        viewWeb()
    }
    
   
}

enter image description here

Menaim
  • 562
  • 2
  • 21