Question

I am trying to submit a request using NSURL to the following:

http://api.allsaints.com/v1/product/?category=Women's Knitwear

when putting this URL in a browser with the ' and the space int the category, the browser (chrome) is modifying the request to:

http://api.allsaints.com/v1/product/?category=Women%27s%20Knitwear which works.

However, I can not get the right encoding to work as part of a NSURL and NSDATA request on iOS. I tried various encoding using stringByAddingPercentEscapesUsingEncoding to normalise the URL but it keep on failing.

Can someone point me in the right direction to an encoding that not only swaps the space to a %20 but also swaps the ' to a %27?

Was it helpful?

Solution

Refer to NSString stringByAddingPercentEscapesUsingEncoding: you should use CFURLCreateStringByAddingPercentEscapes to custom which character you want to escape.

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

CFSTR("!*'();:@+$,/?%#[]") contains the characters will be escaped.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top