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)
}
}
Here's a Swift 3 version, make sure you have added
UIWebViewDelegate
in your class dseclaration and setwebView.delegate = self
inviewDidload()
:FYI:
If you want to see which url is navigating for load then use this delegate method. navigationAction specifically show which page is navigating. it can be diff than webview.url
For swift 4 and swift 4.2 use
let url = webView.url?.absoluteString
:-First import WebKit :-
Then add protocol :-
Then add delegate:-
You are not getting url because
webView
has not finished the loading of that requestedURL
, you can get thatURL
inwebViewDidFinishLoad
method ofUIWebviewDelegate
. For that you need to set delegate ofwebView
with your currentViewController
and need to implementUIWebviewDelegate
.Now you can get current loaded
URL
ofwebView
inwebViewDidFinishLoad
method.