IOS Swift: cannot get current url loaded in UIwebView

I work in a webview iOS swift application:

the problem I am facing is that I want to get every URL displayed by the webview:

at the urls I have to do some if else checks. but I could not get the URLs that the webview loads.

webview displays small results and loads urls when clicked. I want to get all the URLs that the webview is navigating to.

import Foundation import UIKit class seconVC: UIViewController { var toPass:String! var touser:String! var toPassing:String! @IBOutlet weak var webV: UIWebView! @IBOutlet weak var L_back: UIBarButtonItem! @IBOutlet weak var R_frwrd: UIBarButtonItem! override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. var domurl = toPass; var username = touser; var passwing = toPassing; var sendurl = stringA+username+"/"+passwing; //exchange login with auth/username/password println(sendurl); let url = NSURL (string: sendurl); let requestObj = NSURLRequest(URL: url!); webV.userInteractionEnabled = true webV.loadRequest(requestObj); } @IBAction func L_back(sender: UIBarButtonItem) { if (webV.canGoBack){ webV.goBack() } } @IBAction func R_frwrd(sender: UIBarButtonItem) { if (webV.canGoForward){ webV.goForward() } } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } } 

all i want is to get a string of current urls and navigation buttons for web browsing: please help? I referred to all Internet solutions, in my case it does not work.

+3
ios swift uiwebview
source share
3 answers

There are a couple of UIWebViewDelegate like shouldStartLoadWithRequest or webViewDidStartLoad or webViewDidFinishLoad or didFailLoadWithError to help you achieve your goal. If you want to perform the operation after the view has finished, run this delegate method

Swift 3

 if let currentURL = webView.request?.url?.absoluteString{ print(currentURL) } 

as he mentioned here .

+6
source share

parameter-1

 let currentURL = webV.request.URL.absoluteString 

parameter-2

get url in delegate method

 func webViewDidFinishLoad(webView: UIWebView){ print(WebView.request?.mainDocumentURL) } 

Option-3

 func webViewDidFinishLoad(webView: UIWebView) { let currentURL = webView.request().URL() print("\(currentURL.description)") } 

or use as

  func webViewDidFinishLoad(webView: UIWebView){ let currentURL = webView.request?.URL } 

parameter-4

 func webViewDidFinishLoad(webView: UIWebView) { let currentURL: = webView.stringByEvaluatingJavaScriptFromString("window.location.href")! print("\(currentURL)") } 

parameter-5

 func webView(webview: UIWebView, shouldStartLoadWithRequest request: NSURLRequest, navigationType: UIWebViewNavigationType) -> Bool { let currentURL = request.URL.absoluteString print("\(currentURL)") } 

options-6

 func webViewDidFinishLoad(WebVie: UIWebView) { let currentURL = WebVie.request.mainDocumentURL print("\(currentURL)") } 
+5
source share

You must first define a delegate for the WebView class. "WebView.delegate = self". Add this code next to one of the functions above:

 override func viewDidLoad() { super.viewDidLoad() myWebView.delegate = self } 

and for example:

 func webViewDidFinishLoad(_ webView: UIWebView) { if let currentURL = WebView.request?.url?.absoluteString{ print(currentURL) } } 
0
source share

All Articles