Prasad
Prasad

Reputation: 1914

how to capture the required values from a URL

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?

Upvotes: 1

Views: 346

Answers (3)

chings228
chings228

Reputation: 1889

   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);
    }

Upvotes: 0

Dmitry
Dmitry

Reputation: 1041

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

Upvotes: 0

Nick Lockwood
Nick Lockwood

Reputation: 40995

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;
}

Upvotes: 1

Related Questions