Iphone: NSMutableURLRequest returns strange characters for an apostrophe in the style of MS Word

We pull content from our site using XML / NSMutableURLRequest, and sometimes it pulls through the “curly” style of the apostrophe and the quote, rather than “. NSMutableURLRequest seems to hate them and turn them into a strange string \ U00e2 \ U0080 \ U0099.

Is there something I can do to prevent this? I am using the GET method, so should I somehow tell him to use UTF-8? Or am I missing something?

UIApplication* app = [UIApplication sharedApplication]; app.networkActivityIndicatorVisible = YES; NSString *urlStr = [NSString stringWithFormat:@"%@",url]; NSURL *serviceUrl = [NSURL URLWithString:urlStr]; NSMutableURLRequest *serviceRequest = [NSMutableURLRequest requestWithURL:serviceUrl]; [serviceRequest setHTTPMethod:@"GET"]; NSURLResponse *serviceResponse; NSError *serviceError; app.networkActivityIndicatorVisible = NO; return [NSURLConnection sendSynchronousRequest:serviceRequest returningResponse:&serviceResponse error:&serviceError]; 
+4
source share
3 answers

NSURLConnection returns an NSData response. You can accept NSData answer and turn it into a string. Then take this line, return it to the NSData object, correctly encoding it with UTF-8, and feed it to NSXMLParser .

Example: (Assuming a response NSData response from your request)

 // long variable names for descriptive purposes NSString* xmlDataAsAString = [[[NSString alloc] initWithData:response] autorelease]; NSData* toFeedToXMLParser = [xmDataAsAString dataUsingEncoding:NSUTF8StringEncoding]; NSXMLParser* parser = [[[NSXMLParser alloc] initWithData:toFeedToXMLParser] autorelease]; // now utilize parser... 
+5
source

I would suggest replacing these characters using stringByReplacingCharactersInRange:withString: to replace unnecessary strings.

 NSString *currentTitle = @"Some string with a bunch of stuff in it."; //Create a new range for each character. NSRange rangeOfDash = [currentTitle rangeOfString:@"character to replace"]; NSString *location = (rangeOfDash.location != NSNotFound) ? [currentTitle substringToIndex:rangeOfDash.location] : nil; if(location){ currentTitle = [[currentTitle stringByReplacingOccurrencesOfString:location withString:@""] mutableCopy]; } 

I have done this in the past to deal with the same problem you are describing.

0
source

Try using stringByReplacingPercentEscapesUsingEncoding:

0
source

Source: https://habr.com/ru/post/1337038/


All Articles