Reputation: 12191
I'm making a basic RSS reader and it should be opening the link in Safari, but nothing happens when I click on the cell. Here is what I have:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//Navigation Logic:
int storyIndex = [indexPath indexAtPosition: [indexPath length] -1];
NSString *storyLink = [[stories objectAtIndex: storyIndex] objectForKey:@"link"];
//cleaning up the link...
storyLink = [storyLink stringByReplacingOccurrencesOfString:@" " withString:@""];
storyLink = [storyLink stringByReplacingOccurrencesOfString:@"/n" withString:@""];
storyLink = [storyLink stringByReplacingOccurrencesOfString:@" " withString:@""];
NSLog(@"link: %@", storyLink);
//open in Safari
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:storyLink]];
}
Here is the console log:
2011-06-27 20:03:51.817 ParadiseBeats[26927:207] all done
2011-06-27 20:03:51.818 ParadiseBeats[26927:207] stories array had 20 items
2011-06-27 20:03:53.758 ParadiseBeats[26927:207] link: technobuffalo.com/companies/apple/…
And where I put in the link:
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
if ([stories count] == 0) {
NSString *path = @"http://www.technobuffalo.com/feed/";
[self parseXMLFileAtURL:path];
}
}
Here is the parsing code:
-(void)parser:(NSXMLParser *) parser didStartElement:(NSString *) elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *__strong)qName attributes:(NSDictionary *__strong)attributeDict {
currentElement = [elementName copy];
if([elementName isEqualToString:@"item"]){
//clear out story item caches...
item = [[NSMutableDictionary alloc] init];
currentTitle = [[NSMutableString alloc] init];
currentDate = [[NSMutableString alloc] init];
currentSummary = [[NSMutableString alloc] init];
currentLink = [[NSMutableString alloc] init];
}
}
-(void)parser:(NSXMLParser *)parser didEndElement:(NSString *__strong)elementName namespaceURI:(NSString *__strong)namespaceURI qualifiedName:(NSString *__strong)qName {
if ([elementName isEqualToString:@"item"]) {
[item setObject:currentTitle forKey:@"title"];
[item setObject:currentLink forKey:@"link"];
[item setObject:currentSummary forKey:@"summary"];
[item setObject:currentDate forKey:@"date"];
[stories addObject:[item copy]];
NSLog(@"adding story: %@", currentTitle);
}
}
-(void)parser:(NSXMLParser *) parser foundCharacters:(NSString *__strong)string{
//save the characters for the current item
if ([currentElement isEqualToString:@"title"]) {
[currentTitle appendString:string];
}
else if ([currentElement isEqualToString:@"link"]) {
[currentLink appendString:string];
}
else if ([currentElement isEqualToString:@"pubDate"]) {
[currentDate appendString:string];
}
else if ([currentElement isEqualToString:@"description"]) {
[currentSummary appendString:string];
}
}
Upvotes: 1
Views: 562
Reputation: 868
Your link, as showing in your log is
technobuffalo.com/companies/apple/…
instead of
http://technobuffalo.com/companies/apple/…
Please notice the difference: the "http://" prefix.
Your URL strings are missing the "http://" prefix.
Upvotes: 1
Reputation: 1485
Depak: your code above is wrong. You need to check for a prefix of http://, not a suffix. It should be:
if ( ![storyLink hasPrefix:@"http://"] ) {
NSString* oldLink = storyLink;
storyLink = [@"http://" stringByAppendingString:oldLink];
}
Chris: when you create the NSURL from the NSString, store that in a local variable and log that. If I'm right, the NSURL creation failed and you're currently passing NULL into -openURL. Have you tried opening a known-good NSURL:
[[UIApplication sharedApplication] openURL: [NSURL URLWithString: @"http://www.apple.com"]];
Upvotes: 2