Search code examples
iosswiftuiwebviewios8

Getting query string parameters from url in the UIWebView in Swift?


my question is pretty straight forward. Can someone please show me how to get the query string parameters in a url received by the UIWebView. It is pretty easy in objective c, but I need some help in swift as i'm new to the language. Thanks in advance.


Solution

  • In the NSURL class exists the .query property which returns the string of everything after the ? in the url in form: http://www.example.com/index.php?key1=value1&key2=value2 in this example using the code:

    var url: NSURL = NSURL(string: "http://www.example.com/index.php?key1=value1&key2=value2")
    println(url.query) // Prints: Optional("key1=value1&key2=value2")
    

    More information in the documentation

    As far as getting the url from the uiwebview, you could use something along the lines of:

    let url: NSURL = someWebView.NSURLRequest.URL