I make a request to an external API using GET method. For some reasons, the results which I get do not work if the searchText contains spaces e.g. it works fine for stackoverflow, but if searchText is entered as "stack overflow", it will not work (Assume both the values are present). Do I need to escape before making the URL request?
NSString *urlstr = [[NSString alloc] initWithFormat:@"http://mysite.com/xyz?nameBeginsWith=%@", searchText];
The method stringByAddingPercentEscapesUsingEncoding
is now deprecated.
The correct way to escape urls is:
NSString * escapedSearchText = [searchText stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
NSString * url = [NSString stringWithFormat:@"http://example.com/xyz?nameBeginsWith=%@", escapedSearchText];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With