【问题标题】:How to embed a Twitter feed and/or hashtag in IOS如何在 IOS 中嵌入 Twitter 提要和/或主题标签
【发布时间】:2012-12-23 22:10:02
【问题描述】:

我基本上只需要一个表格视图,它显示来自 tableviewcontroller 中 @username 或 #hashtag 的最近推文。没有发布推文或类似内容的要求。

目前我使用MGTwitterEngine,它很复杂,只获取与用户名相关的推文而不是hastags。

我找到了这个tutorial,但是大部分代码没有解释,也没有源代码。

也找到this,但似乎http://search.twitter.com/search?q=%23+ #hashtag 返回nil 数据

还看到了 question 为 ARC 编辑的代码并使用 http://search.twitter.com/search.json?q=%23epicwinning+OR+%40charliesheen 链接获取数据

#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

我打电话

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

返回结果数组:-[TwitterFeed returnedArray:]: unrecognized selector sent to instance

是否有任何简单的教程或代码示例可以同时获取用户名和主题标签推文?

有没有办法用MGTwitterEngine 获取主题标签?

【问题讨论】:

    标签: iphone ios twitter hashtag mgtwitterengine


    【解决方案1】:

    看看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) {
        // ...
    }];
    

    【讨论】:

    • 您好,如何为这个 twitter 搜索 API 实现分页?
    • 使用完整的方法 getSearchTweetsWithQuery: 包括 sinceID 和 maxID 参数。
    • 非常感谢。 +1
    【解决方案2】:

    您可能需要在有效的示例源代码下方实现自己的方法

    试试这个 git

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

    在您的终端 hg clone https://bitbucket.org/wave_1102/hdc2010-iphone 中输入并获取 git。

    HDC2010ViewController 中,将win 替换为您的标签

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

    【讨论】:

      【解决方案3】:

      您可以使用 Twitter Kit 在您的应用中显示完整的时间线 (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
        }
       
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-08-11
        • 1970-01-01
        • 1970-01-01
        • 2015-08-04
        • 2012-01-20
        • 1970-01-01
        • 2015-08-14
        • 1970-01-01
        相关资源
        最近更新 更多