如何从URL捕获所需的值

时间:2012-01-13 11:28:27

标签: iphone ios ios4

我需要从字符串中提取变量的值,该字符串恰好是一个URL。字符串/ url作为单独的php查询的一部分加载,而不是浏览器中的url。

网址将如下所示:

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

如何捕获access_token的值,在本例中为ab8w4azq2xv3dr4ab37vvzmh?

3 个答案:

答案 0 :(得分:1)

此代码应该这样做:

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

或者,这是一个更通用的解决方案,它将所有查询参数提取到字典中:

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

答案 1 :(得分:0)

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

答案 2 :(得分: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);
    }