Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get the URL from webView in swift

I have question, how can I fetch the url from the webView? I perform the following code and I get the nil

Code I'm trying :

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(true)
    webView.loadRequest(URLRequest(url: URL(string: "https://www.youtube.com/watch?v=Vv2zJErQt84")!))
    if let text = webView.request?.url?.absoluteString{
         print(text)
    }
}
like image 279
Haider Ahmed Avatar asked Dec 03 '16 10:12

Haider Ahmed


People also ask

How do I see WebView in Swift?

Loading URL to WebView is very easy. Just create a WebView in your storyboard and then you can use the following code to load url. let url = NSURL (string: "https://www.simplifiedios.net"); let request = NSURLRequest(URL: url!); webView. loadRequest(request);

How do you get current URL of WebView in react native?

To get current URL with React Native webview, we can set the onNavigationStateChange prop to a function that listens for navigation events in the webview. to set onNavigationStateChange to the onNavigationStateChange function. In it, we log webViewState. url which has the URL of the current page in the webview.

What is the use of WebView in Swift?

WebView can be defined as an object which can display the interactive web content and load HTML strings within the iOS application for an in-app browser. It is an instance of the WKWebView class, which inherits the UIView class.


2 Answers

You are not getting url because webView has not finished the loading of that requested URL, you can get that URL in webViewDidFinishLoad method of UIWebviewDelegate. For that you need to set delegate of webView with your current ViewController and need to implement UIWebviewDelegate.

webView.delegate = self

Now you can get current loaded URL of webView in webViewDidFinishLoad method.

func webViewDidFinishLoad(_ webView: UIWebView) {
    if let text = webView.request?.url?.absoluteString{
         print(text)
    }
}
like image 73
Nirav D Avatar answered Sep 21 '22 03:09

Nirav D


For swift 4 and swift 4.2 use let url = webView.url?.absoluteString :-

First import WebKit :-

import WebKit

Then add protocol :-

class ViewController: UIViewController, WKNavigationDelegate

Then add delegate:-

    //MARK:- WKNavigationDelegate

func webView(_ webView: WKWebView, didFailProvisionalNavigation navigation: WKNavigation!, withError error: Error) {
    print(error.localizedDescription)
}
func webView(_ webView: WKWebView, didStartProvisionalNavigation navigation: WKNavigation!) {
    print("Strat to load")

    if let url = webView.url?.absoluteString{
        print("url = \(url)")
    }
}
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
    print("finish to load")

    if let url = webView.url?.absoluteString{
        print("url = \(url)")
    }
}
like image 26
Bijender Singh Shekhawat Avatar answered Sep 22 '22 03:09

Bijender Singh Shekhawat