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 ?

有帮助吗?

解决方案 2

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" ] ]; 

其他提示

Have a look at STTwitter.

STTwitterAPI *twitter =
    [STTwitterAPI twitterAPIApplicationOnlyWithConsumerKey:@""
                                                   consumerSecret:@""];

[twitter verifyCredentialsWithSuccessBlock:^(NSString *bearerToken) {

    [twitter getSearchTweetsWithQuery:@"Snowden"
                         successBlock:^(NSDictionary *searchMetadata, NSArray *statuses) {
        // use the statuses here
    } errorBlock:^(NSError *error) {
        // ...
    }];

} errorBlock:^(NSError *error) {
    // ...
}];

You can use Twitter Kit to display a full timeline in your app (https://docs.fabric.io/ios/twitter/show-timelines.html).

class SearchTimelineViewController: TWTRTimelineViewController {

  convenience init() {
    let client = TWTRAPIClient()
    let dataSource = TWTRSearchTimelineDataSource(searchQuery: "#objc", APIClient: client)
     self.init(dataSource: dataSource)

     // Show Tweet actions
     self.showTweetActions = true
  }
 
}

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top