我以前从未见过这样的错误。下面的代码以前运行完美。我看不到我会做出的任何改变。现在,goBack和goForward无法按预期工作。发生了什么?这是非常奇怪的事情。
我已经尝试过通过各种方式进行跟踪,并且无法理解为什么单击后它不返回。我什至尝试在单击返回后添加webView.reload(),但这也不起作用。
我收到此错误:Error Domain=NSURLErrorDomain Code=-999 "(null)"
,然后是我尝试访问的页面。这是我的课程:
class WebPageModalViewController: UIViewController, WKUIDelegate, WKNavigationDelegate {
@IBOutlet weak var mainView: UIView!
@IBOutlet weak var primaryLabel: UILabel!
@IBOutlet weak var secondaryLabel: UILabel!
@IBOutlet weak var backButtonImageView: UIImageView!
@IBOutlet weak var forwardButtonImageView: UIImageView!
@IBAction func didClickClose(_ sender: Any) {
IHProgressHUD.dismiss()
dismiss(animated: true, completion: nil)
}
@IBAction func didClickOpenInBrowser(_ sender: Any) {
URL.openURL(self.url)
}
var url: String
var webView: WKWebView!
init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?, url: String) {
self.url = url
super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func viewDidLoad() {
super.viewDidLoad()
let backGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(imageTapped(tapGestureRecognizer:)))
backButtonImageView.isUserInteractionEnabled = true
backButtonImageView.addGestureRecognizer(backGestureRecognizer)
let forwardGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(imageTapped(tapGestureRecognizer:)))
forwardButtonImageView.isUserInteractionEnabled = true
forwardButtonImageView.addGestureRecognizer(forwardGestureRecognizer)
self.primaryLabel.text = ""
self.secondaryLabel.text = ""
webView.scrollView.delegate = self
let myURL = URL(string:url)
let myRequest = URLRequest(url: myURL!)
webView.load(myRequest)
setTintColor(backButtonImageView, enabled: false)
setTintColor(forwardButtonImageView, enabled: false)
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
}
override var preferredStatusBarStyle : UIStatusBarStyle {
return .lightContent
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
IHProgressHUD.dismiss()
}
override func loadView() {
super.loadView()
let webConfiguration = WKWebViewConfiguration()
webView = WKWebView(frame: .zero, configuration: webConfiguration)
webView.uiDelegate = self
webView.navigationDelegate = self
webView.addObserver(self, forKeyPath: #keyPath(WKWebView.title), options: .new, context: nil)
webView.addObserver(self, forKeyPath: #keyPath(WKWebView.canGoForward), options: .new, context: nil)
webView.addObserver(self, forKeyPath: #keyPath(WKWebView.canGoBack), options: .new, context: nil)
if (webView != nil) {
mainView.addConstrained(subview: webView!)
}
}
// MARK: WkWebview
func webView(_ webView: WKWebView, didStartProvisionalNavigation navigation: WKNavigation!) {
IHProgressHUD.show()
}
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
IHProgressHUD.dismiss()
}
func webView(_ webView: WKWebView, didFail navigation: WKNavigation!, withError error: Error) {
IHProgressHUD.dismiss()
BannerManager.errorLoadingWebpage.show()
dismiss(animated: true, completion: nil)
}
func webView(_ webView: WKWebView, didFailProvisionalNavigation navigation: WKNavigation!, withError error: Error) {
guard let failingUrlStr = (error as NSError).userInfo["NSErrorFailingURLStringKey"] as? String else { return }
let failingUrl = URL(string: failingUrlStr)!
switch failingUrl {
// Needed to open Appstore-App
case _ where failingUrlStr.hasPrefix("itms-appss://"):
if UIApplication.shared.canOpenURL(failingUrl) {
UIApplication.shared.open(failingUrl, options: [:], completionHandler: nil)
IHProgressHUD.dismiss()
dismiss(animated: true, completion: nil)
return
} else {
self.webView(webView, didFail: navigation, withError: error)
}
default: self.webView(webView, didFail: navigation, withError: error)
}
}
// func webView(_ webView: WKWebView,
// decidePolicyFor navigationAction: WKNavigationAction,
// decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
//
// // if the url is not http(s) schema, then the UIApplication open the url
// if let url = navigationAction.request.url,
// !url.absoluteString.hasPrefix("http://"),
// !url.absoluteString.hasPrefix("https://"),
// UIApplication.shared.canOpenURL(url) {
//
// UIApplication.shared.open(url, options: [:], completionHandler: nil)
// // cancel the request
// decisionHandler(.cancel)
// } else {
// // allow the request
// decisionHandler(.allow)
// }
// }
override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
if keyPath == "title" {
self.primaryLabel.text = webView.title
self.secondaryLabel.text = webView.url?.absoluteString
} else if keyPath == "canGoBack" {
setTintColor(backButtonImageView, enabled: webView.canGoBack)
} else if keyPath == "canGoForward" {
setTintColor(forwardButtonImageView, enabled: webView.canGoForward)
}
}
// MARK: Private Funcs
private func setTintColor(_ image:UIImageView, enabled:Bool) {
image.tintColor = enabled ? UIColor.textSecondary : UIColor.ECHLightGrey
}
@objc func imageTapped(tapGestureRecognizer: UITapGestureRecognizer) {
let tappedImage = tapGestureRecognizer.view as! UIImageView
if tappedImage == backButtonImageView {
webView.goBack()
} else if tappedImage == forwardButtonImageView {
webView.goForward()
}
}
}
extension WebPageModalViewController : UIScrollViewDelegate {
func scrollViewDidScroll(_ scrollView: UIScrollView) {
scrollView.contentOffset.x = 0
}
}
任何想法都会很棒!
答案 0 :(得分:2)
我们看到了同样的问题。
我们的调查表明,它始于iOS 13.4或13.4.1的发布(没有启用13.4的设备来验证这一点),但在这一点上仍然可以正常工作。
到目前为止,我还没有找到解决该问题的方法,但是我有解决方法。
我观察到的是,这在较慢的网络上以及用户在页面完全加载之前进行交互时更经常发生。如果用户在页面加载完成之前点击回去,则会发生错误。此错误基本上表示“已取消”,并引用了他们要离开的页面。如果他们让页面加载完成,则显然不会发生此错误。现在重要的是,此时Web视图的状态为“正在加载”。
但是,如果他们返回的页面在离开时从未完成加载,那么会再次引发错误,但是这次引用的是他们返回的页面。更重要的是,此时Web视图不再加载。
我们要做的是监视此情况,并检查是否未加载页面,如果未加载,请重新加载。这似乎可行,虽然不理想,但它使我们有时间研究适当的修复程序。
此外,我对此设置了重试限制,尽管在实践中我们从未见过超出第一次重载尝试的情况,但在第三次尝试后显示重载按钮会显示错误。
几乎立即抛出错误的事实意味着重新加载不会为用户带来明显的延迟。
希望这会有所帮助。
答案 1 :(得分:0)
我在iOS 13.4中也遇到了同样的问题,这是因为WebView确实完成加载需要花费更多的时间来调用。
使用
webView.stopLoading()
就在
if webView.canGoBack {
print("go back")
webView.goBack()
}
它为我工作