iOS Swift : can't Retrieve current url loaded in UIwebView

萝らか妹 提交于 2019-12-05 19:31:05
iMuzahid

There are couple of UIWebViewDelegate such as shouldStartLoadWithRequest or webViewDidStartLoad or webViewDidFinishLoad or didFailLoadWithError that help you to accomplish your goal. If you want to perform operation after view did finished then implement this delegate method

Swift 3

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

as it mentioned here.

option-1

let currentURL = webV.request.URL.absoluteString

option-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 like

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

option-4

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

option-5

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

option-6

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

First you have to define the delegate for WebView class. "WebView.delegate = self". Add this code beside one of the function above:

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

and for exmaple:

func webViewDidFinishLoad(_ webView: UIWebView) {
    if let currentURL = WebView.request?.url?.absoluteString{
        print(currentURL)
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!