1

I need to extract a variable's value from a string, which happens to be a URL. The string/url is loaded as part of a separate php query, not the url in the browser.

The url's will look like:

http://gmail.com?access_token=ab8w4azq2xv3dr4ab37vvzmh&token_type=bearer&expires_in=3600

How can I capture the value of the access_token which in this example is ab8w4azq2xv3dr4ab37vvzmh?

Prasad
  • 1,904
  • 4
  • 19
  • 24

3 Answers3

1

This code should do it:

- (NSString *)extractToken:(NSURL *)URL
{
    NSString *urlString = [URL absoluteString];
    NSRange start = [urlString rangeOfString:@"access_token="];
    if (start.location != NSNotFound)
    {
        NSString *token = [urlString substringFromIndex:start.location+start.length];
        NSRange end = [token rangeOfString:@"&"];
        if (end.location != NSNotFound)
        {
            //trim off other parameters
            token = [token substringToIndex:end.location];
        }
        return token;
    }
    //not found
    return nil;
}

Alternatively, here is a more general solution that will extract all the query parameters into a dictionary:

- (NSDictionary *)URLQueryParameters:(NSURL *)URL
{
    NSString *queryString = [URL query];
    NSMutableDictionary *result = [NSMutableDictionary dictionary];
    NSArray *parameters = [queryString componentsSeparatedByString:@"&"];
    for (NSString *parameter in parameters)
    {
        NSArray *parts = [parameter componentsSeparatedByString:@"="];
        NSString *key = [[parts objectAtIndex:0] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        if ([parts count] > 1)
        {
            id value = [[parts objectAtIndex:1] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
            [result setObject:value forKey:key];
        }
    }
    return result;
}
Nick Lockwood
  • 40,865
  • 11
  • 112
  • 103
0

Good Category for NSDictionary:

    #import "NSDictionary+URL.h"

@implementation NSDictionary (URL)
+ (NSDictionary *)dictionaryWithUrlString:(NSString *)urlString{
    NSRange urlRange = [urlString rangeOfString:@"?"];
    if(urlRange.length>0){
        urlString = [urlString substringFromIndex:urlRange.length+urlRange.location];
    }
    NSArray *pairsArray = [urlString componentsSeparatedByString:@"&"];

    NSMutableDictionary *parametersDictionary = [[NSMutableDictionary alloc] initWithCapacity:[pairsArray count]];
    for(NSString *pairString in pairsArray){

        NSArray *valuesArray = [pairString componentsSeparatedByString:@"="];
        if([valuesArray count]==2){
            [parametersDictionary setValue:[valuesArray objectAtIndex:1] forKey:[valuesArray objectAtIndex:0]];
        } 
    }
    return [parametersDictionary autorelease];
}
@end
Dmitry
  • 1,035
  • 2
  • 14
  • 27
0
   NSMutableDictionary *querycomponent = [[NSMutableDictionary alloc] init];

    if (![query isEqualToString:@""]){

        NSArray *queryArray = [query componentsSeparatedByString:@"&"];

        for (NSString *subquery in queryArray){

            NSArray *subqueryArray = [subquery componentsSeparatedByString:@"="];

            NSString *key = [subqueryArray objectAtIndex:0];
            NSString *val = [subqueryArray objectAtIndex:1];

            [querycomponent setObject:val forKey:key];

        }

        NSLog(@"querycomponent %@",querycomponent);
    }
chings228
  • 1,859
  • 24
  • 24