String won't url encode in iOS

I’m seriously having a brain fart here, but I can’t figure out why this isn’t encoding for the life of me. Been searching all over, and I can’t even get the code samples to encode. Any ideas?

NSString *searchString = @"waffl&es";

NSString *encodedSearchString = [searchString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSString *urlString = [NSString stringWithFormat:@"http://en.wikipedia.org/?search=%@", encodedSearchString];
NSURL *url = [NSURL URLWithString:urlString];

  • Urlencode in Objective-C
  • URLEncoding a string with Objective-C
  • How to encode a URL in Swift
  • Swift - encode URL
  • NSString with emoticons/emojis url encode
  • is `addingPercentEncoding` broken in Xcode 9?
  • Solutions Collect From Internet About “String won't url encode in iOS”

    For future reference, this is what I found to work (i.e. encode everything properly)

    + (NSString*)encodeURL:(NSString *)string
    {
        NSString *newString = (__bridge_transfer NSString *)CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault, (__bridge CFStringRef)string, NULL, CFSTR(":/?#[]@!$ &'()*+,;=\"<>%{}|\\^~`"), CFStringConvertNSStringEncodingToEncoding(NSUTF8StringEncoding));
    
        if (newString) 
        {
            return newString;
        }
    
        return @"";
    }