Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

EricVgaard

macrumors newbie
Original poster
Oct 24, 2012
2
0
Hi!

I'm developing an app for a school and I want to implement a textview that is supposed to display news and messages. I was thinking that it would be convenient to be able to update the text from a facebook/twitter account.

I've been searching the last two days for a guide/tutorial on how to simply fetch the last tweet/fb-post and display it in a textview, but without any significant result. I only want to parse the last post to a textview.

I'm developing the app using storyboards and for the iPad.

Thanks!
 
Yes perhaps that would be better. I have successfully loaded the curent tweets into an array, and now I would like to display them in the class ProgramTeknik. However, I cant figure ut how to display them. I'm not sure if I'm successfully passing the array to the ProgramTeknik class.



Tweet.h:

Code:
    #import <Foundation/Foundation.h>

    @interface Tweet : NSObject


    @property (strong, nonatomic)NSString *content;
    @property (strong, nonatomic)NSString *dateCreated;



    @end


Tweet.m:

Code:
    #import "Tweet.h"

    @implementation Tweet

    @synthesize content = _content;
    @synthesize dateCreated = _dateCreated;


    @end
XMLParser.h:

Code:
    #import "Foundation/Foundation.h"
    #import "Tweet.h"

    @interface XMLParser : NSObject <NSXMLParserDelegate>

    @property (strong, readonly) NSMutableArray *tweets;

    -(id) loadXMLByURL:(NSString *)urlString;

    @end


XMLParser.m:

Code:
    #import "Foundation/Foundation.h"
    #import "XMLParser.h"


    @implementation XMLParser
    @synthesize tweets = _tweets;

    NSMutableString *currentNodeContent;
    NSXMLParser *parser;
    Tweet *currentTweet;
    bool isStatus;

    bool isdone = true;



    -(id) loadXMLByURL:(NSString *)urlString
    {
        _tweets = [[NSMutableArray alloc] init];
        NSURL *url = [NSURL URLWithString:urlString];
        NSData *data = [[NSData alloc] initWithContentsOfURL:url];
        parser = [[NSXMLParser alloc] initWithData:data];
        parser.delegate = self;
        [parser parse];
        return self;
    }

    - (void) parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
    {
        currentNodeContent = (NSMutableString *) [string stringByTrimmingCharactersInSet:     [NSCharacterSet whitespaceAndNewlineCharacterSet]];
    }

    - (void) parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
    {
        if ([elementName isEqualToString:@"status"])
        {
        currentTweet = [Tweet alloc];
        isStatus = YES;
        }
        if ([elementName isEqualToString:@"user"])
        {
            isStatus = NO;
        }
    }

    - (void) parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
    {
        if (isStatus) {
            if ([elementName isEqualToString:@"text"])
            {
                currentTweet.content = currentNodeContent;
            }
            if ([elementName isEqualToString:@"created_at"])
            {
                currentTweet.dateCreated = currentNodeContent;
            }
        
        }
        if ([elementName isEqualToString:@"status"])
        {
            [self.tweets addObject:currentTweet];
            currentTweet = nil;
            currentNodeContent = nil;
        }
    }

    @end


ProgramTeknik.h:

Code:
    #import <UIKit/UIKit.h>
    #import "XMLParser.h"
    #import "Tweet.h"

    @interface ProgramTeknik : UIViewController
    @property (weak, nonatomic) IBOutlet UITextView *teknikNews;

    @end



ProgramTeknik.m:

Code:
    #import "ProgramTeknik.h"
    #import "ViewController.h"
    #import "XMLParser.h"
    #import "Tweet.h"


    @interface ProgramTeknik ()

    @end

    @implementation ProgramTeknik


    XMLParser *xmlParser; 


    - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
    {
        self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
        if (self) {
        // Custom initialization
        }
        return self;
    }

    - (void)viewDidLoad
    {
    
    
        [super viewDidLoad];
    	// Do any additional setup after loading the view.
    
        xmlParser = [[XMLParser alloc] loadXMLByURL:@"http://api.twitter.com/1/statuses/user_timeline/baristashopen.xml"];

    _teknikNews.text = @"test";

    }

    - (void)didReceiveMemoryWarning
    {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }

    @end
 
Last edited by a moderator:
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.