链接无法打开Safari

时间:2011-06-27 16:19:46

标签: objective-c ios4 rss mobile-safari

我正在制作一个基本的RSS阅读器,它应该在Safari中打开链接,但是当我点击单元格时没有任何反应。这就是我所拥有的:

 - (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]];


}

这是控制台日志:

 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/…

我把链接放在哪里:

 - (void)viewDidAppear:(BOOL)animated
 {
     [super viewDidAppear:animated];
if ([stories count] == 0) {
    NSString *path = @"http://www.technobuffalo.com/feed/";
    [self parseXMLFileAtURL:path];
}

}

以下是解析代码:

 -(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];
}


}

2 个答案:

答案 0 :(得分:2)

Depak:上面的代码错了。您需要检查http://的前缀,而不是后缀。它应该是:

if ( ![storyLink hasPrefix:@"http://"] ) {
    NSString* oldLink = storyLink;
    storyLink = [@"http://" stringByAppendingString:oldLink];
}

Chris:当您从NSString创建NSURL时,将其存储在本地变量中并记录该变量。如果我是对的,NSURL创建失败,您当前正在将NULL传递给-openURL。你试过打开一个已知良好的NSURL:

[[UIApplication sharedApplication] openURL: [NSURL URLWithString: @"http://www.apple.com"]];

答案 1 :(得分:1)

您的日志中显示的链接

  

technobuffalo.com/companies/apple/...

而不是

  

<强> HTTP ://technobuffalo.com/companies/apple/...

请注意区别:“http://”前缀。

您的网址字符串缺少“http://”前缀。