我需要重构代码才能开始使用WKWebView,因为苹果建议我从UIWebView切换到WKWebview。我的问题是,当应用执行时,我的代码的一部分没有被触发,我似乎无法指出我做错了什么,这就是我需要替换的代码
0.29292
我试图这样做
class Webview: UIWebView, UIWebViewDelegate, UIGestureRecognizerDelegate, MFMailComposeViewControllerDelegate {
var initialViewController: LinkViewControllerType?
var isLoadingHTMLString:Bool = true
var scrollViewDidScroll: ((_ scrollView: UIScrollView) -> ())?
var scrollViewDidEndDecelerating: ((_ scrollView: UIScrollView) -> ())?
var scrollViewDidEndDragging: ((_ scrollView: UIScrollView,_ willDecelerate: Bool) -> ())?
var scrollViewWillEndDragging: ((_ scrollView: UIScrollView,_ withVelocity: CGPoint,_ targetContentOffset: UnsafeMutablePointer<CGPoint>) -> ())?
var shouldStartLoadWith: ((_ webview: UIWebView,_ request: URLRequest, _ navigationType: UIWebView.NavigationType) -> Bool)?
var webViewDidFinishLoad: ((_ webView: UIWebView) -> ())?
var tapGestureRecogniser:UITapGestureRecognizer?
func configure(from: LinkViewControllerType) {
self.initialViewController = from
self.delegate = self
self.scrollView.delegate = self
if (self.tapGestureRecogniser == nil) {
let recog = UITapGestureRecognizer.init(target: self, action: #selector(doubleTap(_:)))
recog.delegate = self
self.addGestureRecognizer(recog)
self.tapGestureRecogniser = recog
}
}
func webView(_ webView: UIWebView, shouldStartLoadWith request: URLRequest, navigationType: UIWebView.NavigationType) -> Bool {
return shouldStartLoadWith?(webView, request, navigationType) ?? true
}
func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
return true
}
@objc func doubleTap(_ gestureRecogniser: UITapGestureRecognizer) {
let touchPoint = gestureRecogniser.location(in: self) as CGPoint
let imageSrc = String(format: "document.elementFromPoint(%f, %f).src", touchPoint.x, touchPoint.y)
if let srcOfImage = self.stringByEvaluatingJavaScript(from: imageSrc) {
if (srcOfImage.contains("http://") || srcOfImage.contains("https://")) {
self.openImageView(httpUrl: srcOfImage)
}
}
}
private func openImageView(fileUrl:URL? = nil, httpUrl:String? = nil) {
let viewController = ViewControllerManager.Initializers.GuidelineImageViewController()
if let fileUrl = fileUrl {
if let imageData = try? Data(contentsOf: fileUrl) {
viewController.image = UIImage(data: imageData)
}
} else {
viewController.httpUrl = httpUrl
}
self.initialViewController?.navigationController?.pushViewController(viewController, animated: true)
}
func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?) {
if let initialViewController = self.initialViewController {
initialViewController.dismiss(animated: true, completion: nil)
}
}
func webViewDidFinishLoad(_ webView: UIWebView) {
webViewDidFinishLoad?(webView)
let when = DispatchTime.now() + 0.05
DispatchQueue.main.asyncAfter(deadline: when) {
self.alpha = 1
}
}
override func scrollViewDidScroll(_ scrollView: UIScrollView) {
super.scrollViewDidScroll(scrollView)
scrollViewDidScroll?(scrollView)
}
override func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
scrollViewDidEndDecelerating?(scrollView)
}
override func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool) {
scrollViewDidEndDragging?(scrollView,decelerate)
}
override func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint,
targetContentOffset: UnsafeMutablePointer<CGPoint>) {
scrollViewWillEndDragging?(scrollView, velocity, targetContentOffset)
}}
但是我的这段代码没有被触发
class Wkwebview: WKWebView, WKNavigationDelegate, UIGestureRecognizerDelegate, UIScrollViewDelegate, MFMailComposeViewControllerDelegate{
var initialViewController: LinkViewControllerType?
var isLoadingHTMLString:Bool = false
var scrollViewDidScroll: ((_ scrollView: UIScrollView) -> ())?
var scrollViewDidEndDecelerating: ((_ scrollView: UIScrollView) -> ())?
var scrollViewDidEndDragging: ((_ scrollView: UIScrollView,_ willDecelerate: Bool) -> ())?
var scrollViewWillEndDragging: ((_ scrollView: UIScrollView,_ withVelocity: CGPoint,_ targetContentOffset: UnsafeMutablePointer<CGPoint>) -> ())?
var decidePolicyFor: ((_ webview: WKWebView,_ request: WKNavigationAction) -> Bool)?
var viewDidLoad: ((_ webView: WKWebView) -> ())?
var tapGestureRecogniser:UITapGestureRecognizer?
func configure(from: LinkViewControllerType) {
self.initialViewController = from
self.navigationDelegate = self
self.scrollView.delegate = self
if (self.tapGestureRecogniser == nil) {
let recog = UITapGestureRecognizer.init(target: self, action: #selector(doubleTap(_:)))
recog.delegate = self
self.addGestureRecognizer(recog)
self.tapGestureRecogniser = recog
}
}
func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) -> Bool {
return decidePolicyFor?(webView,navigationAction) ?? true
}
func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
return true
}
@objc func doubleTap(_ gestureRecogniser: UITapGestureRecognizer) {
let touchPoint = gestureRecogniser.location(in: self) as CGPoint
let imageSrc = String(format: "document.elementFromPoint(%f, %f).src", touchPoint.x, touchPoint.y)
var srcOfImage = ""
self.evaluateJavaScript(imageSrc, completionHandler: { (result: Any?, error: Error?) in
if(error == nil){
if(result != nil){
srcOfImage = result as! String
if (!srcOfImage.isEmptyOrWhitespace()) {
if (srcOfImage.contains("http://") || srcOfImage.contains("https://")) {
self.openImageView(httpUrl: srcOfImage)
}
}
}
}
})
}
private func openImageView(fileUrl:URL? = nil, httpUrl:String? = nil) {
let viewController = ViewControllerManager.Initializers.GuidelineImageViewController()
if let fileUrl = fileUrl {
if let imageData = try? Data(contentsOf: fileUrl) {
viewController.image = UIImage(data: imageData)
}
} else {
viewController.httpUrl = httpUrl
}
self.initialViewController?.navigationController?.pushViewController(viewController, animated: true)
}
func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?) {
if let initialViewController = self.initialViewController {
initialViewController.dismiss(animated: true, completion: nil)
}
}
func viewDidLoad(_ webView: WKWebView) {
viewDidLoad(webView)
let when = DispatchTime.now() + 0.05
DispatchQueue.main.asyncAfter(deadline: when) {
self.alpha = 1
}
}
func scrollViewDidScroll(_ scrollView: UIScrollView) {
scrollViewDidScroll?(scrollView)
}
func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
scrollViewDidEndDecelerating?(scrollView)
}
func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool) {
scrollViewDidEndDragging?(scrollView,decelerate)
}
func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint,
targetContentOffset: UnsafeMutablePointer<CGPoint>) {
scrollViewWillEndDragging?(scrollView, velocity, targetContentOffset)
}}
在UIWebView上没有问题。它可以正常工作。非常感谢您的协助。