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

穿精又带淫゛_ 提交于 2019-12-08 11:26:14

问题


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:

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.


回答1:


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;
@end

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]];
        else
            [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;
}
@end

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];

Disclaimer:

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.



来源:https://stackoverflow.com/questions/3556471/how-to-encode-hebrew-string-nsstring-into-a-unicode-format-in-order-to-send-as

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!