gpt4 book ai didi

ios - 某些 WKWebView 链接不可点击

转载 作者:行者123 更新时间:2023-12-01 18:05:14 25 4
gpt4 key购买 nike

我在使用基于 WKWebView 的 iOS 应用程序时遇到了挑战:页面上的某些链接不可点击。当我用 Safari View Controller 尝试它们时,它们起作用了。请问有人能帮帮我吗?

这是我的源代码:

import UIKit
import WebKit

class ViewController: UIViewController, WKUIDelegate, WKNavigationDelegate {
var webView: WKWebView! //declare the webview has to be optional
var activityIndicator: UIActivityIndicatorView!


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

override func viewDidLoad() {
super.viewDidLoad()

let myURL = URL(string: "https://stolenandfound.com/")
let myRequest = URLRequest(url: myURL!)
webView.load(myRequest)


activityIndicator = UIActivityIndicatorView() //declare the progress indicator
activityIndicator.frame = CGRect(x: 0, y: 0, width: 50, height: 50)

self.activityIndicator.center = CGPoint(x:self.view.bounds.size.width/2.0,y: self.view.bounds.size.height/2.0);

activityIndicator.autoresizingMask = (UIViewAutoresizing(rawValue: UIViewAutoresizing.RawValue(UInt8(UIViewAutoresizing.flexibleRightMargin.rawValue) | UInt8(UIViewAutoresizing.flexibleLeftMargin.rawValue) | UInt8(UIViewAutoresizing.flexibleBottomMargin.rawValue) | UInt8(UIViewAutoresizing.flexibleTopMargin.rawValue))))

//activityIndicator.center = CGPoint(x: UIScreen.main.bounds.size.width/2, y: UIScreen.main.bounds.size.height/2)
activityIndicator.hidesWhenStopped = true

activityIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyle.whiteLarge
activityIndicator.color = UIColor.darkGray
self.view.addSubview(activityIndicator)
}



func webView(_ webView: WKWebView, didStartProvisionalNavigation navigation: WKNavigation!) {
activityIndicator.startAnimating()
}

func webView(_ webView: WKWebView, didFailProvisionalNavigation navigation: WKNavigation!, withError error: Error) {
print("it is an error")
activityIndicator.stopAnimating()
let alert = UIAlertController(title: "Network Error", message: "You have no internet coonection", preferredStyle: .alert)

let restartAction = UIAlertAction(title: "Reload page", style: .default, handler: { (UIAlertAction) in
self.viewDidLoad()
})

alert.addAction(restartAction)
present(alert, animated: true, completion: nil)
}




func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
activityIndicator.stopAnimating()
}

@IBAction func backButton(_ sender: UIBarButtonItem) {
webView.goBack()
}


@IBAction func refreshButton(_ sender: UIBarButtonItem) {
webView.reload()
}

}

最佳答案

如果链接在 Safari 中的新选项卡中打开,则必须实现 WKUIDelegate 函数 func webView(_ webView: WKWebView, createWebViewWith configuration: WKWebViewConfiguration, for navigationAction: WKNavigationAction, windowFeatures: WKWindowFeatures) -> WKWebView? 并在那里处理请求。那时你可以在现有的 webView 中加载请求,或者创建一个新的 webView 或者在 Safari 中打开它。

func webView(_ webView: WKWebView, createWebViewWith configuration: WKWebViewConfiguration, for navigationAction: WKNavigationAction, windowFeatures: WKWindowFeatures) -> WKWebView? {
guard let theURL = navigationAction.request.url else {
return nil
}
if loadInCurrentWebview{
_ = webView.load(navigationAction.request) //loads the link in the current webView
} else if loadInNewWebview{
return WKWebView()
}else {
//loads in safari
if #available(iOS 10.0, *) {
UIApplication.shared.open(theURL, options: [:], completionHandler: nil)
} else {
UIApplication.shared.openURL(theURL)
}
}
return nil
}

关于ios - 某些 WKWebView 链接不可点击,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47722776/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com