首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >TWRequest是否适用于推特流api?

TWRequest是否适用于推特流api?
EN

Stack Overflow用户
提问于 2011-12-20 03:55:19
回答 2查看 1.5K关注 0票数 4

我正在尝试制作一个基本的iphone应用程序,可以显示附近的推文。我使用TWRequest对象通过twitter search api来完成此任务。不幸的是,我实际上想要在地图上使用它们的GPS坐标来标记tweet,而搜索api似乎没有返回tweet的实际位置比城市名称更准确。

因此,我认为我需要切换到流api。我想知道在这种情况下是否可以继续使用TWRequest对象,或者是否需要实际切换到使用NSURLConnection?提前感谢!

Avtar

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-02-14 10:12:16

可以,您可以使用TWRequest对象。使用来自Twitter API文档的适当URL和参数创建TWRequest对象,并将TWRequest.account属性设置为Twitter帐户的ACAccount对象。

然后,您可以使用TWRequest的signedURLRequest方法来获取一个NSURLRequest,该NSURLConnection可用于使用connectionWithRequest:delegate:创建异步委托。

完成后,只要从Twitter接收到数据,就会调用委托的connection:didReceiveData:方法。请注意,接收到的每个NSData对象可能包含多个JSON对象。在使用NSJSONSerialization从JSON转换每个文件之前,您需要将它们分开(用“\r\n”分隔)。

票数 10
EN

Stack Overflow用户

发布于 2012-09-19 05:28:34

我花了一些时间来启动和运行它,所以我想我应该把我的代码发布给其他人。在我的例子中,我试图让tweet靠近某个位置,所以您将看到我使用了一个locations参数和一个作用域中的location结构。您可以向params字典添加任何您想要的参数。

还要注意,这只是最基本的功能,您需要做一些事情,比如通知用户找不到帐户,并允许用户在存在多个帐户时选择他们想要使用的twitter帐户。

祝你流媒体快乐!

代码语言:javascript
复制
//First, we need to obtain the account instance for the user's Twitter account
ACAccountStore *store = [[ACAccountStore alloc] init];
ACAccountType *twitterAccountType = [store accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];

//  Request permission from the user to access the available Twitter accounts
[store requestAccessToAccountsWithType:twitterAccountType
                 withCompletionHandler:^(BOOL granted, NSError *error) {
                     if (!granted) {
                         // The user rejected your request
                         NSLog(@"User rejected access to the account.");
                     }
                     else {
                         // Grab the available accounts
                         NSArray *twitterAccounts = [store accountsWithAccountType:twitterAccountType];
                         if ([twitterAccounts count] > 0) {
                             // Use the first account for simplicity
                             ACAccount *account = [twitterAccounts objectAtIndex:0];
                             NSMutableDictionary *params = [[NSMutableDictionary alloc] init];
                             [params setObject:@"1" forKey:@"include_entities"];
                             [params setObject:location forKey:@"locations"];
                             [params setObject:@"true" forKey:@"stall_warnings"];
                             //set any other criteria to track
                             //params setObject:@"words, to, track" forKey@"track"];

                             //  The endpoint that we wish to call
                             NSURL *url = [NSURL URLWithString:@"https://stream.twitter.com/1.1/statuses/filter.json"];

                             //  Build the request with our parameter
                             TWRequest *request = [[TWRequest alloc] initWithURL:url
                                                                      parameters:params
                                                                   requestMethod:TWRequestMethodPOST];

                             // Attach the account object to this request
                             [request setAccount:account];
                             NSURLRequest *signedReq = request.signedURLRequest;

                             // make the connection, ensuring that it is made on the main runloop
                             self.twitterConnection = [[NSURLConnection alloc] initWithRequest:signedReq delegate:self startImmediately: NO];
                             [self.twitterConnection scheduleInRunLoop:[NSRunLoop mainRunLoop]
                                                   forMode:NSDefaultRunLoopMode];
                             [self.twitterConnection start];

                         } // if ([twitterAccounts count] > 0)
                     } // if (granted) 
                 }];
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8566742

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档