Get unicode chars from webservice and display them in ios app - ios

I need some help:
i get from WebService only a part from the uunicode value, and after this I append the prefix \u to finish the value. The .ttf is good, i tested with some hardcoded values.
NSString *cuvant = [[self.catData objectAtIndex:indexPath.row]objectAtIndex:9]; //Get data
//apend prefix (double \ to escape the \u command)
cuvant = [NSString stringWithFormat:#"\\u%#",cuvant];
// cell.catChar.text = [NSString stringWithUTF8String:"\ue674"]; --->this works very well
cell.catChar.text = [NSString stringWithUTF8String:[cuvant UTF8String]]; //---> this doesn't work
i searched the documentation, and other sites but i didn't found nothing usefull, all the hints are with hardcoded data... i need tot take the codes dinamically
Thanks!

All you need is just to feed this unicoded string as data first. So make a C-String then
NSData *dataFromUnicodedString = [NSData dataWithBytes:yourCUnicodedString length:strlen(yourCUnicodedString)];
and afterwards the resulting string will be
NSString *unicodedString = [[NSString alloc] initWithData:dataFromUnicodedString encoding:NSUTF8StringEncoding];

Related

Converting NSData to NSString without knowing the encoding type

Disclaimer:
this previous question does not contain a valid answer.
this other instead does contain the technique I used to convert it but does not answer the question I have.
Question:
I am reading some NSData from a BLE characteristic which corresponds to a string, however I have no idea what encoding format it uses.
This is the view from the debugger on Xcode:
How can I convert it to a string? I'd like to extract this value:
"<951c7c1d e0fbdcc3 7b8b0b97 5c522036>"
I tried the following with with no meanigful conversion. However XCode seems to do the job. Why?
NSData * valueAsData = characteristic.value.mutableCopy;
NSString* newStr = [[NSString alloc] initWithData:characteristic.value encoding:NSUTF8StringEncoding];
NSString* newStr2 = [[NSString alloc] initWithData:characteristic.value encoding:NSASCIIStringEncoding];
Get the descriptors associated with the characteristic. One of them should be CBUUIDCharacteristicFormatString. This will indicate the presentation format, which will indicate the data type of the characteristic value.

unable to show emoji's properly in UIlabel

I am trying to save the emojis to server and on later time receiving them.
I used the Following Code before
NSData *data = [strEmo dataUsingEncoding:NSNonLossyASCIIStringEncoding];
NSString *goodValue = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSData *data1 = [strEmo dataUsingEncoding:NSUTF8StringEncoding];
NSString *goodValue = [[NSString alloc] initWithData:data encoding:NSNonLossyASCIIStringEncoding];
It generates the Hashcode for the Emoji. But decoding doesn't work. So I skipped the Idea of using this Code.
Then I used a third Party NSString+HTML.h Class. Which Sends and recieve emoji easily. But
Now the Problem is when there are so many emoji's the UIlabel on which I am showing the data, the emojis are distorted and If I saved 20 emojis it shows 12-13 only.
I have added the Pic for reference
where Yellow part is UIlabel with back colored Yellow
Buddy why are you changing the string in Data two times in a row just simply use this
NSString *uniText = [NSString stringWithUTF8String:[strEmo UTF8String]];
NSData *msgData = [uniText dataUsingEncoding:NSNonLossyASCIIStringEncoding];
NSString *readyString = [[NSString alloc] initWithData:msgData encoding:NSUTF8StringEncoding];
First convert your string into constant C characters then convert it into string using UTF8 encoding, now convert it into NSData as (7-bit verbose ASCII to represent all Unicode characters) using NSNonLossyASCIIStringEncoding encoding and then again string ready to send with unicode characters. Hope this should work as it is working fine in my code.

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 stringWithContentsOfURL with encoding?

I have a problem using "stringWithContentsOfURL" to get source page of a web(chinese)
Thinking using "NSUTF8StringEncoding" but didn't work(got null for result). My code looks like this
NSString *string = [NSString stringWithContentsOfURL:[NSURL URLWithString:#"http://www.morningstar.com.tw/download_pdf_2-1.aspx"] encoding:NSUTF8StringEncoding error:nil];
NSLog(#"html = %#",string);
replaced "NSUTF8StringEncoding" by "NSASCIIStringEncoding", did get a result but words are scrambled.
Thanks for the help!
You need a different encoding:
NSStringEncoding encoding = CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingDOSChineseTrad);
You can also use kCFStringEncodingDOSChineseSimplif for simplified chinese. Use encoding in place of NSASCIIStringEncoding or the other standard ones.
See the documentation for more encodings if these do not work properly.

Resources