ios stringWithContentsOfURL with encoding? - ios

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.

Related

Get unicode chars from webservice and display them in ios app

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];

How to use stringWithContentsOfFile:usedEncoding:error:

I've tried to load a file with unknown encoding. This is because I dont always have control over the file that I will load. I assumed that the method stringWithContentsOfFile:usedEncoding:error: will do this and will let me know the file encoding. Unfortunately following code doesn't provide the encoding I want - it always return 0.
NSStringEncoding *encoding = nil;
NSError *error = nil;
NSString *json = [NSString stringWithContentsOfFile:path
usedEncoding:encoding
error:&error];
NSLog(#"\n%lu\n%#",(unsigned long)encoding,error);
It returns content of file, so you may wonder why I need this encoding, well that string is JSON that I want to serialize it into NSDictionary and the dataUsingEncoding: method requires encoding. I tried to pass encoding variable but this throws an error. So I tried fail safe UTF8 encoding and then it worked.
NSData *jsonData = [json dataUsingEncoding:NSUTF8StringEncoding];
So I must using this incorrect as encoding equals to 0 instead of 4 (UTF8). Can someone help me with that?
Try that :
NSStringEncoding encoding;
NSError *error = nil;
NSString *json = [NSString stringWithContentsOfFile:path
usedEncoding:&encoding
error:&error];
NSLog(#"\n%lu\n%#",(unsigned long)encoding,error);
To be clearer, you can't receive the encoding value in your pointer, you need to give a plain NSStringEncoding address
Since you are not aware of the encoding of the file, I will suggest you to see this link.
Its basically String Programming Guide which will let you know in depth what to do.
Below is the snapshot for which you are looking into:
Hope this will help you. Happy coding :)

Unusual stringWithFormat: argument

I apologize if this is a noob question.
I have been following this tutorial about mapkit and I stumbled on this line of code
NSString *json = [NSString stringWithFormat:formatString,
centerLocation.latitude,
centerLocation.longitude,
0.5 * METERS_PER_MILE];
The reason this is unusual at least to me is that it is missing the nsstring that has the %# flags in it. The tutorial claims that we are adding the latitude and longitude information into the json.
But when I print out formatString and json, the output is identical.
I have never seen nsstrings used in this way before. Is there a hidden variable that is getting set?
Can someone explain to me how this nsstring object (named json) contains those 4 arguments?
Someplace else in the code, formatString must be defined something like this:
NSString *formatString = #"latitude=%f, longitude=%f, %f = half the number of meters in a mile";
Make sure your test looks like this:
NSLog(#"the format is %# and the json is %#", formatString, json);
They shouldn't look the same. The only way they would look the same is if format string doesn't refer to any format specifiers, like this:
NSString *formatString = #"I'm a silly format with no percent format specifiers";
Here's a good intro on the topic from Apple.
That formatString actually contains the %#'s. It might be like this:
NSString *formatString = #"lat: %f | lon: %f | half-meters-per-mile: %f";
NSString *json = [NSString stringWithFormat:formatString,
centerLocation.latitude,
centerLocation.longitude,
0.5 * METERS_PER_MILE];
(note that the substitutions (%f) might not be correct, I'm guessing)
As for how it contains those four arguments, everything after the first one are values that you want added into the string. The first one is a string that says where to put those values.
If you check the tutorial, following line of code is written above what you have posted-
NSString *jsonFile = [[NSBundle mainBundle] pathForResource:#"command" ofType:#"json"];
NSString *formatString = [NSString stringWithContentsOfFile:jsonFile encoding:NSUTF8StringEncoding error:nil];
From here the format string is created, this file will be available in the resources folder of your tutorial.

Chinese Character Encoding Issue 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

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