Emojis displaying as boxes - objective-c

I am making an app that posts content from a UITextField, to a PHP script, and store in a database to read later. I am having one heck of a time working with emojis.
Using json, I will get a response like this:
content = "\Uf44d";
id = 104;
time = 1350359055;
In this instance, I used an emoji. But when I do [dictionary objectForKey:#"content"], a box just appears. 
I'm thinking I need to convert to UTF-8. But I'm not completely sure. Please help!

U+F44D is a Unicode character in the "Private Use Area" U+E000..U+F8FF, so that is probably not the character you want to display.
On the other hand, U+1F44D is the "THUMBS UP SIGN", so it could be that your Web service does not create a correct JSON response for Unicode characters greater than 0xFFFF.
According to the JSON RFC, characters that are not part of the "Basic Multilingual Plane" can be escaped using a UTF-16 surrogate pair. For the U+1F44D character the JSON Unicode escape sequence would be "\ud83d\udc4d".
The following code shows that it works in general:
const char *s = "{ \"content\": \"\\ud83d\\udc4d\", \"id\": 104, \"time\": 1350359055 }";
NSData *jsonData = [NSData dataWithBytes:s length:strlen(s)];
NSError *error;
NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:&error];
self.label.text = [jsonDict objectForKey:#"content"];
This displays the "THUMBS UP SIGN" correctly in the label.
But you don't have to escape characters, the Web service could also just send the UTF-8 sequence.

I hope this will help you...I'm using this and e106 is my emoji code.
Only replcare with content line with below line:
content = [NSString stringWithFormat:#"%C", 0xe106];

Related

Decode String with Emoji characters such as \ud83d

I have following string, and i want to show it in a UILabel properly with emojies and the new lines. Also I want to draw it using drawInRect method. How do I get them converted/Encoded/Decoded properly?
This string will change on runtime so should show any unicode character/ emoji or special characters such as \n or &
I'm sorry that I do not know proper terms to use to ask this question. Which makes it difficult for me to find an answer online. My knowledge about this topic is very low.
\ud83d\ude02\ud83d\ude02\ud83d\ude02\ud83d\ude02\ud83d\ude02\ud83d\ude05\ud83d\ude06
\u0db8\u0da0\u0d82
\u0d91\u0d9a\u0dca\u0d9a\n#set_with_machan\nkunuharapa na \n#Follow
#lankan_machan\nhttps://www.instagram.com/lankan_machan
after encoding the text should look like this with emojis, unicode characters & new lines.
I was able to find a solution and Edited it a bit. This un escapes the unicode characters perfectly and shows them properly. Shows the new lines too. Thanks #DanZimm for help.
- (NSString*) unescapeUnicodeString2:(NSString*)string
{
NSString* esc1 = [string stringByReplacingOccurrencesOfString:#"\\u" withString:#"\\U"];
NSString* esc2 = [esc1 stringByReplacingOccurrencesOfString:#"\"" withString:#"\\\""];
NSString* quoted = [[#"\"" stringByAppendingString:esc2] stringByAppendingString:#"\""];
NSData* data = [quoted dataUsingEncoding:NSUTF8StringEncoding];
NSString* unesc = [NSPropertyListSerialization propertyListWithData:data options:NSPropertyListImmutable format:NULL error:NULL];
assert([unesc isKindOfClass:[NSString class]]);
return unesc;
}
Solution found here I had to edit it because one of the methods were deprecated.

Web Service JSON nsdictionary unicode string [duplicate]

NSData* jsonData is the http response contains JSON data.
NSString* jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
NSLog(#"jsonString: %#", jsonString);
I got the result:
{ "result": "\u8aaa" }
What is the proper way to encoding the data to the correct string, not unicode string like "\uxxxx"?
If you convert the JSON data
{ "result" : "\u8aaa" }
to a NSDictionary (e.g. using NSJSONSerialization) and print the dictionary
NSError *error;
NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:&error];
NSLog(#"%#", jsonDict);
then you will get the output
{
result = "\U8aaa";
}
The reason is that the description method of NSDictionary uses "\Unnnn" escape sequences
for all non-ASCII characters. But that is only for display in the console, the dictionary is correct!
If you print the value of the key
NSLog(#"%#", [jsonDict objectForKey:#"result"]);
then you will get the expected output
說
I don't quite understand what the problem is. AFNetworking has given you a valid JSON packet. If you want the above code to output the character instead of the \u… escape sequence, you should coax the server feeding you the result to change its output. But this shouldn't be necessary. What you most likely want to do next is run it through a JSON deserializer…
NSDictionary * data = [NSJSONSerialization JSONObjectWithData:jsonData …];
…and you should get the following dictionary back: #{#"result":#"說"}. Note that the result key holds a string with a single character, which I'm guessing is what you want.
BTW: In future, I suggest you copy-paste output into your question rather than transcribing it by hand. It'll avoid several needless rounds of corrections and confusion.

How to Use An Escape URL String With Formatting in Objective-C

I need to gather data from a website based on the user's input.
searchString is the user inputted value, such as "search this string".
NSString *withoutSpaces = [searchString stringByReplacingOccurrencesOfString:#" " withString:#"%20"];
Here, I need to replace spaces with %20
Next, I need to put the new string without spaces (replaced with %20) into another string.
NSString *unescapedSearchString = [NSString stringWithFormat:
#"website.com/query?=%22%#%22", withoutSpaces];
The site I need is not really "website.com", but that's just an example. I also need the %22 to remain at the beginning and end.
As you can see, I need the %# to format the new withoutSpaces user input into the website URL.
I did a search and found examples but I could not find any with formatting such as in my case using %#.
What's the best way to "escape" the characters and keep my formatted string? Currently, when I try to access data from the website, it comes back as null. However, when I try a string without the %# formatting and an actual value, I successfully retrieve the data from a website.
Any help is greatly appreciated.
You should do things this way:
NSString *searchString = ... // the raw search string with spaces and all
NSString *quoted = [NSString stringWithFormat:#"\"%#\"", searchString];
NSString *escaped = [quoted stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSString *urlString = [NSString stringWithFormat:#"website.com?query=%#&value=all", escaped];
BTW - the URL seems a little off. There should be a variable name before the = and after the ?.

Parsing arabic text with SBJson in Xcode

i have a JSON file that i'm trying to parse using SBJson.
the response string that i'm receiving is displaying arabic characters correctly when i do a
NSLog(#"%#",responseString);
but whenever i use the SBJson parser
NSDictionary *myDictionary = [responseString JSONValue];
and try
NSLog(#"%#", myDictionary);
my arabic characters are transformed to something weird:
\U0633\U0627\U0642\U064a\U0629 \U0627\U0644\U0645\U0633\U0643
please can anyone help
Don't worry. All is well.
\u0633
is equivalence to
س
try this in C++:
std::wstring tStr = L"\u0633";
tStr will equal to س
Just NSLog is not converting
use this :
NSStringEncoding Arabicencoding = CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingWindowsArabic);
NSString *yourstring = [[NSString alloc]initWithData:yourdata encoding:Arabicencoding];
"\U0633\U0627\U0642\U064a\U0629 \U0627\U0644\U0645\U0633\U0643" is actually just the encoding that Objective-C uses for none ascii characters. Try adding a subview/label on whatever you're testing with which will display the text. First try with "responseString" and then "myDictionary" it could display the same.

encode a nsstring with utf-8Type for post connection

Trying to postdata in post connection using setHttpPost
NSString* strRequest = [NSString stringWithFormat:#"username=%#&password==%#&client_assertion_type=%#&client_assertion=%#&spEntityID=%#",
strUsrName,strPasswrd,#"JWT",strAppTknContent,#"http://localhost:8080/opensso"];
NSData* dataRequest = [NSData dataWithBytes:[strRequest UTF8String] length:[strRequest length]];
[theRequest setHTTPMethod: #"POST"];
[theRequest setHTTPBody: dataRequest];
want to encode the value for username,password,client_assertion_type alone utf-8 type
NSString* username = [ encode utf-8 type #"fff"];
how do i encode only the value of each key elements and post it
You cannot encode a string in UTF-8 and then put it in a NSString instance. NSString uses Unicode characters anyway but not in an encoded form.
Why would you want to encode them separately anyway? What exactly are you trying to achieve?
And just to point out a mistake in your code. The following line does not work once you have characters that require more than 1 byte in UTF-8:
NSData* dataRequest = [NSData dataWithBytes:[strRequest UTF8String] length:[strRequest length]];
The length parameter is expected to be the number of bytes but you are passing the number of characters.
A correct and simpler alternative is:
NSData* dataRequest = [strRequest dataUsingEncoding: NSUTF8StringEncoding ];
Update:
As you obviously want to properly URL encoded your parameters, you're probably looking for code like this:
NSString* strRequest = [NSString stringWithFormat:#"username=%#&password==%#&client_assertion_type=%#&client_assertion=%#&spEntityID=%#",
[strUsrName stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding],
[strPasswrd stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding],
#"JWT",
[strAppTknContent stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding],
[#"http://localhost:8080/opensso" stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]
];
NSData* dataRequest = [strRequest dataUsingEncoding: NSUTF8StringEncoding ];
Update 2:
It's a very strange design to transmit data that way. When it was first used, it probably was a hack to get something working quickly. Unfortunately, it became a standard.
Basically, you have two levels of encoding. The URL encoding to serves to put several key/value pairs into a single string such that the string can later be split again into the key/value pairs. It needs to know that a UTF-8 encoding is used on the second level so it can escape the problematic characters. It's does UTF-8 the string yet.
The second level translates the string (a sequence of characters) into a byte stream using the UTF-8 encoding since HTTP is specified as a transmission of bytes, not characters.