How to encode hebrew string (NSString) into a Unicode format in order to send as a URL in Objective-C

The title pretty much sums it up. I have a hebrew-containing String used in a NSUrl:

NSString * urlS = @"http://irrelevanttoyourinterests/some.aspx?foo=bar&this=that&Text=תל אביב"

I would like to convert in into:

  • UILabel - string as text and links
  • Replace letters in a string
  • Objective-C and Swift URL encoding
  • casting NSInteger into NSString
  • Parse NSURL query property
  • Swift string count() vs NSString .length not equal
  • Text=%u05EA%u05DC%20%u05D0%u05d1%u05d9%u05d1

    and then send it as a GET request.
    I have tried many encoding methods unsuccessfully, and eventually I tried statically inserting the encoded string into the URL, quoted above.

    NSURL *url1 = [NSURL URLWithString:urlS];

    Then I use startAsyncLoad which you may be familiar with (here), but when the URL is constracted with the static Unicode string, nothing gets sent (checked with Wireshark), although if I use the following line before the startAsyncLoad it sends (wrongly encoded, of course).

    urlS = [urlS stringByAddingPercentEscapesUsingEncoding:NSWindowsCP1254StringEncoding];

    Thanks in advance.

    Solutions Collect From Internet About “How to encode hebrew string (NSString) into a Unicode format in order to send as a URL in Objective-C”

    The percent escapes that you have given as an example are Unicode code points. This type of encoding is non-standard, so I think it is unlikely that there exists a method already that can do this. You may have to roll your own, but it’s not too difficult.

    In a header (perhaps called NSString+NonStandardPercentEscapes.h) put the following:

    #import <Foundation/Foundation.h>
    @interface NSString (NonStandardPercentEscapes)
    - (NSString *) stringByAddingNonStandardPercentEscapes;

    And, in a source file (perhaps called NSString+NonStandardPercentEscapes.m) put the following:

    #import "NSString+NonStandardPercentEscapes.h"
    @implementation NSString (NonStandardPercentEscapes)
    - (NSString *) stringByAddingNonStandardPercentEscapes
        NSCharacterSet *mustEscape = [NSCharacterSet characterSetWithCharactersInString:@"<>~\"{}|\\-`^% "];
        NSMutableString *result = [[NSMutableString alloc] init];
        NSUInteger length = [self length];
        unichar buffer[length];
        [self getCharacters:buffer range:NSMakeRange(0, length)];
        for (NSUInteger i = 0; i < length; i++)
            if ([mustEscape characterIsMember:buffer[i]])
                [result appendFormat:@"%%%02hhx", buffer[i]];
            else if (buffer[i] > 0xFF)
                [result appendFormat:@"%%u%04hx", buffer[i]];
            else if (!isascii((int)buffer[i]))
                [result appendFormat:@"%%%02hhx", buffer[i]];
                [result appendFormat:@"%c", buffer[i]];
        // return the mutable version, nobody will know unless they check the class
        return [result autorelease];
        // alternatively, you can force the result to be immutable
        NSString *immutable = [[result copy] autorelease];
        [result release];
        return immutable;

    Then, wherever you need to encode your Hebrew string, you can do the following (as long as your source file includes the above header):

    NSString * urlS = @"http://irrelevanttoyourinterests/some.aspx?foo=bar&this=that&Text=תל אביב";
    urlS = [urlS stringByAddingNonStandardPercentEscapes];
    NSUrl *url1 = [NSURL URLWithString:urlS];


    I have no idea what characters need to be escaped and at what point the escaping should start (this just encodes the entire URL which is probably not what you want), but the above code should at least get you under way.