user3606054
user3606054

Reputation: 591

iOS: NSASCIIStringEncoding doesn't encode certain characters correctly

I have url that I am going to request data from. Here is the code.

 urlStr = [urlStr stringByAddingPercentEscapesUsingEncoding:
 NSASCIIStringEncoding];

 ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:[NSURL URLWithString:urlStr]];

This works except when I have certain characters such as ş. The urlstr will return null. Is there a way around this to except certain character types? Any tips or suggestions are appreciated.

Upvotes: 0

Views: 958

Answers (1)

Mike
Mike

Reputation: 9835

I've used the following method with success in many applications:

- (NSString *)urlEncode:(NSString *)str {
    return (NSString *)CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(NULL, (CFStringRef)str, NULL, CFSTR("!*'();:@&=+$,/?%#[]"), kCFStringEncodingUTF8));
}

Note that I use this on only the params of the URL, so the following would work (notice I added the ş, which seemed to work, although I'm not familiar with that character):

NSString *baseURL = @"http://www.google.com";
NSString *paramsString = @"testKey=test value_with some (weirdness)!___ş";
NSString *resultingURLString = [NSString stringWithFormat:@"%@?%@", baseURL, [self urlEncode:paramsString]];

Which produces the result:

http://www.google.com?testKey%3Dtest%20value_with%20some%20%28weirdness%29%21___%C5%9F

Upvotes: 1

Related Questions