【问题标题】:Displaying "description" from an RSS feed in an iOS app在 iOS 应用程序中显示来自 RSS 提要的“描述”
【发布时间】:2023-03-18 06:35:01
【问题描述】:

我正在尝试创建一个简单的 RSS 阅读器,我想从 rss 提要中获取“description”标签并将其显示在我的应用程序中,有什么帮助吗?

是否建议使用NSXMLParser 或任何其他解析器来简化我的工作?

我使用的源代码来自:iOS programming RSS reader tutorial

【问题讨论】:

    标签: ios nsxmlparser rss-reader


    【解决方案1】:

    该教程还剩下什么。一切正常。他们 刚刚离开使用描述项。对吧……?

    来自该教程:

    @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;
            } 
    
    }
    

    如果您在这里有任何问题,请告诉我:::

    【讨论】:

    • 在字符串中使用“%@”会显示 Feed 的所有内容,包括链接和描述。我只需要显示描述,还有其他方法吗?
    • 只有描述。当前的 o/p 类似于:“Feed 的 URL”,后跟描述。
    • 我尝试使用 RegEX 排除 URL,但也有其他 URL 应该显示给用户,所以这个想法没有成功。
    • didSelectRowAtIndexPath: 这个方法负责选择项数据。您只需要使用 Desc 数组正确显示。
    猜你喜欢
    • 2020-08-25
    • 2013-11-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-30
    • 2015-04-11
    • 1970-01-01
    • 2012-04-27
    相关资源
    最近更新 更多