2011-12-19 127 views
4

我想做一个基本的iPhone应用程序,显示附近的鸣叫。我正在使用TWRequest对象来使用twitter搜索api完成此操作。不幸的是,我实际上想使用他们的GPS坐标在地图上标记推文,而搜索api似乎没有以比城市名称更好的准确性返回发布推文的实际位置。TWRequest是否适用于Twitter流式API?

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

Avtar

回答

10

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

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

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

+0

不知道他们可能会分手,谢谢那个珍闻! – wbarksdale 2012-09-18 00:33:17

3

我花了一点时间才得到这个启动和运行,所以我想我应该把我的代码发布给其他人。在我的情况下,我试图让推特靠近某个位置,所以你会看到我使用了一个locations参数和一个位于我范围内的位置结构。你可以添加你想要的参数字典的任何参数。

另外请注意,这是裸露的骨头,你会想要做的事情,如通知用户,没有找到一个帐户,并允许用户选择他们想要使用的Twitter帐户,如果有多个帐户存在。

Happy Streaming!

//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" [email protected]"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) 
       }]; 
+0

在发布复制和粘贴样板/逐字回答多个问题时要小心,这些往往会被社区标记为“垃圾”。如果你这样做,那么它通常意味着问题是重复的,所以将它们标记为:http://stackoverflow.com/a/12485390/419 – Kev 2012-09-18 22:08:10

+0

@Kev但是非规范化会提高读取性能! – wbarksdale 2012-09-19 01:15:46

+0

是的,非常好:) – Kev 2012-09-19 01:17:04