我一直在研究一个简单的应用程序,该应用程序可以对网站进行网页浏览,学生可以在其中进行考试。
所以基本上,我的问题是学生完成后,必须单击将发送答案的按钮。 将出现一个弹出窗口,使他们确认。 https://i.stack.imgur.com/5GhB8.png 除了它没有显示。按下按钮时没有任何反应。 它可以在Safari上完美运行,我注意到它可以在已弃用的Webview(UIWebview)上运行,但是我一直在尝试使其在WKWebView上运行。
我绝对不是敏捷专家,因此,如果答案很简单,我深表歉意。我一直在寻找有关我的问题的答案,但是我不确定如何实现。
在此先感谢您的帮助,
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()
}
}
答案 0 :(得分: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
}
Voila!一切都将与UIWebView
相同。
注意:您将无法在WebSite上点击几个链接,因为它们将是HTTP而不是HTTPS。默认情况下,WKWebView会阻止所有不安全的HTTP请求。
要绕过,只需将NSExceptionAllowsInsecureHTTPLoads
添加为true
文件中的info.plist
。
答案 1 :(得分:1)
我也面临类似的问题,我的连接Facebook的弹出窗口不会在WKWebView中显示,但在野生动物园浏览器上可以正常工作。
此代码导致了此问题。
- (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 :(得分: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)
}
}
答案 3 :(得分:0)
当 Web 视图需要新的弹出窗口时,WKWebView 的 UIDelegate 就会出现。
按照以下步骤在 WKWebView 中显示 POP-Up 视图
进行另一个网络视图
private var popupWebView: WKWebView?
将主 web 视图的 uiDelegate 分配给 self
webView?.uiDelegate = self
确认控制器的 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
}
}
希望对你有帮助?