0

タイトルはそれをかなり要約しています。NSUrl で使用されるヘブライ語を含む文字列があります。

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

私はに変換したい:

Text=%u05EA%u05DC%20%u05D0%u05d1%u05d9%u05d1

GET リクエストとして送信します。多くのエンコーディング方法を試してみましたがうまくいきませんでした。最終的には、エンコードされた文字列を上記の URL に静的に挿入しようとしました。

NSURL *url1 = [NSURL URLWithString:urlS];

次に、使い慣れているかもしれない startAsyncLoad を使用します (こちら) が、URL が静的な Unicode 文字列で構築されている場合、何も送信されません (Wireshark でチェック)。 、 もちろん)。

urlS = [urlS stringByAddingPercentEscapesUsingEncoding:NSWindowsCP1254StringEncoding];

前もって感謝します。

4

1 に答える 1

0

例として挙げたパーセント エスケープは Unicode コード ポイントです。このタイプのエンコーディングは非標準であるため、これを行う方法が既に存在する可能性は低いと思います。自分で巻く必要があるかもしれませんが、それほど難しくありません。

ヘッダー (おそらく と呼ばれるNSString+NonStandardPercentEscapes.h) に次のように記述します。

#import <Foundation/Foundation.h>

@interface NSString (NonStandardPercentEscapes)
- (NSString *) stringByAddingNonStandardPercentEscapes;
@end

そして、ソース ファイル (おそらく と呼ばれるNSString+NonStandardPercentEscapes.m) に次のように記述します。

#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

次に、ヘブライ文字列をエンコードする必要がある場合は、次の操作を実行できます (ソース ファイルに上記のヘッダーが含まれている場合)。

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

NSUrl *url1 = [NSURL URLWithString:urlS];

免責事項:

どの文字をエスケープする必要があり、どの時点でエスケープを開始する必要があるのか​​ わかりません(これはおそらくあなたが望むものではないURL全体をエンコードするだけです)が、上記のコードは少なくともあなたを始めるはずです.

于 2010-08-25T01:17:42.760 に答える