Вопрос


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...

Это было полезно?

Решение 2

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.

Другие советы

The same functionality i have implemented in my application also.

If you are using webView to use this

NSURL* url = [[webView request] URL];

NSString *string =  [[url absoluteString] stringByReplacingOccurrencesOfString:@"#" withString:@"&"]; (here it's tweak to chnage '#' to '&')

NSArray *components = [string componentsSeparatedByString:@"&"];
NSLog(@"queryParams %@", components); 

Basically we need to divide our URL in to two sub-part , according this you will get main url and other part as list of parameter.`

Here you will get all the parameter related to your URL

Hope this will help you.

I thin these code will help you:

NSArray *query = @"https://oauth.vk.com/blank.html#access_token=b1b33dfaa3&expires_in=86400&user_id=311515 ";
NSMutableDictionary *params = [[NSMutableDictionary alloc] init];
for (NSString *param in [query componentsSeparatedByString:@"&"]) {
    NSArray *parts = [param componentsSeparatedByString:@"="];
    if([parts count] < 2) continue;
    [params setObject:[parts objectAtIndex:1] forKey:[parts objectAtIndex:0]];
}
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top