TWRequest适用于Twitter流API吗?

4
我正在尝试制作一个基本的iPhone应用程序,用于显示附近的推文。我使用Twitter搜索API中的TWRequest对象来实现这一点。不幸的是,我实际上想要标记推文在地图上的GPS坐标,而搜索API似乎没有比城市名称更好的精度返回推文的实际位置。
因此,我认为我需要切换到流API。我想知道是否有可能在这种情况下继续使用TWRequest对象,或者我需要切换到使用NSURLConnection?谢谢!
2个回答

10

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

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

完成此操作后,每当从Twitter接收到数据时,代理的connection:didReceiveData:方法将被调用。 请注意,接收到的每个NSData对象可能包含多个JSON对象。 在使用NSJSONSerialization将它们转换为JSON之前,您需要将它们拆分(由“\r\n”分隔)。


3
花了一点时间才让它运行起来,所以我想把我的代码发布出来给其他人。在我的情况下,我试图获取靠近某个位置的推文,因此您将看到我使用了一个“locations”参数和一个我在范围内的位置结构。您可以向参数字典中添加任何参数。请注意,这只是最基本的代码,您需要做一些事情,比如通知用户未找到帐户,并允许用户选择他们想要使用的Twitter帐户(如果存在多个帐户)。祝您流媒体愉快!
//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) 
                 }];

在发布复制和粘贴的样板/逐字回答多个问题时要小心,社区往往会将其标记为“垃圾邮件”。如果您这样做,通常意味着这些问题是重复的,请将它们标记为重复:http://stackoverflow.com/a/12485390/419 - Kev

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接