Question

Issue:

I'm parsing some info from an xml file (RSS feed) into a feed reader I'm developing. When trying opening news URL with Safari [[UIApplication sharedApplication] openURL:url] is not working.

I suspect the xml file itself is not well prepared. Actually, I'm struck on this and I just can't figure a way out.

Details:

The project works fine with all feeds it was tested with, only a particolar feed is not working. To be more specific, when parsed it gives this kind of url: %0Ahttp://google.com%0A

Here is the XML file I'm trying to read.

I'm using Ray Wenderlich's RSS reader tutorial as a workbench.

This is the code I'm using:

NSURL *url = [NSURL URLWithString:[entry.articleUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
if (![[UIApplication sharedApplication] openURL:url])
    NSLog(@"%@%@",@"Failed to open url:",[url description]);

Getting this: 2013-03-22 12:25:57.918 myApp[426:c07] Failed to open url:%0Ahttp://google.it/index.html%0A

Please note: every single feed I tested this project with (say 20+) didn't give any trouble. Any help will have my personal blessing for a healthy and prosperous life.

Was it helpful?

Solution

This has nothing to do with XML parsing, this is about correct URL.

[NSURL URLWithString:[entry.articleUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]; This is very, very wrong.

You should never escape the whole URL. You should escape only the query parameters. The %0A in the beginning and in the end is caused by encoding a line break.

In this case, they should be already escaped, so just trim the white spaces:

NSString* urlString = [entry articleUrl];
NSString* trimmedUrlString = [urlString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
NSURL *url = [NSURL URLWithString:trimmedUrlString];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top