WKWebView弹出窗口未显示。

9
我一直在开发一个简单的应用程序,它可以制作一个网站的Webview,学生可以在里面参加考试。
基本上,我的问题是当学生完成考试后,他们必须点击一个按钮来发送答案。弹出窗口将出现以让他们确认。 https://istack.dev59.com/5GhB8.webp 但实际上并没有出现。当按钮被按下时什么都不会发生。 它在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()
    }
}

请问您能添加代码吗?没有代码很难修复。 - Karthick Ramesh
我已经添加了代码。 - Notsu
你在添加 WKWebView 时使用了约束吗? - Bogdan Novikov
我认为你需要查看https://dev59.com/iF8d5IYBdhLWcg3wYhSX - Karthick Ramesh
我在每个位置都使用了约束。我已经阅读了那个主题,我只需要实现这三个函数吗? - Notsu
4个回答

6
我也遇到了类似的问题,我的问题是在WKWebView中弹出连接Facebook的弹窗无法显示,但在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
}

希望这能对您有所帮助。

你有用 Swift 语言编写的代码吗? - Notsu
我该如何执行这个函数?我不确定应该输入哪些参数。 - Notsu
这让我找到了解决方法,谢谢 :) - JHawkZZ

5

我的解决方案是实现 WKUIDelegate 并添加处理不同场景(警报)的功能:

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

}

3
当Web视图需要新的弹出窗口时,WKWebView的UIDelegate将会涉及其中。
按照以下步骤在WKWebView中显示弹出视图。
  1. Take a another web-view

    private var popupWebView: WKWebView?
    
  2. Assign the uiDelegate of your main web-view to self

    webView?.uiDelegate = self
    
  3. Confirm the WKUIDelegate of to your controller

    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
        }
    }
    
希望这能帮到您。

准确无误,这正是我所寻找的。 - Lokesh Deshmukh

2
我尝试了另一种方法,对我起作用了。这是代码: 创建一个WebView实例。最初的回答:

我尝试了其他方法,但这个方法对我有效。以下是代码: 创建一个WebView实例。

fileprivate var webView: WKWebView?

Initialize instance and make assign it to view.

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即可。

最初的回答:


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接