2011-12-19 3 views
4

나는 인근 트윗을 보여주는 기본 아이폰 앱을 만들고자한다. 트위터 검색 API를 사용하여이 작업을 수행하기 위해 TWRequest 객체를 사용하고있었습니다. 불행히도 실제로 GPS 좌표를 사용하여지도에서 트윗을 표시하고 싶은데 검색 API가 짹짹이 도시 이름보다 정확한 정확도로 만들어진 실제 위치를 반환하지 않는 것 같습니다.트위터 스트리밍 API에서 TWRequest가 작동합니까?

이와 같이 스트리밍 API로 전환해야한다고 생각합니다. 이 경우 TWRequest 객체를 계속 사용하거나 실제로 NSURLConnection을 사용하여 전환해야하는지 궁금합니다. 미리 감사드립니다!

Avtar

답변

10

예, TWRequest 객체를 사용할 수 있습니다. 트위터 API doco의 적절한 URL과 매개 변수를 사용하여 TWRequest 객체를 만들고 TWRequest.account 속성을 트위터 계정의 ACAccount 객체로 설정합니다.

그런 다음 TWRequest의 signedURLRequest 메서드를 사용하여 connectionWithRequest : delegate :를 사용하여 비동기 NSURLConnection을 만드는 데 사용할 수있는 NSURLRequest를 가져올 수 있습니다.

이 작업이 완료되면 Twitter에서 데이터를받을 때마다 위임자의 connection : didReceiveData : 메서드가 호출됩니다. 수신 된 각 NSData 객체에는 둘 이상의 JSON 객체가 포함될 수 있습니다. NSJSONSerialization을 사용하여 각각을 JSON에서 변환하기 전에이를 "\ r \ n"으로 구분하여 분할해야합니다.

+0

그들이 쪼개 질 수 있다는 것을 몰랐습니다. – wbarksdale

3

이 작업을 수행하는 데 약간의 시간이 걸렸으므로 다른 사람들을 위해 코드를 게시해야한다고 생각했습니다. 제 경우에는 특정 위치에 트윗을 가까이하려고했기 때문에 locations 매개 변수와 위치 범위 구조체를 사용했음을 알 수 있습니다. 원하는 매개 변수를 매개 변수 사전에 추가 할 수 있습니다.

또한이 계정은 기본 계정이 아니므로 계정을 찾을 수 없다는 사실을 사용자에게 알리는 등의 작업을 수행하고 여러 계정이있는 경우 사용할 트위터 계정을 선택할 수 있습니다.

해피 스트리밍 중!

//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

+0

@Kev하지만 비정규 화는 읽기 성능을 높입니다! – wbarksdale

+0

예, 아주 좋습니다 :) – Kev

관련 문제