WKWebView弹出没有显示出来

问题描述 投票:2回答:3

我一直工作在一个简单的应用程序,使一个网站,学生可以参加考试的网页视图。

所以基本上我的问题是,当学生完成后,他们必须点击一个按钮,将发送答案。会显示一个弹出,使他们确认。 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()
    }
}
javascript ios swift webview wkwebview
3个回答
1
投票

我也面临类似的问题,我的是用于连接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;
}

斯威夫特版本:

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
}

希望这可以帮助你


2
投票

我试过轮的其他方式,它为我工作。下面是代码:创建的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。默认块未确保所有HTTP请求WKWebView。

要绕过,只需要添加NSExceptionAllowsInsecureHTTPLoadstrue文件info.plist


0
投票

我的解决办法是实施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)
    }

}
© www.soinside.com 2019 - 2024. All rights reserved.