Search code examples
iosuiwebviewswift3

Accessing the URL of a UIWebView in Swift 3


I am trying to access the URL of a UIWebView in Swift 3. Here is the code that I am using to accomplish this (the name of my webview is RootView):

let currentURL : NSString = RootView.request?.URL?.absoluteString

However, I get an error saying "Value of type 'URLRequest' has no member 'URL'" that prevents me from compiling. I know that previous stack overflow users have used this same line of code successfully, so I am not sure what is preventing me from also obtaining the URL. Could the problem lie in a change that Apple made to Swift 3? Thanks!

Here is my full ViewController.swift file where I put this code:

import UIKit

class ViewController: UIViewController, UIWebViewDelegate {

    @IBOutlet weak var RootView: UIWebView!

    override func viewDidLoad() {
        super.viewDidLoad()
        self.RootView.delegate = self
        let url1 = URL (string: "https://www.google.com")!
        RootView.loadRequest(URLRequest(url: url1))
        RootView.scrollView.isScrollEnabled = true
        RootView.isUserInteractionEnabled = true
        let currentURL : NSString = RootView.request?.URL?.absoluteString
        print(currentURL)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
}

Solution

  • You have three options to access the url

    let currentURL : String? = RootView.request?.mainDocumentURL?.absoluteString
    let currentURL : String? = RootView.request?.url?.absoluteString
    let currenturl : String? = RootView.stringByEvaluatingJavaScript(from: "window.location.href")
    

    but all of these three values would return you nil, as request need time, they won't be ready until the request finished. Have a try.

    import UIKit
    
    class URLResearchVC: UIViewController , UIWebViewDelegate {
    
        @IBOutlet weak var RootView: UIWebView!
    
        override func viewDidLoad() {
            super.viewDidLoad()
            self.RootView.delegate = self
            let url1 = URL (string: "https://www.google.com")!
            RootView.loadRequest(URLRequest(url: url1))
            RootView.scrollView.isScrollEnabled = true
            RootView.isUserInteractionEnabled = true
            let currentURL : String? = RootView.request?.mainDocumentURL?.absoluteString
            let currentUrl : String? = RootView.request?.url?.absoluteString
            let currenturl : String? = RootView.stringByEvaluatingJavaScript(from: "window.location.href")
            print(currentURL)
            print(currentUrl)
            print(currenturl)
        }
    
        override func didReceiveMemoryWarning() {
            super.didReceiveMemoryWarning()
        }
    
        func webViewDidStartLoad(_ webView: UIWebView) {
            print("-------")
            print(webView.request?.mainDocumentURL)
            print("-------")
            print(webView.request?.url?.absoluteString)
            print("-------")
            print(webView.stringByEvaluatingJavaScript(from: "window.location.href"))
        }
        func webViewDidFinishLoad(_ webView: UIWebView) {
            print("-------")
            print(webView.request?.mainDocumentURL)
            print("-------")
            print(webView.request?.url?.absoluteString)
            print("-------")
            print(webView.stringByEvaluatingJavaScript(from: "window.location.href"))
        }
    }