Search code examples
iphoneiostwitterhashtagmgtwitterengine

How to embed a Twitter feed and/or hashtag in IOS


I need basically just a table view that showed recent tweets from either a @username or a #hashtag in a tableviewcontroller. No requirements to post tweets or anything like that.

Currently I use MGTwitterEngine it is complicated and only fetches username related tweets not hastags.

I found this tutorial but most of the codes is not explained and there is no source code.

Also find this but it seems http://search.twitter.com/search?q=%23+ #hashtag returns nil data

Also saw this question edited code for ARC and used http://search.twitter.com/search.json?q=%23epicwinning+OR+%40charliesheen link to fetch data

#import <Foundation/Foundation.h>


@protocol latestTweetsDelegate
- (void)returnedArray:(NSArray*)tArray;
@end


@interface latestTweets : NSObject
{
    NSMutableData *responseData;
    NSMutableArray *resultsArray;
    id<latestTweetsDelegate> delegate;
}


@property (nonatomic, strong) NSMutableArray *resultsArray;
@property (strong,nonatomic) id<latestTweetsDelegate> delegate;

- (id)initWithTwitterURL:(NSString *)twitterURL;

@end
#import "latestTweets.h"
#import "SBJson.h"

@implementation latestTweets
@synthesize resultsArray, delegate;

- (id)initWithTwitterURL:(NSString *)twitterURL
{
    self = [super init];
    if (self) {
        responseData = [NSMutableData data];
        NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:twitterURL]];
        [[NSURLConnection alloc] initWithRequest:request delegate:self];
    }
    return self;
}

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {

}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
    [responseData appendData:data];
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {

    NSLog(@"Connection failed: %@", [error description]);
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {

    NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];

    NSArray *newData = [responseString JSONValue];


    [self.delegate returnedArray:newData];
}

@end

I call

latestTweets *lt = [[latestTweets alloc] initWithTwitterURL:@"http://search.twitter.com/search.json?q=%23epicwinning+OR+%40charliesheen"];
lt.delegate = self;

Returns result array : -[TwitterFeed returnedArray:]: unrecognized selector sent to instance

Is there any simple tutorial or code sample to fetch both username and hashtag tweets at the same time?

or

Is there a way to fetch also hashtags with MGTwitterEngine ?


Solution

  • You may need to implement your own method below an example source code that works

    Try this git

    https://bitbucket.org/wave_1102/hdc2010-iphone/src

    In your terminal hg clone https://bitbucket.org/wave_1102/hdc2010-iphone type and fetch git.

    In HDC2010ViewController replace win with your hashtag

    // search twitter for the HDC10 hashtag and add the tweets to our array
        [ tweetArray addObjectsFromArray:[ tweetFactory recentTweetsForHashTag:@"win" ] ];