iPhone Rss Reader App For Twitter

On April 12, 2013, in iPhone iPad Object-C, by James Liu

In the post, iPhone Rss Reader App IOS Tutorial, I gave an example to show you how to create a RSS Reader App for iPhone. In that tutorial, I am using the data source from WordPress powered website. One of my friend, Mohamed, wonder if it can be modified to get RSS Feed from Twitter. I think it is necessary to create this iPhone App tutorial to show how to migrate the iPhone RSS Read app example to read the twitter rss feed.

In the iPhone RSS Reader App IOS example, there are two files to be modified.

  • RssTableViewController.m
  • RssHttpController.m

RssTableViewController is the main class of the whole app. It controls to loading the data from the server. In this example, it will call RssHttpController to load the rss feed from twitter address:

https://api.twitter.com/1/statuses/user_timeline.rss?screen_name=nba

Therefore, we need to change the code from:

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    rssData = [NSMutableArray array];
    
    RssHttpController *httpController = [[RssHttpController alloc] init];
    [httpController getRSSContent:[[NSNumber numberWithInt:1] stringValue] rssurl:@"http://jmsliu.com/feed?paged=" delegate:self];
}

To:

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    rssData = [NSMutableArray array];
    
    RssHttpController *httpController = [[RssHttpController alloc] init];
    [httpController getRSSContent:[[NSNumber numberWithInt:1] stringValue] rssurl:@"https://api.twitter.com/1/statuses/user_timeline.rss?screen_name=nba" delegate:self];
}

In the RssHttpController, we also need to make some corresponding changes to make HTTP request to send to the right web URL. Here is the code example:

-(NSURLConnection *)getRSSContent:(NSString *)pageID rssurl:(NSString *)url delegate:(id<NSURLConnectionDataDelegate>)delegate
{
    NSURL *urlRef = [NSURL URLWithString:url];
    
    NSURLRequest *rssRequest = [NSURLRequest requestWithURL:urlRef];
    NSURLConnection *connect = [[NSURLConnection alloc] initWithRequest:rssRequest delegate:delegate startImmediately:YES];
    return connect;
}

In the RssXMLParser.m, we will parse the coming xml from the twitter rss feed. The RSS Feed XML format is a standard format. Therefore, there is no different between Twitter RSS Feed xml and wordpress RSS Feed xml.

https://api.twitter.com/1/statuses/user_timeline.rss?screen_name=nba

After these changes, let’s see what our Twitter RSS app will look like:

iPhone Twitter RSS Feed App

iPhone Twitter RSS Feed App

We can see there are lots of meaningless message inside, such as “amp; 17”, “amp; 5a”. That is because in the Twitter Rss Feed xml, some html special characters like & are encoded as html special entities. So we need to convert these special entities back to normal characters. To do so, we need to make a change in our RssXMLParser.m. Here is the example code:

- (void) parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
    switch (aNode) {
        case title:
        {
            string = [string stringByTrimmingCharactersInSet:[NSCharacterSet nonBaseCharacterSet]];
            string = [string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
            if(string.length != 0)
            {
                lastTitle = [lastTitle stringByAppendingString:string];
            }
        }
        
        break;
        case postlink:
        {
            string = [string stringByTrimmingCharactersInSet:[NSCharacterSet nonBaseCharacterSet]];
            string = [string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
            if(string.length != 0 && articles != nil)
            {
                NSLog(@"%@", lastTitle);
                lastTitle = [lastTitle stringByReplacingOccurrencesOfString:@"&" withString:@"&"];
                [articles setObject:string forKey:lastTitle];
            }
        }
        break;
            
        default:
            break;
    }
}

Then, let’s see the final version of iPhone Twitter RSS Reader:

iPhone Twitter RSS Feed App

iPhone Twitter RSS Feed App

iPhone RSS Reader App Source Code at $2.99

Here is the full version of iPhone RSS Reader App source code. You will get:

  • Full Version of source code for Request Twitter Rss Feed
  • Help you make money from Google AdMob Ads
  • You can use it in any projects of yours



Tagged with:  

Leave a Reply

Free WordPress Themes

WordPress Themes