I’m parsing an RSS feed and storing each item in a dictionary and each one of these in an array. This all works fine, but when I try and open a link from the feed nothing happens, the URL appears to be correct as I have NSLogged it when pressing the button, I have remove whitespace, newlines and tabs from the URL, but am unsure weather I have done this correctly. Any Ideas?
I know the initial parsing and storage is fine as I am using other elements in other parts of the program so I will just show the code directly involved with the problem.
Formatting the URL
libraryRSSString = [[stories objectAtIndex: 0] objectForKey: @"link"];
[libraryRSSString stringByReplacingOccurrencesOfString:@" " withString:@""];
[libraryRSSString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
and the action for opening the link, I will mention now that the action works when hardcoding the link, as in @”http://www.google.com”;
-(IBAction)LibraryRSSAction{
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:libraryRSSString]];
NSLog(@"button pressed");
NSLog(@"the link is %@",libraryRSSString);
}
You are encoding the url in a wrong way, try this.
EDIT (To include the code on the link above):
Create a category named “NSString+URLEncoding”, files NSString+URLEncoding.h and NSString+URLEncoding.m.
NSString+URLEncoding.h:
NSString+URLEncoding.m:
Now import the file NSString+URLEncoding.h on the file you want to use the method and just do
libraryRSSString = [[[stories objectAtIndex: 0] objectForKey: @"link"] urlEncodeUsingEncoding:NSUTF8StringEncoding];