Search code examples
iosnsxmlparserrss-reader

Displaying "description" from an RSS feed in an iOS app


I am trying to create an Simple RSS reader , i would like to fetch the "description" tag from the rss feed and display it in my app , any help ?

Is it advisable to use NSXMLParser or any other parser to make my work easy ?

The Source code i am using is from : iOS programming RSS reader tutorial


Solution

  • What else left with that tutorial . Everything works fine na. They just left to use the Description item . right ...?

    Here goes from that tutorial :

    @interface ViewController (){
    
     NSXMLParser *parser;
        NSMutableArray *feeds;
        NSMutableDictionary *item;
        NSMutableString *title;
        NSMutableString *link;
        NSString *element;
    
     NSMutableString *desc; // Description .
    }
    

    Just paste this code . Works like charm :

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

    Use this parameter Desc anywhere to get the Description of the RSSFeed item.

    Here is completion of it :

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

    If you have any problem here , Plz let me know :::