Search code examples
swifturlnsstringnsurlurl-parameters

I need to save an url parameter with Swift


I have an URL like this:

let myURL = http://localhost/#access_token=12312312.1231231.31321 

I need to take "12312312.1231231.31321" from that URL. I've tried with componentsSeparatedByString but I don't get what I need.


Solution

  • Looking at your question, is really just what you have mention using componentsSeparatedByString

    let myURL = "http://localhost/#access_token=12312312.1231231.31321"
    let strings = myURL.components(separatedBy: "=")
    print(strings[1])    //strings[1] will give you the "12312312.1231231.31321"
    

    componentsSeparatedByString will return you an array of strings that is separated by "=" in this case.