ASCII vs UTF8

Posted by lorenb on Aug 13, 2009 in business, iphone |

We got some email from MMATracker users saying that the MMAJunkie RSS feed wasn’t working in the application any more.

The code that I was using:

NSURL *xmlURL = [NSURL URLWithString:URL];
NSError *err = nil;
NSString *xmlFeed = [[NSString alloc] initWithContentsOfURL:xmlURL encoding:NSUTF8StringEncoding error:&err];
if (err) {
	// handle error
}

After some trial/error, I determined the problem was the encoding. It was ASCII instead of UTF8. The feed is coming from Feedburner which is owned by Google. Seems odd they would stop using UTF8.

To fix this, I used the following code:

NSURL *xmlURL = [NSURL URLWithString:URL];

NSError *err = nil;
NSString *agentString = @"MMATracker";

NSMutableURLRequest *req = [[NSMutableURLRequest alloc] initWithURL:xmlURL];
[req setValue:agentString forHTTPHeaderField:@"User-Agent"];

NSData *data = [NSURLConnection sendSynchronousRequest:req returningResponse:nil error:&err];
NSString *xmlFeed = [[NSString alloc] initWithBytes:[data bytes] length:[data length] encoding:NSUTF8StringEncoding];
if (xmlFeed == nil) {
	xmlFeed = [[NSString alloc] initWithBytes:[data bytes] length:[data length] encoding:NSASCIIStringEncoding];
}

if (err) {
	// handle error
}

Leave a Reply

XHTML: You can use these tags:' <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Copyright © 2010 LB Technology Services Blog All rights reserved.