Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SWIFT: Why I can't get the current URL loaded in UIWebView? [closed]

I need to get the current URL loaded into the webview and this is how I'm trying to get that but it gives me this error: "Cannot convert the exporessions type 'ST7??' to type 'String'

and this is the code

 var currentURL : NSString = webView.request?.URL.absoluteString!

What is wrong with this?

like image 907
ernestocattaneo Avatar asked Dec 18 '14 15:12

ernestocattaneo


2 Answers

If you put parentheses around this, the error goes away:

let currentURL : NSString = (webView.request?.URL.absoluteString)!
like image 93
Rob Avatar answered Nov 08 '22 14:11

Rob


Beware that yours might not be just a syntax problem. If your webView is in a state where request == nil your app will crash at runtime.

I'd rather write something like:

if let currentURL = webView.request?.URL.absoluteString {

    // do things ...
    // Your currentURL will be automatically bridged to Swift's String type 

} else {

   // Just in case request is nil ...

}
like image 35
Matteo Piombo Avatar answered Nov 08 '22 15:11

Matteo Piombo