Search code examples
iosobjective-cspecial-charactersencodensurl

Encode URL contains special characters


I have a URL that contains special characters (% is used very much).. i want to encode the URL,how? this is one example of my my URLs: http://some.com//en/it/name-50%-other-set-50%-/68

I tried the following :

//  Option - 1.

NSString* str = [self.shareURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

//  Option - 2. 

NSString *unsafeString = @"this &string= confuses ? the InTeRwEbZ";
CFStringRef safeString = CFURLCreateStringByAddingPercentEscapes (
    NULL,
    (CFStringRef)unsafeString,
    NULL,
    CFSTR("/%&=?$#+-~@<>|\\*,.()[]{}^!"),
    kCFStringEncodingUTF8
);

//  Option - 3.

NSString *escapedString = [unescaped stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLHostAllowedCharacterSet]]; 

Solution

  • AS you said, http://some.com//en/it/name-50%-other-set-50%-/68 is a bad URL. What is the good URL you're supposed to have ?

    And why is there // after .com?