Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

URL Encoding a string [closed]

i am receiving a json data object and then i extract a string from it

NSDictionary *jsonDictionary = [NSJSONSerialization JSONObjectWithData:data
                                                               options:0
                                                                 error:nil];
NSString *country=jsonDictionary[@"address"][@"country"];

then i try to make the string suitable to be used in a URL

NSString *newCountryString = [country stringByReplacingOccurrencesOfString:@" "  
   withString:@"%%20"];

but it is not working

if i hard coded the newCountryString it would work, why is that ?

like image 215
B.I.A Avatar asked Jan 28 '14 10:01

B.I.A


People also ask

What does %20 do in URL?

A space is assigned number 32, which is 20 in hexadecimal. When you see “%20,” it represents a space in an encoded URL, for example, http://www.example.com/products%20and%20services.html.

What does %2 mean in a URL?

The % indicates an escaped character. It's a hexadecimal number that follows in the next two characters. In your example that is %2C , which is the hexadecimal number for the comma. Unescaped that becomes asset=travel,car,house,business.

What is %20 in query string?

According to the W3C (and they are the official source on these things), a space character in the query string (and in the query string only) may be encoded as either " %20 " or " + ".

How do you add %20 to a URL?

Spaces are not allowed in URLs. They should be replaced by the string %20. In the query string part of the URL, %20 can be abbreviated using a plus sign (+).


2 Answers

Use This -

NSString *newCountryString = [country stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

This code will return a representation of the receiver using a given encoding to determine the percent escapes necessary to convert the receiver into a legal URL string.

for more details: https://developer.apple.com/documentation/foundation/nsstring/1415058-stringbyaddingpercentescapesusin

Edit - stringByAddingPercentEscapesUsingEncoding is deprecated in iOS 9. Use following instead

[country stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLHostAllowedCharacterSet]]

Swift 3 -

country.addingPercentEncoding( withAllowedCharacters: .urlHostAllowed)

for more details: https://developer.apple.com/documentation/foundation/nsstring/1411946-stringbyaddingpercentencodingwit?language=objc

like image 78
Akshay Nalawade Avatar answered Sep 23 '22 00:09

Akshay Nalawade


As variant you can use method below:

- (NSString *)URLEncodeStringFromString:(NSString *)string
{
 static CFStringRef charset = CFSTR("!@#$%&*()+'\";:=,/?[] ");
 CFStringRef str = (__bridge CFStringRef)string;
 CFStringEncoding encoding = kCFStringEncodingUTF8;
 return (NSString *)CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(NULL, str, NULL, charset, encoding));
}
like image 37
Ruslan Soldatenko Avatar answered Sep 21 '22 00:09

Ruslan Soldatenko