Ios Xcode WKWebView代码允许WebView处理弹出窗口

Ios Xcode WKWebView代码允许WebView处理弹出窗口,ios,xcode,wkwebview,xcode9,popupwindow,Ios,Xcode,Wkwebview,Xcode9,Popupwindow,我对Xcode完全陌生,语法也完全不懂 我还有一件事要做,然后我在Xcode中实现了我所需要的 我需要插入一个允许WebView处理弹出窗口的函数,但我不知道从哪里开始插入代码 这是当前代码: import UIKit import WebKit class ViewController: UIViewController, WKNavigationDelegate { override func viewDidLoad() { super.viewDidLoad()

我对Xcode完全陌生,语法也完全不懂

我还有一件事要做,然后我在Xcode中实现了我所需要的

我需要插入一个允许WebView处理弹出窗口的函数,但我不知道从哪里开始插入代码

这是当前代码:

import UIKit
import WebKit

class ViewController: UIViewController, WKNavigationDelegate {
    override func viewDidLoad() {
        super.viewDidLoad()
        let webView = WKWebView()
        let htmlPath = Bundle.main.path(forResource: "index", ofType: "html")
        let folderPath = Bundle.main.bundlePath
        let baseUrl = URL(fileURLWithPath: folderPath, isDirectory: true)
        do {
            let htmlString = try NSString(contentsOfFile: htmlPath!, encoding: String.Encoding.utf8.rawValue)
            webView.loadHTMLString(htmlString as String, baseURL: baseUrl)
        } catch {
            // catch error
        }
        webView.navigationDelegate = self
        view = webView
    }
}
我如何编辑它以允许弹出窗口


请记住,我不知道如何添加我正在查找的解决方案,因此请告诉我在何处插入代码片段。

试试这段代码,希望能有所帮助

class ViewController: UIViewController {
var webView: WKWebView!
var popupWebView: WKWebView?
var urlPath: String = "WEBSITE_URL"

override func viewDidLoad() {
    super.viewDidLoad()
    setupWebView()
    loadWebView()
}
//MARK: Setting up webView
func setupWebView() {
    let preferences = WKPreferences()
    preferences.javaScriptEnabled = true
    preferences.javaScriptCanOpenWindowsAutomatically = true

    let configuration = WKWebViewConfiguration()
    configuration.preferences = preferences

    webView = WKWebView(frame: view.bounds, configuration: configuration)
    webView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
    webView.uiDelegate = self
    webView.navigationDelegate = self

    view.addSubview(webView)
}

func loadWebView() {
    if let url = URL(string: urlPath) {
        let urlRequest = URLRequest(url: url)
        webView.load(urlRequest)
    }
}
}


谢谢你。我是否在已有代码之后添加此代码?那是我被困的一个大地方。
extension ViewController: WKUIDelegate {
//MARK: Creating new webView for popup
func webView(_ webView: WKWebView, createWebViewWith configuration: WKWebViewConfiguration, for navigationAction: WKNavigationAction, windowFeatures: WKWindowFeatures) -> WKWebView? {
    popupWebView = WKWebView(frame: view.bounds, configuration: configuration)
    popupWebView!.autoresizingMask = [.flexibleWidth, .flexibleHeight]
    popupWebView!.navigationDelegate = self
    popupWebView!.uiDelegate = self
    view.addSubview(popupWebView!)
    return popupWebView!
}
//MARK: To close popup
func webViewDidClose(_ webView: WKWebView) {
    if webView == popupWebView {
        popupWebView?.removeFromSuperview()
        popupWebView = nil
    }
}
}
}