【问题标题】:Does TWRequest work for the twitter streaming api?TWRequest 是否适用于 twitter 流 api?
【发布时间】:2012-01-23 21:39:52
【问题描述】:

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

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

头像

【问题讨论】:

    标签: ios twitter ios5 twitter-oauth


    【解决方案1】:

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

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

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

    【讨论】:

      【解决方案2】:

      我花了一些时间来启动和运行它,所以我想我应该为其他人发布我的代码。在我的例子中,我试图让推文靠近某个位置,所以你会看到我使用了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) 
                       }];
      

      【讨论】:

      • 在发布多个问题的复制和粘贴样板/逐字答案时要小心,这些往往会被社区标记为“垃圾邮件”。如果您这样做,则通常意味着问题是重复的,因此请将它们标记为:stackoverflow.com/a/12485390/419
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-10-19
      • 1970-01-01
      • 2011-10-09
      • 1970-01-01
      • 1970-01-01
      • 2014-09-02
      • 1970-01-01
      相关资源
      最近更新 更多