Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

webViewDidFinishLoad in WKWebView

I have to migrate from UIWebView to WKWebView and I'm having some trouble with this. It looks like that "WKWebView" has no member 'request'. Is there a way to handle this situation using the WKWebView? I simply need to get the URL loaded and then if it contains ".pdf" just show a button.

func webViewDidFinishLoad(_ webView : WKWebView) {
    UIApplication.shared.isNetworkActivityIndicatorVisible = false

    if (webView.request?.url!.absoluteString)!.range(of: ".pdf") != nil {
        pdfBackButton.isHidden = false
    }
    else {
    pdfBackButton.isHidden = true}

}
like image 718
ernestocattaneo Avatar asked Nov 22 '16 16:11

ernestocattaneo


People also ask

What is the difference between UIWebView and WKWebView?

Unlike UIWebView, which does not support server authentication challenges, WKWebView does. In practical terms, this means that when using WKWebView, you can enter site credentials for password-protected websites.

What is WKWebView in Swift?

A WKWebView object is a platform-native view that you use to incorporate web content seamlessly into your app's UI. A web view supports a full web-browsing experience, and presents HTML, CSS, and JavaScript content alongside your app's native views.

How do I migrate to WKWebView?

You can implement WKWebView in Objective-C, here is simple example to initiate a WKWebView : WKWebViewConfiguration *theConfiguration = [[WKWebViewConfiguration alloc] init]; WKWebView *webView = [[WKWebView alloc] initWithFrame:self. view. frame configuration:theConfiguration]; webView.

What browser is WKWebView?

WKWebView. WKWebView was introduced in iOS 8 allowing app developers to implement a web browsing interface similar to that of mobile Safari. This is due, in part, to the fact that WKWebView uses the Nitro Javascript engine, the same engine used by mobile Safari.


1 Answers

Add "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)
}

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")        
    } 
}

Hope this will help you!

like image 142
Mangesh Kondaskar Avatar answered Nov 04 '22 00:11

Mangesh Kondaskar