Reputation: 27153
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=%@¶m2=%@¶m3=%@¶m4=%@¶m5=%@&
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).
Upvotes: 2
Views: 1680
Reputation: 2332
I wrote this specially for you, quite simple:
+ (NSString*) URlStringForBaseURL:(NSString*)baseURL withParams:(NSDictionary*)paramsdictonary{
NSString* url = [baseURL stringByAppendingString:@"?"];
NSUInteger index = 0;
for (NSString* key in [paramsdictonary allKeys]) {
index++;
if (index == [paramsdictonary count])
url = [url stringByAppendingFormat:@"%@=%@",key,[paramsdictonary valueForKey:key]];
else
url = [url stringByAppendingFormat:@"%@=%@&",key,[paramsdictonary valueForKey:key]];
}
return url;
}
And you can use it (of course, the order of the URL params is dos not matter):
NSMutableDictionary* params = [NSMutableDictionary dictionary];
[params setValue:@"value1" forKey:@"param1"];
[params setValue:@"value2" forKey:@"param2"];
[params setValue:@"value3" forKey:@"param3"];
NSString* urlStr = [HTMLTextFormat URlStringForBaseURL:@"http://www.domain.com/api/" withParams:params];
NSLog(@"url_: %@",urlStr);
Upvotes: 4
Reputation: 63667
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
Upvotes: 6