Codificación de argumentos de cadena para las URL
He creado un método para crear URL para mí.
- (NSString *)urlFor:(NSString *)path arguments:(NSDictionary *)args
{
NSString *format = @"http://api.example.com/%@?version=2.0.1";
NSMutableString *url = [NSMutableString stringWithFormat:format, path];
if ([args isKindOfClass:[NSDictionary class]]) {
for (NSString *key in args) {
[url appendString:[NSString stringWithFormat:@"&%@=%@", key, [args objectForKey:key]]];
}
}
return url;
}
Cuando intento construir algo como abajo, las URL no están codificadas, por supuesto.
NSDictionary *args = [NSDictionary dictionaryWithObjectsAndKeys:
@"http://other.com", @"url",
@"ABCDEF", @"apiKey", nil];
NSLog(@"%@", [self urlFor:@"articles" arguments:args]);`
El valor devuelto eshttp://api.example.com/articles?version=2.0.1&url=http://other.com&apiKey=ABCDEF cuando debería serhttp://api.example.com/articles?version=2.0.1&url=http%3A%2F%2Fother.com&apiKey=ABCDEF.
Necesito codificar tanto la clave como el valor. Busqué algo y encontréCFURLCreateStringByAddingPercentEscapes ystringByAddingPercentEscapesUsingEncoding Pero ninguna de las pruebas que hice funcionó.
¿Cómo puedo hacerlo?