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)
}
}
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);
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.
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.
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)
}
}
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)")
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With