3

Is there something like

+(NSString *) URLencode: (NSString *) someString


+(NSString *) URLdecode: (NSString *) someString

If so, how to implement it?

Note to downvoters. This is NOT a simple question. I need something that can comprehensively do this. For example:

NSString * test = @"汉字马拉松是";
NSString * encoded = [test stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding];
PO(encoded);
PO(test);

Will yield the following:

2012-06-04 16:04:22.709 BadgerNew[2266:17003] <0xcd890 GrabClass.m:(930)> encoded (null)
2012-06-04 16:04:22.710 BadgerNew[2266:17003] <0xcd890 GrabClass.m:(931)> test 汉字马拉松是

I want my encoding to be able to URL encode EVERYTHING including japanese language, etc.

So I want encoded to contain something along %E6%B1%89%E5%AD%97%E9%A9%AC%E6%8B%89%E6%9D%BE%E6%98%AF

There are 2 different methods in http://mobiledevelopertips.com/networking/a-better-url-encoding-method.html

None are good enough.

I need to be able to encode ALL strings, including japanese/chinese characters.

Update: I followed H2CO3 answer and do this: NSString * test = @"汉字马拉松是"; NSString * encoded = [test stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; PO(encoded); PO(test); PO(encoded);

Work as expected.

user4951
  • 32,206
  • 53
  • 172
  • 282
  • "If so, how to implement it" <-- if there is, don't implement it. Use it. –  May 31 '12 at 17:58
  • Well, I think there is a way to do it but not as simple as having Apple nicely give a function. I saw an answer where you have to create a function. Turns out there is a way that works better. – user4951 May 31 '12 at 18:18
  • If you had read the documentation in advance, you would have found @Leon Lucardie's methods for NSString. –  May 31 '12 at 18:20
  • Actually his method is not good enough. Check http://mobiledevelopertips.com/networking/a-better-url-encoding-method.html I need to be able to encode ALL strings, including japanese/chinese characters. – user4951 May 31 '12 at 18:22
  • I strongly suspect it's something you need to implement and not something apple just give. If apple does give then yes use it rather than implement it. – user4951 May 31 '12 at 18:23
  • @H2CO3 Leon method is not good enough for my purpose. You speak so much yet know nothing. – user4951 Jun 04 '12 at 09:23
  • I know nothing? Consider having a look at my works on GitHub. –  Jun 04 '12 at 09:59
  • Okay fine. Then if you know so much about this issue, why not answer it? Try something that can encode 汉字马拉松是 to %E6%B1%89%E5%AD%97%E9%A9%AC%E6%8B%89%E6%9D%BE%E6%98%AF Get something working. – user4951 Jun 04 '12 at 10:05
  • I had already provided the solution. Call the dedicated Cocoa methods *with an appropriate encoding*. –  Jun 04 '12 at 11:03
  • There is no such method. If there is I wouldn't ask here. Try your self. The solution given by Leon doesn't work. – user4951 Jun 04 '12 at 18:42
  • How do you think you can encode Chinese/Japanese characters in ASCII? You should try with UTF-8 (recommended, `NSUTF8StringEncoding`), UTF-16 (`NSUTF16StringENcoding`) and `NSShiftJISStringEncoding` instead. –  Jun 04 '12 at 18:49
  • Would you turn that into an answer. That's exactly what I am looking for thanks. Sorry for underestimating you :) – user4951 Jun 05 '12 at 02:36
  • Let me check. I have to actually verify that and my gut feeling says it's not going to be that easy. – user4951 Jun 05 '12 at 05:47

2 Answers2

4

You probably want to take a look at the stringByAddingPercentEscapesUsingEncoding and stringByReplacingPercentEscapesUsingEncoding methods.

Encoding example (using ASCII):

NSString* encodedString =
[originalString stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding];

Decoding example (using ASCII):

NSString* originalString =
[encodedString stringByReplacingPercentEscapesUsingEncoding:NSASCIIStringEncoding];

EDIT: If this doesn't give the desired results try replacing NSASCIIStringEncoding with NSUTF8StringEncoding.

Also you might want to try the variant of above methods :

NSString * encodedString =
  (NSString *)CFURLCreateStringByAddingPercentEscapes(
    NULL,
    (CFStringRef)originalString,
    NULL,
    (CFStringRef)@"!*'();:@&=+$,/?%#[]",
    kCFStringEncodingUTF8 );

Which will work better in some cases

Leon Lucardie
  • 9,541
  • 4
  • 50
  • 70
  • I need something more comprehensive. According to http://mobiledevelopertips.com/networking/a-better-url-encoding-method.html the method is not comprehensice enough. – user4951 May 31 '12 at 18:24
  • Revised my original answer with some extra steps you can try without resorting to custom implementations – Leon Lucardie May 31 '12 at 18:46
  • Try something that can encode 汉字马拉松是 to %E6%B1%89%E5%AD%97%E9%A9%AC%E6%8B%89%E6%9D%BE%E6%98%AF – user4951 Jun 04 '12 at 09:24
1

It's natural that Chinese and Japanese characters don't work with ASCII string encoding. If you try to escape the string by Apple's methods, which you definitely should to avoid code duplication, store the result as a Unicode string. Use one of the following encodings:

NSUTF8StringEncoding
NSUTF16StringEncoding
NSShiftJISStringEncoding (not Unicode, Japanese-specific)
  • NSString * test = @"汉字马拉松是"; NSString * encoded = [test stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; PO(encoded); PO(test); PO(encoded); – user4951 Jun 05 '12 at 17:35
  • 2012-06-06 00:35:39.320 BadgerNew[10329:18d03] <0x10e92c GrabClass.m:(323)> encoded: %E6%B1%89%E5%AD%97%E9%A9%AC%E6%8B%89%E6%9D%BE%E6%98%AF – user4951 Jun 05 '12 at 17:36
  • You do know what you're talking about. Sorry for doubting you. – user4951 Jun 05 '12 at 17:36
  • No problem, I'm glad we finally managed to understand each other correctly. –  Jun 05 '12 at 17:48
  • Cool. No one else told me that and I have look at the web. It's even more comprehensive than other sources. – user4951 Jun 06 '12 at 04:57