WKWebView 弹出窗口未显示

WKWebView popup not showing up

我一直在开发一个简单的应用程序,该应用程序制作学生可以参加考试的网站的网络视图。

所以基本上我遇到的问题是,当学生完成后,他们必须单击一个将发送答案的按钮。 将出现一个弹出窗口让他们确认。 https://i.stack.imgur.com/5GhB8.png 除了它没有出现。按下按钮时没有任何反应。 它在 Safari 上运行完美,我注意到它在已弃用的 webview (UIWebview) 上运行,但我一直在努力使其在 WKWebView 上运行。

我绝对不是 swift 专家,所以如果答案很简单,我深表歉意。我一直在努力寻找有关我的问题的一些答案,但我不确定如何实施它。

提前感谢您的帮助,

import UIKit
import WebKit

class webViewController: UIViewController {

    @IBOutlet weak var webview: WKWebView!

    override func viewDidLoad() {

        super.viewDidLoad()

        let lien = "***"
        if let url = URL(string: lien) {
            let request = URLRequest(url: url)
            _ = webview.load(request);
        }
    }


    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
}

我也遇到了类似的问题,我的是用于连接 facebook 的弹出窗口不会在 WKWebView 中显示,但在 safari 浏览器上工作正常。

此代码导致了问题。

- (WKWebView *)webView:(WKWebView *)webView createWebViewWithConfiguration:(WKWebViewConfiguration *)configuration forNavigationAction:(WKNavigationAction *)navigationAction windowFeatures:(WKWindowFeatures *)windowFeatures {
//This condition was causing the problem while trying to get popup
    if (!navigationAction.targetFrame.isMainFrame) {
        [webView loadRequest:navigationAction.request];
    }
    return nil;
}

我将其更改为以下代码并且有效

- (WKWebView *)webView:(WKWebView *)webView createWebViewWithConfiguration:(WKWebViewConfiguration *)configuration forNavigationAction:(WKNavigationAction *)navigationAction windowFeatures:(WKWindowFeatures *)windowFeatures {
    if (navigationAction.targetFrame == nil) {
        NSURL *tempURL = navigationAction.request.URL;
        NSURLComponents *URLComponents = [[NSURLComponents alloc] init];
        URLComponents.scheme = [tempURL scheme];
        URLComponents.host = [tempURL host];
        URLComponents.path = [tempURL path];
        if ([URLComponents.URL.absoluteString isEqualToString:@"https://example.com/Account/ExternalLogin"]) {
            WKWebView *webViewtemp = [[WKWebView alloc] initWithFrame:self.view.bounds configuration:configuration];
            webViewtemp.UIDelegate = self;
            webViewtemp.navigationDelegate = self;
            [self.view addSubview:webViewtemp];
            return webViewtemp;
        } else {
            [webView loadRequest:navigationAction.request];
        }
    }
    return nil;
}

Swift版本:

func webView(_ webView: WKWebView, createWebViewWith configuration: WKWebViewConfiguration, for navigationAction: WKNavigationAction, windowFeatures: WKWindowFeatures) -> WKWebView? {
    if navigationAction.targetFrame == nil {
        let tempURL = navigationAction.request.url
        var components = URLComponents()
        components.scheme = tempURL?.scheme
        components.host = tempURL?.host
        components.path = (tempURL?.path)!
        if components.url?.absoluteString == "https://example.com/Account/ExternalLogin" {
            let webViewtemp = WKWebView(frame: self.view.bounds, configuration: configuration)
            webViewtemp.uiDelegate = self
            webViewtemp.navigationDelegate = self
            self.view.addSubview(webViewtemp)
            return webViewtemp
        } else {
            webView.load(navigationAction.request)
        }
    }
    return nil
}

希望对您有所帮助

我尝试了相反的方法,它对我有用。这是代码: 创建 WebView 实例

fileprivate var webView: WKWebView?

初始化实例并将其分配给视图。

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

此后,只需添加以下委托方法:

func webView(_: WKWebView, createWebViewWith _: WKWebViewConfiguration, for navigationAction: WKNavigationAction, windowFeatures _: WKWindowFeatures) -> WKWebView? {
    self.webView?.load(navigationAction.request)
    return nil
}

瞧!一切都将与 UIWebView.

相同

注意:您将无法点击网站上的一些链接,因为它们是 HTTP 而不是 HTTPS。默认情况下,WKWebView 会阻止所有不安全的 HTTP 请求。

要绕过,只需在 info.plist 文件中将 NSExceptionAllowsInsecureHTTPLoads 添加为 true

我的解决方案是实施 WKUIDelegate 并为 Web 视图可能显示的不同场景(警报)添加功能:

extension WKWebViewController: WKUIDelegate {

    func webView(_ webView: WKWebView, runJavaScriptAlertPanelWithMessage message: String, initiatedByFrame frame: WKFrameInfo, completionHandler: @escaping () -> Void) {
        let alertController = UIAlertController(title: nil, message: message, preferredStyle: .actionSheet)

        alertController.addAction(UIAlertAction(title: NSLocalizedString("OK", comment: "OK button"), style: .default, handler: { (action) in
            completionHandler()
        }))

        present(alertController, animated: true, completion: nil)
    }

    func webView(_ webView: WKWebView, runJavaScriptConfirmPanelWithMessage message: String, initiatedByFrame frame: WKFrameInfo, completionHandler: @escaping (Bool) -> Void) {
        let alertController = UIAlertController(title: nil, message: message, preferredStyle: .actionSheet)

        alertController.addAction(UIAlertAction(title: NSLocalizedString("OK", comment: "OK button"), style: .default, handler: { (action) in
            completionHandler(true)
        }))

        alertController.addAction(UIAlertAction(title: NSLocalizedString("Cancel", comment: "Cancel button"), style: .default, handler: { (action) in
            completionHandler(false)
        }))

        present(alertController, animated: true, completion: nil)
    }

    func webView(_ webView: WKWebView, runJavaScriptTextInputPanelWithPrompt prompt: String, defaultText: String?, initiatedByFrame frame: WKFrameInfo, completionHandler: @escaping (String?) -> Void) {
        let alertController = UIAlertController(title: nil, message: prompt, preferredStyle: .actionSheet)

        alertController.addTextField { (textField) in
            textField.text = defaultText
        }

        alertController.addAction(UIAlertAction(title: NSLocalizedString("OK", comment: "OK button"), style: .default, handler: { (action) in
            if let text = alertController.textFields?.first?.text {
                completionHandler(text)
            } else {
                completionHandler(defaultText)
            }
        }))

        alertController.addAction(UIAlertAction(title: NSLocalizedString("Cancel", comment: "Cancel button"), style: .default, handler: { (action) in
            completionHandler(nil)
        }))

        present(alertController, animated: true, completion: nil)
    }

}

当网络视图需要新的弹出窗口时window,WKWebView的UIDelegate就会出现。

按照以下步骤在 WKWebView 中显示 POP-Up 视图

  1. 换个网页浏览

    private var popupWebView: WKWebView?
    
  2. 将您的主网络视图的 uiDelegate 分配给 self

    webView?.uiDelegate = self
    
  3. 确认 WKUIDelegate 到你的控制器

    extension PaisaPalVC: WKUIDelegate {
        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
            if let newWebview = popupWebView {
                view.addSubview(newWebview)
            }
            return popupWebView ?? nil
        }
        func webViewDidClose(_ webView: WKWebView) {
            webView.removeFromSuperview()
            popupWebView = nil
        }
    }
    

希望对您有所帮助