NSString with emoticons/emojis url encode

前端 未结 3 849
粉色の甜心
粉色の甜心 2021-01-03 17:16

I am trying to take the contents of a UITextField that may contain special characters and emojis and turn it into something I can pass in a GET request to a PHP service.

相关标签:
3条回答
  • 2021-01-03 17:42

    NSNonLossyASCIIStringEncoding to stringByAddingPercentEscapesUsingEncoding: returns nil because the string you're passing in is not ASCII. Using NSUTF8StringEncoding percent escapes the emoji characters and that's the result I would expect. If your server-side middleware is not automatically unescaping the emoji from the query string, then you should look to do that in your server-side code if it's causing issues upstream.

    0 讨论(0)
  • 2021-01-03 17:47

    You can use the below code to encode emoji string

    NSString *uniText = [NSString stringWithUTF8String:[youremojistring UTF8String]];
    NSData *msgData = [uniText dataUsingEncoding:NSNonLossyASCIIStringEncoding];
    NSString *goodMsg = [[NSString alloc] initWithData:msgData encoding:NSUTF8StringEncoding];
    
    0 讨论(0)
  • 2021-01-03 17:58

    Use this to escape emoji:

    NSString *escapedEmoji = [NSString stringWithCString:[emojiString cStringUsingEncoding:NSNonLossyASCIIStringEncoding] encoding:NSUTF8StringEncoding];
    

    And this to unescape emoji:

    NSString *unescapedEmoji = [NSString stringWithCString:[escapedString cStringUsingEncoding:NSUTF8StringEncoding] encoding:NSNonLossyASCIIStringEncoding];
    
    0 讨论(0)
提交回复
热议问题