Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the best practic using url string with NSString stringWithFormat

I use NSString stringWithFormat method for create an URL string. But now I have problem with a "quick" editing this string.

For example I have an script on the server that process some request with parameters.

I have an URL string like this:

http://www.domain.com/api/?param1=%@&param2=%@&param3=%@&param4=%@&param5=%@&

but when I have more than 5, 6 parameters it is really hard to modify this string.

Anybody knows best method how to create URL string (I mean when we modify it).

like image 652
Matrosov Oleksandr Avatar asked Mar 04 '26 06:03

Matrosov Oleksandr


1 Answers

This is a sample of how to add parameters in a safe way. Long but reliable.

NSString* const kBaseURL = @"http://maps.google.com/maps/api/geocode/xml";
NSMutableDictionary *parameterDic = [NSMutableDictionary dictionary];
[parameterDic setObject:@"plaza de la puerta del sol 1, madrid, spain" forKey:@"address"];
[parameterDic setObject:@"false" forKey:@"sensor"];

NSMutableArray *parameters = [NSMutableArray array];
for (__strong NSString *name in parameterDic) {
    NSString *value = [parameterDic objectForKey:name];
    name = encodeToPercentEscapeString(name);
    value = encodeToPercentEscapeString(value);
    NSString *queryComponent = [NSString stringWithFormat:@"%@=%@", name, value];
    [parameters addObject:queryComponent];
}
NSString *query = [parameters componentsJoinedByString:@"&"];
NSString *urlString = [NSString stringWithFormat:@"%@?%@", kBaseURL, query];
NSURL *url = [NSURL URLWithString:urlString];
NSLog(@"%@",url);

The code above calls this C function because stringByAddingPercentEscapesUsingEncoding won't convert some special characters in the name or value of the parameters. As pointed by Jesse Rusak see Proper URL (Percent) Encoding in iOS for a discussion.

// remove CFBridgingRelease and __bridge if your code is not ARC
NSString* encodeToPercentEscapeString(NSString *string) {
    return (NSString *)
        CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(NULL,
                                            (__bridge CFStringRef) string,
                                            NULL,
                                            (CFStringRef) @"!*'();:@&=+$,/?%#[]",
                                            kCFStringEncodingUTF8));
}

This prints

http://maps.google.com/maps/api/geocode/xml?sensor=false&address=plaza%20de%20la%20puerta%20del%20sol%201,%20madrid,%20spain

Bonus track: how to deconstruct and rebuild a string:

NSString *stringUrl = @"http://www.google.com:80/a/b/c;params?m=n&o=p#fragment";
NSURL *url = [NSURL URLWithString:stringUrl];
NSLog(@"%@",stringUrl);
NSLog(@"         scheme: %@",[url scheme]);
NSLog(@"           host: %@",[url host]);
NSLog(@"           port: %@",[url port]);
NSLog(@"           path: %@",[url path]);
NSLog(@"   relativePath: %@",[url relativePath]);
NSLog(@"parameterString: %@",[url parameterString]);
NSLog(@"          query: %@",[url query]);
NSLog(@"       fragment: %@",[url fragment]);

NSMutableString *s = [NSMutableString string];
[s appendFormat:@"%@://%@",[url scheme],[url host]];
if ([url port]!=nil){
    [s appendFormat:@":%@",[url port]];
}
[s appendFormat:@"%@",[url path]];
if ([url parameterString]!=nil){
    [s appendFormat:@";%@",[url parameterString]];
}
if ([url query]!=nil){
    [s appendFormat:@"?%@",[url query]];
}
if ([url fragment]!=nil){
    [s appendFormat:@"#%@",[url fragment]];
}
NSLog(@"%@",s);

This prints

http://www.google.com:80/a/b/c;params?m=n&o=p#fragment
         scheme: http
           host: www.google.com
           port: 80
           path: /a/b/c
   relativePath: /a/b/c
parameterString: params
          query: m=n&o=p
       fragment: fragment
like image 62
Jano Avatar answered Mar 06 '26 19:03

Jano



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!