2011-12-19 48 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