Search code examples
iosnsurl

Get a parameter from NSURL



In my application I need to get parameters "access_token" and "user_id" from this url :
https://oauth.vk.com/blank.html#access_token=b1b33dfaa3&expires_in=86400&user_id=311515

Here is My code for accessing the "user_id" parameter in webViewDidFinishLoad:

NSArray *userAr = [[[[webView request] URL] absoluteString] componentsSeparatedByString:@"&user_id="];

NSString *user_id = [userAr lastObject];


How do I get the "access_token"? If I use the same method it doesn't work...


Solution

  • Try to understand the code. componentsSeparatedByString creates an array of smaller strings that are separated by &user_id=. This means, the output for you would be:

    Array of ["https://oauth.vk.com/blank.html#access_token=b1b33dfaa3&expires_in=86400", "311515"]
    

    Now you see why the lastObject gets you the value of user_id. Now try to use the same methods to get the value of access_token.

    Hint: you need to set & as the separator.