简体   繁体   中英

How to fix issue with unrecognized Special Character on Ipad/Iphone?

I am working on an app that gets a bunch of descriptions through xml and then after parsing puts them on the screen. I'm having a problem with some of the descriptions with apostrophes turning into question marks. What I mean is, they start out in the xml, on the output screen and in the database I get them from as an apostrophe, but then it shows up on the app as a Question mark. This doesn't always happen, but it does with the same descriptions every time. Heres an example:

This is what is in the xml/database

But it won't be easy. 
After a tour of the house, you'll be 

This is what shows up on the app:

But it won?t be easy. 
After a tour of the house, you?ll be

I'm pretty sure that the problem is that the ipad/iphone doesn't recognize the character that it is receiving...but I have no idea how I would go about fixing it. Here is a my Parser code: I believe the xml is being sent as UTF - 8 encoding.

[whereToGetXML appendFormat:xmlID];
NSURL *URL=[[NSURL alloc] initWithString:whereToGetXML];


NSData *dataFromServer = [[NSData alloc] initWithContentsOfURL:URL];  
NSLog(@"where to get xml is:%@", whereToGetXML);
// Do any additional setup after loading the view from its nib.
NSData *dataWithoutStringTag = [[NSData alloc]init];


NSXMLParser *firstParser = [[NSXMLParser alloc] initWithData: dataFromServer];
[firstParser setDelegate:self];
[firstParser parse];

//Convert the returned parsed string to data using dataUsingEncoding method. HAVE TO HAVE allowLossyConversion:YES or else it will crash on half of the states. 

dataWithoutStringTag =[tempParserString dataUsingEncoding: NSASCIIStringEncoding allowLossyConversion:YES];

NSString *htmlCode = [[NSString alloc] initWithData:dataWithoutStringTag encoding:NSASCIIStringEncoding];
NSMutableString *temp = [NSMutableString stringWithString:htmlCode];


NSData *finalData = [temp dataUsingEncoding:NSASCIIStringEncoding     allowLossyConversion:YES];

NSXMLParser* parser = [[NSXMLParser alloc] initWithData:finalData];
// NSLog(@"Data is:  %@", dataWithoutStringTag);
//NSXMLParser* parser = [[NSXMLParser alloc] initWithData: dataFromServer];

[parser setDelegate: self];
//[parser setShouldResolveExternalEntities:YES];
[parser parse];
[parser release];

It's presumably because the string isn't escaped.

Try adding a backslash before each quotation mark.

\'message\'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM