0

I am building chat app and need to send and receive emoji images. I use following code to post data :

 let myUrl = NSURL(string: "http://test.php")  
 let request = NSMutableURLRequest(URL:myUrl!)
 request.HTTPMethod = "POST"    
 let postString = " shareImageMessage=\(message)"
 request.HTTPBody = postString.dataUsingEncoding(NSUTF8StringEncoding)

I can see my message with emojies in debug area as follows :

Debug area output

But only following image display in mysql database :

mysql screenshot

And following code from php script :

$shareImageMessage = $_POST["shareImageMessage"];

I couldn't understand why only one kind of image transferring.

OzzY
  • 221
  • 5
  • 16
  • You need to show the code you're actually using to insert the data into the database. – Pekka Apr 11 '16 at 08:54
  • I don't see how your external database issue could be either iOS or Swift-related... have you debugged what the PHP script receives? – holex Apr 11 '16 at 08:57
  • It could be a rendering issue. The place where you print the output may not have the correct rendering for other emoji. – Pankaj Apr 11 '16 at 08:59

2 Answers2

0

To send more than one emoticon in the same url simply convert the string you are sending to the server to a base64 and then decode it on the other end of the database. To Encode and Decode strings to base64 you can check this Convert between UIImage and Base64 string. :)

Community
  • 1
  • 1
Mwangi Gituathi
  • 305
  • 2
  • 10
0

SWIFT: encode the message with base64 like so:

func base64encode(message: NSString)->String{
  let plainData = message.dataUsingEncoding(NSUTF8StringEncoding)
  return plainData.base64EncodedStringWithOptions(NSDataBase64EncodingOptions.fromRaw(0))!
}

usage : let msgToPost = base64encode("born to be encoded ")

then in PHP just use the famous $decodedmsg = base64_decode($str);

this code worked perfectly for me

one more thing.. make sure that your database column supports emoji, you can refer to the question for more detail about fixing the database emoji thing

How to insert utf-8 mb4 character(emoji in ios5) in mysql?

Community
  • 1
  • 1
Med Abida
  • 1,214
  • 11
  • 31
  • Your function changed to the following format by Xcode and doesn't post emoticons : return plainData!.base64EncodedStringWithOptions(NSDataBase64EncodingOptions(rawValue: 0)) – OzzY Apr 11 '16 at 09:53
  • My MySQL version is 5.0. I tried alter table command but encountered following error "#1115 - Unknown character set: 'utf8mb4'" . And also, there is no utf8mb4 option in Collation drop down menu. I think it is related with MySQL version. – OzzY Apr 11 '16 at 10:34
  • I think that version 5.5 is a must in order to use utf8mb4 – Med Abida Apr 11 '16 at 10:36