且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

简单的Webview:navigationDelegate在启动时崩溃

更新时间:2023-10-23 11:51:40

您正在使用UIWebView,并尝试调用WKWebView的方法,由于UIWebView上发送的选择器无法识别,导致应用崩溃.

You are using UIWebView and trying to call the methods of WKWebView which is leading to crash your app due to unrecognised selector send on UIWebView.

尝试创建新项目并使用下面提到的代码.

Try to create new project and use below mentioned code.

将"WebKit"框架导入您的类.

import "WebKit" framework to your class.

override func viewDidLoad() {
 super.viewDidLoad()
 let myWebView:WKWebView = WKWebView(frame: CGRect(x: 0.0, y: 0.0, width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height))
 myWebView.navigationDelegate = self;
 let url = URL(string: "https://www.Apple.com")!
 myWebView.load(URLRequest(url: url))
 self.view.addSubview(myWebView)
}

然后实现WKNavigationDelegate方法

then implement WKNavigationDelegate method

func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
UIApplication.shared.isNetworkActivityIndicatorVisible = false
let url = webView.url
print(url as Any) // this will print url address as option field
if url?.absoluteString.range(of: ".pdf") != nil {
     pdfBackButton.isHidden = false
     print("PDF contain")
   }
else {
     pdfBackButton.isHidden = true
     print("No PDF Contain")        
   } 
}

希望这对您有帮助!