TWRequest是否适用于Twitter流式传输API?

时间:2011-12-19 19:55:19

标签: ios twitter ios5 twitter-oauth

我正在尝试制作一个显示附近推文的基本iphone应用。我正在使用TWRequest对象通过twitter搜索API完成此操作。不幸的是,我实际上想要使用他们的GPS坐标在地图上标记推文,搜索api似乎没有返回推文的实际位置,其准确性比城市名称更高。

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

Avtar

2 个答案:

答案 0 :(得分:10)

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

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

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

答案 1 :(得分:3)

我花了一些时间来启动并运行,所以我认为我应该为其他人发布我的代码。在我的情况下,我试图让推文靠近某个位置,所以你会看到我使用了locations参数和我在范围内的位置结构。你可以在params字典中添加你想要的任何参数。

另请注意,这很简单,您需要做一些事情,例如通知用户未找到帐户,并允许用户选择他们想要在多个帐户存在时使用的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) 
                 }];
相关问题