Encoding string arguments for URLs

后端 未结 3 695
误落风尘
误落风尘 2021-01-25 03:22

I created a method to build URLs for me.

- (NSString *)urlFor:(NSString *)path arguments:(NSDictionary *)args
{
    NSString *format = @\"http://api.example.com/         


        
3条回答
  •  滥情空心
    2021-01-25 03:42

    IIRC, slashes should be interpreted properly when they're in the query part of a URL. Did you test to see if it still works without encoded slashses? Otherwise, do something like:

    if ([args isKindOfClass:[NSDictionary class]]) {
            for (NSString *key in [args allKeys]) {
                NSString *value = [(NSString*)CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault, (CFStringRef)[args objectForKey:key], NULL, CFSTR("/?&:=#"), kCFStringEncodingUTF8) autorelease];
                [url appendString:[NSString stringWithFormat:@"&%@=%@", key, value]];
                [value release];
            }
    }
    
    return url;
    

    Note the value of the 4th argument to CFURLCreateStringByAddingPercentEscapes.

提交回复
热议问题