EMoji Unicode for IOS - ios

I call api and get the string which include the android java unicode in NSDictionary :
\ud83d\ude25\ud83d\ude1e\U2764\ud83d\ude2d
When get the the value above and assign to string, I just will get the string as:
\ud83d\ude25\ud83d\ude1e❤\ud83d\ude2d
I totally can not get the value \U2764 but get the value ❤
So I can not replace the code \U2764 to IOS unicode.
How can I get it \U2764? I convert into UTF-8.
Thanks
Edit:
This is get the directory from api (dic):
{
message = "\ud83d\ude25\ud83d\ude1e\U2764\ud83d\ude2d";
mesgid = "213133";
Thumbnail = "";
}
Then i get the message, but the dic2 string is still get \ud83d\ude25\ud83d\ude1e❤\ud83d\ude2d:
const char *cString = [[dic6 objectForKey:#"Message"] UTF8String];
NSString *dic2 = [[NSString alloc] initWithUTF8String:cString];
if ([dic2 rangeOfString:#"\\U"].location==NSNotFound && [dic2 rangeOfString:#"\\u"].location==NSNotFound)
{
NSLog(#"Substring Not Found");
}
else
{
NSLog(#"Substring Found");
}
I totally can not get the unicode.
Why?

try this
NSString *uniString = [NSString stringWithUTF8String:#"\ud83d\ude25\ud83d\ude1e❤\ud83d\ude2d
"];

Hopefully this will resolve your problem
char cString[] = "\ud83d\ude25\ud83d\ude1e\U2764\ud83d\ude2d";
NSData *data = [NSData dataWithBytes:cString length:strlen(cString)];
NSString *string = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];

Related

want to have this string "مرحبا 😀 Hello" save it to database and display back to uilabel

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.
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:
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.

initWithBase64Encoding deprecated base64

I am receiving a warning in Xcode after upgrading to Xcode 7 in my project, I'm using CoacoSecurity which uses Base64 for encryption in the following line of code :
if (![NSData instancesRespondToSelector:#selector(initWithBase64EncodedString:options:)])
{
decoded = [[self alloc] initWithBase64Encoding:[string stringByReplacingOccurrencesOfString:#"[^A-Za-z0-9+/=]" withString:#"" options:NSRegularExpressionSearch range:NSMakeRange(0, [string length])]];
}
Its telling me that initWithBase64Encoding is deprecated, so how can i get over this warning and fix it.
I've converted it but I'm getting another warning:
decoded = [[self alloc] initWithBase64EncodedString:[string stringByReplacingOccurrencesOfString:#"[^A-Za-z0-9+/=]" withString:#""] options:NSRegularExpressionSearch];
The warning says:
Implicit conversion from enumeration type enum NSStringCompareOptions to different enumeration type NSDataBase64DecodingOptions (aka enum NSDataBase64DecodingOptions)
use this
NSData *decodedData = [[NSData alloc] initWithBase64EncodedString:base64String options:0];
instand of
NSData *data=[[NSData alloc]initWithBase64Encoding:(NSString *)dict];
Here's a solution that works with OSX 10.8 and up.
// assume sData is an NSString that's already been set
NSData *vData;
if ([vData respondsToSelector:#selector(base64EncodedStringWithOptions:)]) {
vData = [[NSData alloc] initWithBase64EncodedString:sData options:kNilOptions];
} else { // 10.8 or earlier
vData = [[NSData alloc] initWithBase64Encoding:sData];
}
NSString *sResult = [[NSString alloc]
initWithData:vData encoding:NSUTF8StringEncoding];
I threw in the sResult line only if you wanted to convert it to an NSString instead of an NSData.
So, that gives you a Base64 encoded string. If you want to now unencode it, you would do:
// assuming sData is an NSString that's already been set
NSString *sResult = #"";
NSData *vData = [sData dataUsingEncoding:NSUTF8StringEncoding];
if ([vData respondsToSelector:#selector(base64EncodedStringWithOptions:)]) {
sResult = [vData base64EncodedStringWithOptions:kNilOptions];
} else { // 10.8 or earlier
sResult = [vData base64Encoding];
}
Well, first i used initWithBase64Encoding as #Ske57 recommended and then to get over that warning i had to cast it to NSDataBase64DecodingOptions and it should work fine :
decoded = [[self alloc] initWithBase64EncodedString:[string stringByReplacingOccurrencesOfString:#"[^A-Za-z0-9+/=]" withString:#""] options:(NSDataBase64DecodingOptions)NSRegularExpressionSearch];

Convert emoji coming as uf-8 hex in text file back to character

I have imported whatsapp chat in email and when I opened it contains characters like this =F0=9F=94=A9 for emoji which is utf-8 string. Can someone please tell me how can I convert these characters back to emoji while showing in iOS app.
I am face same type of issue for emoji text and i found solution that i mention here.
-(NSString *)decodeSmiley:(NSString *)strData
{
if(strData != nil)
{
NSData *data = [strData dataUsingEncoding:NSUTF8StringEncoding];
NSString *decodedString = [[NSString alloc] initWithData:data encoding:NSNonLossyASCIIStringEncoding];
return decodedString;
}
return #"";
}
For encoding
-(NSString *)encodeSmiley:(NSString *)strData
{
if(strData != nil)
{
NSData *data = [strData dataUsingEncoding:NSNonLossyASCIIStringEncoding];
NSString *base64Encoded = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
return base64Encoded;
}
return #"";
}

Special Character to Original string - Objective C

I have tried the string(contain special character) to original String. The string received from service. I tried many codes. But i cant get the original String.
i tried the following code
// \U00e0\U00ae\U0089\U00e0\U00ae\U00b2\U00e0\U00ae\U0095\U00e0\U00ae\U00ae\U00e0\U00af\U008d
NSString *name2escaped = #"\\U00e0\\U00ae\\U0089\\U00e0\\U00ae\\U00b2\\U00e0\\U00ae\\U0095\\U00e0\\U00ae\\U00ae\\U00e0\\U00af\\U008d";
NSString *name2 = [NSString stringWithCString:[name2escaped cStringUsingEncoding:NSUTF8StringEncoding] encoding:NSNonLossyASCIIStringEncoding];
NSLog(#"name2 = %#", name2);
inputvalue.stringValue = name2;
NSLog (#"%#",name2);
Output prints: à®à®²à®à®®à¯
I worked in Mac OS X development 10.9 Mavericks
Service sometimes send chinese character also. Can anybody help me
As according to your requirement output should be Tamil or Chinese or English Language
Change NSNonLossyASCIIStringEncoding to NSUTF16StringEncoding :
NSString *name2escaped = #"\\U00e0\\U00ae\\U0089\\U00e0\\U00ae\\U00b2\\U00e0\\U00ae\\U0095\\U00e0\\U00ae\\U00ae\\U00e0\\U00af\\U008d";
NSString *name2 = [NSString stringWithCString:[name2escaped cStringUsingEncoding:NSUTF8StringEncoding] encoding:NSUTF16StringEncoding];
NSLog(#"name2 = %#", name2);
Output is : name2 = 屵〰攰屵〰慥屵〰㠹屵〰攰屵〰慥屵〰戲屵〰攰屵〰慥屵〰㤵屵〰攰屵〰慥屵〰慥屵〰攰屵〰慦屵〰㡤 –
I did it
My code is
- (NSString *) OrigninalString:(NSString *)getfilername
{
const char *c;
NSString *name2escaped = [NSString stringWithFormat:#"%#",getfilername];
NSString *string = [NSString
stringWithCString:[name2escaped cStringUsingEncoding:NSUTF8StringEncoding]
encoding:NSNonLossyASCIIStringEncoding];
c = [string cStringUsingEncoding:NSISOLatin1StringEncoding];
if (!c) {
c = [name2escaped cStringUsingEncoding:NSISOLatin1StringEncoding];
}
NSString *newString = [NSString stringWithCString:c encoding:NSUTF8StringEncoding];
c = nil;
string = nil;
return newString;
}

How to convert NData populated with hex values to NSString

I have a NSdata object that is populated with a bunch of information thats formated in hex.. I am trying to convert it into its proper string representation but am struggling to have any success.
One thing I have tried is to simply put it into a NSString and then NSLog it with a special character identifier thingy.. forgot the word (%02x), However to do this I am encoding it to NSUTF16.. which i dont want to do.. I mearly want to see exactly whats the data I am getting looks like as a NSString.
The reason I am doing this is because I am having some issues with my encoding later on in my code and im not sure if its because the data I am receiving is incorrect or me stuffing it up at some point when I am handling it.
Any help would be appreciated.
You can get a string representation of your NSData like so:
NSData *data = (your data)
NSString *string = [NSString stringWithCString:[data bytes] encoding:NSUTF8StringEncoding];
Does that answer your question?
Maybe I haven't understood, but something like this:
NSData *yourData;
NSLog(#"%#", [yourData description]);
doesn't fit your need?
Give this a try -
-(NSString*)hexToString:(NSData*)data{
NSString *hexString = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
if (([hexString length] % 2) != 0)
return nil;
NSMutableString *string = [NSMutableString string];
for (NSInteger i = 0; i < [hexString length]; i += 2) {
NSString *hex = [hexString substringWithRange:NSMakeRange(i, 2)];
NSInteger decimalValue = 0;
sscanf([hex UTF8String], "%x", &decimalValue);
[string appendFormat:#"%d", decimalValue];
}
return string;
}

Resources