我正在尝试创建一个简单的RSS阅读器,我想从rss提要中获取“description
”标签并将其显示在我的应用程序中,任何帮助?
建议使用NSXMLParser
或任何其他解析器使我的工作变得轻松吗?
我使用的源代码来自:iOS programming RSS reader tutorial
答案 0 :(得分:1)
该教程还剩下什么。一切都很好。他们 刚离开使用Description项。对...?
以下是教程:
@interface ViewController (){
NSXMLParser *parser;
NSMutableArray *feeds;
NSMutableDictionary *item;
NSMutableString *title;
NSMutableString *link;
NSString *element;
NSMutableString *desc; // Description .
}
只需粘贴此代码即可。像魅力一样工作:
#pragma mark - parsing of RssFeed Values
-(void) parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{
element = elementName;
if ([element isEqualToString:@"item"]) {
item = [[NSMutableDictionary alloc]init];
title = [[NSMutableString alloc]init];
link = [[NSMutableString alloc] init];
desc = [[NSMutableString alloc] init];
}
}
-(void) parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{
if ([elementName isEqualToString:@"item"]) {
[item setObject:title forKey:@"title"];
[item setObject:link forKey:@"link"];
[item setObject:desc forKey:@"description"];
[feeds addObject:[item copy]];
}
}
-(void) parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{
if ([element isEqualToString:@"title"]) {
[title appendString:string];
}else if ([element isEqualToString:@"link"]){
[link appendString:string];
}else if ([element isEqualToString:@"description"]){
[desc appendString:string];
}
}
-(void) parserDidEndDocument:(NSXMLParser *)parser{
[self.tableView reloadData];
}
在任何地方使用此参数 Desc 来获取描述 RSSFeed项目。
这是完成它:
-(void) tableView: (UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
NSString *string = [feeds[indexPath.row] objectForKey: @"description"];
stringUrl = [feeds[indexPath.row] objectForKey:@"link"];
NSLog(@"Description %@", string);
actionSheet = [[UIActionSheet alloc] initWithTitle:string delegate:self cancelButtonTitle:Nil destructiveButtonTitle:@"OK" otherButtonTitles:@"GoTo URL", nil];
actionSheet.actionSheetStyle = UIActionSheetStyleBlackTranslucent;
[actionSheet showInView:self.view];
}
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex;
{
self.actionSheet.delegate = self;
switch (buttonIndex) {
case 0:
NSLog(@"ButtonIndex at 0");
break;
case 1:
NSLog(@"ButtonIndex at 1");
//Add your Segue functionalities over here to open link on the browser .
}
break;
}
}
如果您有任何问题,请告诉我:::