Compare NSURL String and simple Swift String

4.4k Views Asked by At

I have a function in Xcode 6.2 where I want to compare a URL returned from webview and compare it with a static string. I am doing it like this:

  func webViewDidFinishLoad(webView: UIWebView){
        let currentURL = webView.request?.URL
        print("Webview did finish load ")
        println(currentURL)



        if (webView.request?.URL == "Optional(http://addi.star.com/adminpanel/first.php/login)") 
        {

        println("voilaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")

        // code for dissming the view
        //self.dismissViewControllerAnimated(true, completion: nil)

        }
   }

...but it's not working. Can someone please explain how I can compare currentURL to static URL so that I can dismiss the view? I think I'm missing the types to compare it successfully, but there must be a way to do this...

2

There are 2 best solutions below

1
dopcn On BEST ANSWER

just compare string

if (webView.request?.url?.absoluteString == "http://addi.star.com/adminpanel/first.php/login"){
    //code
}
1
Muzahid On
 func webViewDidFinishLoad(webView: UIWebView){
            let currentURL = webView.request?.URL
            print("Webview did finish load ")
            println(currentURL)

    if let urlStr = webView?.request?.URL?.absoluteString where urlStr == "http://addi.star.com/adminpanel/first.php/login"{
        println("voilaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")

           // code for dissming the view
            //self.dismissViewControllerAnimated(true, completion: nil)

    }


        }

Hope this will help you.