How the F*** to Encode URL in Objective-C for IOS

Google it.

You’ll find there are a ton of solutions, all of them produce memory related errors because of ARC.

if you want to do it in a way that works, just:

NSString *charactersToEscape = @"!*'();:@&=+$,/?%#[]" ";
    NSCharacterSet *allowedCharacters = [[NSCharacterSet characterSetWithCharactersInString:charactersToEscape] invertedSet];
    
    NSString *url = [NSString stringWithFormat:@"%@", @"http://stuff.com/things/"];
    NSString *encodedUrl = [url stringByAddingPercentEncodingWithAllowedCharacters:allowedCharacters];

Jesus…this took me way too long to figure out

6 thoughts on “How the F*** to Encode URL in Objective-C for IOS

  1. you saved me a night of wasting my time with this FU**ing Apple ios objective shit.
    I own you a beer! Thank you!!

  2. What I’m missing in following code.

    int main (int argc, const char * argv[]) {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
    NSString *charactersToEscape = @”!*'();:@&=+$,/?%#[]”;
    NSCharacterSet *allowedCharacters = [[NSCharacterSet characterSetWithCharactersInString:charactersToEscape] invertedSet];

    NSString *encodedUrl = [@”test=” stringByAddingPercentEncodingWithAllowedCharacters:allowedCharacters];

    NSLog (@”hello world”);
    [pool drain];
    return 0;
    }

    I m getting error:

    : Uncaught exception NSInvalidArgumentException, reason: -[NSConstantString stringByAddingPercentEncodingWithAllowedCharacters:]: unrecognized selector sent to instance 0x601270

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.