Urlencode in Objective-C

六眼飞鱼酱① 提交于 2019-11-28 00:31:08

You don't show us how you created the MyData variable or what type it is, but I'm guessing it's a NSString that you might have constructed something like the following:

NSString *myString = [NSString stringWithFormat:@"%@=%@", parameterKey, parameterValue];
NSData   *postData = [myString dataUsingEncoding:NSUTF8StringEncoding];

What you need to do is to "percent escape" any characters that are defined as reserved per RFC 3986. Thus, you'd replace the above with:

NSString *myString = [NSString stringWithFormat:@"%@=%@", parameterKey, [self percentEScapeString:parameterValue]];
NSData   *postData = [myString dataUsingEncoding:NSUTF8StringEncoding];

where

- (NSString *)percentEscapeString:(NSString *)string
{ 
    return CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault,
                                                                     (CFStringRef)string,
                                                                     NULL,
                                                                     (CFStringRef)@":/?@!$&'()*+,;=",
                                                                     kCFStringEncodingUTF8));
}

Technically, per the W3C specs for application/x-www-form-urlencoded, you should replace spaces with + characters, thus:

- (NSString *)percentEscapeString:(NSString *)string
{ 
    NSString *result = CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault,
                                                                                 (CFStringRef)string,
                                                                                 (CFStringRef)@" ",
                                                                                 (CFStringRef)@":/?@!$&'()*+,;=",
                                                                                 kCFStringEncodingUTF8));
    return [result stringByReplacingOccurrencesOfString:@" " withString:@"+"];
}

Personally, I put these sorts of methods in a NSString category, rather than in the current class, but hopefully this illustrates the idea.

Regardless, do not be tempted to use stringByAddingPercentEscapesUsingEncoding, because that doesn't give you the control you need. You really need to use CFURLCreateStringByAddingPercentEscapes, as shown above.


You asked about the emoticons. The above percent escaping works fine with the emoticons. For example, consider:

NSString *string1 = @"Text !@# & ( | 'l;,. 😒😚😜";
NSString *string2 = [self percentEscapeString:string1];
NSString *string3 = [string2 stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSLog(@"%@", string1);
NSLog(@"%@", string2);
NSLog(@"%@", string3);
2013-12-25 18:20:16.391 PercentEncodeTest[67199:70b] Text !@# & ( | 'l;,. 😒😚😜
2013-12-25 18:20:16.397 PercentEncodeTest[67199:70b] Text%20%21%40%23%20%26%20%28%20%7C%20%27l%3B%2C.%20%F0%9F%98%92%F0%9F%98%9A%F0%9F%98%9C
2013-12-25 18:20:16.401 PercentEncodeTest[67199:70b] Text !@# & ( | 'l;,. 😒😚😜

As you can see, string2 is entirely percent escaped and should be correctly transmitted. And when we convert back to NSUTF8StringEncoding, we get our emoticons back fine.

I suspect that the problem now is not in the fact that the emoticons are correctly percent escaped, but rather your use of them by the destination.

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