【问题标题】:Xcode WKWebView code to allow WebView to process popupsXcode WKWebView 代码允许 WebView 处理弹出窗口
【发布时间】:2019-03-29 23:05:04
【问题描述】:

我对 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
    }
}

如何编辑它以允许弹出窗口?

请记住,我不知道如何添加我找到的解决方案,所以请告诉我在哪里插入 sn-p。

【问题讨论】:

    标签: ios xcode wkwebview xcode9 popupwindow


    【解决方案1】:

    试试这个代码,希望对你有帮助!

    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
        }
    }
    }
    }
    

    【讨论】:

    • 谢谢你。我是否在我已经拥有的代码之后添加这个?这是我卡住的地方之一。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-07-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-12
    相关资源
    最近更新 更多