-1

I am looking for solution where i want to store English + Arabic + Emoji Character to store to Database and retrieve it back while display.

Below is the code what i have used to support Emoji, after that Arabic text is not showing.

+(NSString *)emojiToSave:(NSString *)str
{
    NSData *dataForEmoji = [str dataUsingEncoding:NSNonLossyASCIIStringEncoding];
    NSString *encodevalue = [[NSString alloc]initWithData:dataForEmoji encoding:NSUTF8StringEncoding];
    return encodevalue;
}
+(NSString *)emojiToDisplay:(NSString *)str
{
    NSData *msgData = [str dataUsingEncoding:NSUTF8StringEncoding];
    NSString *goodMsg = [[NSString alloc] initWithData:msgData encoding:NSNonLossyASCIIStringEncoding];
    return goodMsg;
}

Can anyone pls suggest to give support for Arabic what change i should do?

Thanks in advance.

Maheta Dhaval K
  • 764
  • 6
  • 19
  • Could you give a string for example? – Yun CHEN Jul 02 '17 at 10:10
  • “مرحبا Hello” Same example i have given in question title also. – Maheta Dhaval K Jul 02 '17 at 10:17
  • An solution given, base64 codes are included in ASCII, it should be accepted by all kind of database, like MySql, SQLite, Sql Server etc. Detail is in following answer. – Yun CHEN Jul 02 '17 at 10:36
  • And the character set utf8mb4 of MySql is also could solve your problem if you have access to modify your database. – Yun CHEN Jul 02 '17 at 10:42
  • +1 for below answer.. it works perfect! – Maheta Dhaval K Jul 02 '17 at 11:32
  • yeah, it works because it uses utf8 and not ascii (hasn't much to do with base64) – Michael Jul 02 '17 at 11:59
  • @MahetaDhavalK: if all you care for is that it works, you should accept Yun CHENs answer. if you want to understand why the original code didn't work, and why the new code does work, i.e. if you want to learn something for the future, that's a different story.. if the database (client encoding, server encoding, table column encoding) and everything else is set to UTF8, everything will work without a need for base64, and with everything working properly. – Michael Jul 02 '17 at 12:15

2 Answers2

1

Try convert it into base64 code, then insert base64 code to database:

//Original string to base64 string
NSString *emojiString = @"مرحبا  Hello";
NSData *emojiData = [emojiString dataUsingEncoding:NSUTF8StringEncoding];
NSString *base64String = [emojiData base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];

//Base64 string to original string
NSData *base64Data = [[NSData alloc] initWithBase64EncodedString:base64String options:NSDataBase64DecodingIgnoreUnknownCharacters];
NSString *originalString =[[NSString alloc] initWithData:base64Data encoding:NSUTF8StringEncoding];

NSLog(@"Result: %@",originalString);

Output: enter image description here

Yun CHEN
  • 6,450
  • 3
  • 30
  • 33
  • Yes. But database may not support the emoji code, like MySql without utf8mb4 cannot save the emoji code into table. So the Base64 code is a considerable option for this case. – Yun CHEN Jul 02 '17 at 11:41
  • for things like sorting or full-text search, clear text is preferrable to base64. it's better to fix the database encoding for that column with one `ALTER` statement than to use the base64 hammer – Michael Jul 02 '17 at 12:01
  • Yes, I agree with that the best solution is to alter database to support emoji code. – Yun CHEN Jul 02 '17 at 14:20
  • I have converted data base to support emoji with utf8mb4 support. But cannot compare device names. Sending [[UIDevice currentDevice] name] "Ipad" to server. When I get it back it is "Ipad\Uf643". Then I try to compare them again with "isequalTo" or localizedCompare method. But cannot do it. Serverside code is in Python. – Durgaprasad Feb 12 '18 at 07:11
1

You have to use an encoding that supports emoji and arabic characters. ASCII doesn't support that.

You should use NSUTF8StringEncoding everywhere, and you're fine.

Why are you using ASCII anyways? Why are you converting a string to an NSData and then back to NSString again? It doesn't make sense.

Michael
  • 6,451
  • 5
  • 31
  • 53