Chinese Character Encoding Issue ios - ios

I have one app in that I have support four Languages. In that When I am login with Chinese User name at that time it shows me Response like this ..
[{"0":"41","intid":"41","1":"\u8a00\u3046","varfirstname":"\u8a00\u3046","2":"\u8a00\u3046","varlastname":"\u8a00\u3046","3":"\u5730","varusername":"\u5730","4":"abc#gmail.com","varemailid":"abc#gmail.com","5":"qwert","varpassword":"qwert","6":"12345","varmobileno":"12345","7":"Enable","mobileMessage":"Enable","8":"","varphoneno":"","9":"Enable","enumstatus":"Enable","10":"2013-01-30","date_insert":"2013-01-30","11":"2013-01-30","date_edit":"2013-01-30","12":"1.38.28.36","varipaddress":"1.38.28.36"}]
I want to Show "varfirstname" to UITextfield Text . But I am not getting any Text when I print it in NSLog .
NSLog(#"Text is === %#",textfname,text);
How can I decode this Text? And show it on UITextfield or UILabel.

I just searched it and found one of the useful Answer from here.
It's natural that Chinese and Japanese characters don't work with ASCII string encoding. If you try to escape the string by Apple's methods, which you definitely should to avoid code duplication, store the result as a Unicode string. Use one of the following encodings:
NSUTF8StringEncoding
NSUTF16StringEncoding
NSShiftJISStringEncoding (not Unicode, Japanese-specific)
UPDATE
For Example you can encode Decode your chinese String like below:
NSString * test = #"汉字马拉松是";
NSString* encodedString =[test stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSLog(#"====%#",encodedString);
OUTPUT IS:
%E6%B1%89%E5%AD%97%E9%A9%AC%E6%8B%89%E6%9D%BE%E6%98%AF
Then Decode it like:
NSString* originalString =[encodedString stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSLog(#"====%#",originalString);
OUTPUT IS:
汉字马拉松是

NSString *abc = #"\u8a00\u3046";
NSLog(#" %# " , [NSString stringWithUTF8String:[abc UTF8String]]);
and if you use json :
NSString *html = #"\u8a00\u3046";
NSData *jsonData = [html dataUsingEncoding:NSUTF8StringEncoding];
NSLog(#" %# " , [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding]);
they all output "言う" I think it is Japanese

Related

Objective C using UItextview/NSstring place holders in long strings

please tell me why I get an: Interface type can not be Statically allocated,
error on the code below and what I can do to be able to use this placeholder in the Json string I am building below. Email is a UITextfield.
NSString *CCEmail = email.text;
NSString *jsonInputString =
#"{\"email\": \" %# \",\"password\": \"iamlearningtocode\"}",CCEmail;
To get the %# in your string replaced with the text in CCEmail you need to call
NSString *jsonInputString = [NSString stringWithFormat:#"{\"email\": \" %# \",\"password\": \"iamlearningtocode\"}", CCEmail];
However with this approach a CCEmail containing " would cause the string to become invalid JSON. I suggest you build your data in a proper NSDictionary and use NSJSONSerialization to convert to string.
You have to use stringWithFormat method of NSString class.
NSString *jsonInputString = [NSString stringWithFormat:#"{\"email\": \" %# \",\"password\": \"iamlearningtocode\"}",CCEmail];

convert unicode characters while data loading in uiwebview

{ disclaimertxt = "<b>sample tex\U221a\U00a9t n\U221a\U00a9ewerv\U221a\U00a9e iew adults.</b>
\n<br/>
\n<br/>
\nthis is sample \U221a\U2020 an d\U221a\U00a9convertion of the language\U221a\U00a9reduction\U201a\U00c4\U00b6
\n ";
}
the above one is dictionary which contains above value with key disclaimertext
but in output the unicode characters is replaced with ?(C)" and "?A
and my code is :
NSData *messagedata = [dictionary[disclaimertxt] dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *string = [[NSString alloc]initWithData:messagedata encoding:NSUTF8StringEncoding];
so i want to display those unicode characters with respected text/value while loading in uiwebview.. plz give ur suggestions. in ios7 its working fine with desired output what i need. but when i run ios6 aim getting above outout.so plz find solution.
Follow these steps:
manually load the HTML from the page that doesn't include the meta tag, into a string.
Using string manipulation, insert your appropriate encoding meta tag into the manually loaded HTML
set the HTML in your web view to the modified string using loadHTMLString:
Use this to convert Unicode charaters to NSString:
char cString[] = "This isn\u2019t Test String";
NSData *data = [NSData dataWithBytes:cString length:strlen(cString)];
NSString *string = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
Now use this string in your Webview.

How do I properly encode Unicode characters in my NSString?

Problem Statement
I create a number of strings, concatenate them together into CSV format, and then email the string as an attachment.
When these strings contain only ASCII characters, the CSV file is built and emailed properly. When I include non-ASCII characters, the result string becomes malformed and the CSV file is not created properly. (The email view shows an attachment, but it is not sent.)
For instance, this works:
uncle bill's house of pancakes
But this doesn't (note the curly apostrophe):
uncle bill’s house of pancakes
Question
How do I create and encode the final string properly so that all valid unicode characters are included and the result string is formed properly?
Notes
The strings are created via a UITextField and then are written to and then read from a Core Data store.
This suggests that the problem lies in the initial creation and encoding of the string: NSString unicode encoding problem
I don't want to have to do this: remove non ASCII characters from NSString in objective-c
The strings are written and read to/from the data store fine. The strings display properly (individually) in the app's table views. The problem only manifests when concatenating the strings together for the email attachment.
String Processing Code
I concatenate my strings together like this:
[reportString appendFormat:#"%#,", category];
[reportString appendFormat:#"%#,", client];
[reportString appendFormat:#"%#\n", detail];
etc.
Replacing curly quotes with boring quotes makes it work, but I don't want to do it this way:
- (NSMutableString *)cleanString:(NSString *)activity {
NSString *temp1 = [activity stringByReplacingOccurrencesOfString:#"’" withString:#"'"];
NSString *temp2 = [temp1 stringByReplacingOccurrencesOfString:#"‘" withString:#"'"];
NSString *temp3 = [temp2 stringByReplacingOccurrencesOfString:#"”" withString:#"\""];
NSString *temp4 = [temp3 stringByReplacingOccurrencesOfString:#"“" withString:#"\""];
return [NSMutableString temp4];
}
Edit:
The email is sent:
NSString *attachment = [self formatReportCSV];
[picker addAttachmentData:[attachment dataUsingEncoding:NSStringEncodingConversionAllowLossy] mimeType:nil fileName:#"MyCSVFile.csv"];
where formatReportCSV is the function that concatenates and returns the csv string.
You seem to be running across a string encoding issue. Without seeing what your Core Data model looks like, I'd assume the issue boils down to the issue reproduced by the code below.
NSString *string1 = #"Uncle bill’s house of pancakes.";
NSString *string2 = #" Appended with some garbage's stuff.";
NSMutableString *mutableString = [NSMutableString stringWithString: string1];
[mutableString appendString: string2];
NSLog(#"We got: %#", mutableString);
// We got: Uncle bill’s house of pancakes. Appended with some garbage's stuff.
NSData *storedVersion = [mutableString dataUsingEncoding: NSStringEncodingConversionAllowLossy];
NSString *restoredString = [[NSString alloc] initWithData: storedVersion encoding: NSStringEncodingConversionAllowLossy];
NSLog(#"Restored string with NSStringEncodingConversionAllowLossy: %#", restoredString);
// Restored string with NSStringEncodingConversionAllowLossy:
storedVersion = [mutableString dataUsingEncoding: NSUTF8StringEncoding];
restoredString = [[NSString alloc] initWithData: storedVersion encoding: NSUTF8StringEncoding];
NSLog(#"Restored string with UTF8: %#", restoredString);
// Restored string with UTF8: Uncle bill’s house of pancakes. Appended with some garbage's stuff.
Note how the first string (encoded using ASCII) couldn't handle the presence of the non-ASCII character (it can if you use dataUsingEncoding:allowsLossyConversion: with the second parameter being YES).
This code should fix the issue:
NSString *attachment = [self formatReportCSV];
[picker addAttachmentData:[attachment dataUsingEncoding: NSUTF8StringEncoding] mimeType:nil fileName:#"MyCSVFile.csv"];
Note: you may need to use one of the UTF16 string encodings if you need to handle non-UTF8 languages like Japanese.

iOS : decode utf8 string

I'm receiving a json data from server with some strings inside. I use SBJson https://github.com/stig/json-framework to get them.
However when I output some strings at UILabel they look like this: \u0418\u043b\u044c\u044f\u0411\u043b\u043e\u0445 (that's Cyrillic symbols)
And it's all right with latin characters
How can I decode it into normal symbols?
Some code about getting data:
NSData * data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
NSString *stringData = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSDictionary *object = [parser objectWithString:stringData error:nil];
NSString *comments = [NSString stringWithFormat:#"%#",[object valueForKey:#"comments"]];
String comments has a very special format, so I'm doing some operation like stringByTrimmingCharactersInSet ,
stringByReplacingOccurrencesOfString ,
NSArray* json_fields = [comments_modified componentsSeparatedByString: #";"];
to get a final data.
This is an example of received data after some trimming/replacing (it's NSString* comments):
"already_wow"=0;"date_created"="2012/03/1411:11:18";id=41598;name="\U0418\U043b\U044c\U044f\U0411\U043b\U043e\U0445";text="\U0438\U043d\U0442\U0435\U0440\U0435\U0441\U043d\U043e";"user_id"=1107;"user_image"="user_image/a6/6f/96/21/20111220234109510840_1107.jpg";"user_is_deleted"=0;username=IlyaBlokh;"wow_count"=0;
You see that fields text and name are encoded
If I display them on the view (at UILabel for example), they still look the same
maybe the string returned is just the unicode string representation (ascii string), that's means not returned the content encoded with utf8, to try this with NSASCIIStringEncoding to get stringData

Encrypted twitter feed

I'm developing an iOS application , that will take a twits from twitter,
I'm using the following API
https://api.twitter.com/1/statuses/user_timeline.json?include_entities=true&include_rts=true&count=2&screen_name=TareqAlSuwaidan
The problem are feed in Arabic Language ,
i.e the text feed appears like this
\u0623\u0646\u0643 \u0648\u0627\u0647\u0645
How can i get the real text (or how to encode this to get real text) ?
This is not encrypted, it is unicode. The codes 0600 - 06ff is Arabic. NSString handles unicode.
Here is an example:
NSString *string = #"\u0623\u0646\u0643 \u0648\u0627\u0647\u0645";
NSLog(#"string: '%#'", string);
NSLog output:
string: 'أنك واهم'
The only question is exactly what problem are you seeing, are you getting the Arabic text? Are you using NSJSONSerialization to deserialize the JSON? If so there should be no problem.
Here is an example with the question URL (don't use synchronous requests in production code):
NSURL *url = [NSURL URLWithString:#"https://api.twitter.com/1/statuses/user_timeline.json?include_entities=true&include_rts=true&count=2&screen_name=TareqAlSuwaidan"];
NSData *data = [NSData dataWithContentsOfURL:url];
NSError *error;
NSArray *jsonObject = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error];
NSDictionary *object1 = [jsonObject objectAtIndex:0];
NSString *text = [object1 objectForKey:#"text"];
NSLog(#"text: '%#'", text);
NSLog output:
text: '#Naser_Albdya أيدت الثورة السورية منذ بدايتها وارجع لليوتوب واكتب( سوريا السويدان )
Those are Unicode literals. I think all that's needed is to use NSString's stringWithUTF8String: method on the string you have. That should use NSString's native Unicode handling to convert the literals to the actual characters. Example:
NSString *directFromTwitter = [twitterInterface getTweet];
// directFromTwitter contains "\u0623\u0646\u0643 \u0648\u0627\u0647\u0645"
NSString *encodedString = [NSString stringWithUTF8String:[directFromTwitter UTF8String]];
// encodedString contains "أنك واهم", or something like it
The method call inside the conversion call ([directFromTwitter UTF8String]) is to get access to the raw bytes of the string, that are used by stringWithUTF8String. I'm not exactly sure on what those code points come out to, I just relied on Python to do the conversion.

Resources