SLRequest 클래스를 사용하여 Twitter Streaming API에서 데이터를 가져 오려고합니다. 아래 코드에서 설명하는 끝점과 매개 변수를 사용하면 프로그램이 "중단" 이고 JSON 데이터가 인쇄되지 않습니다. twitter dev의 예를 기반으로 한 종단점을 사용하고 있습니다. 웹 사이트 https://dev.twitter.com/docs/streaming-apis/parameters#with 특정 위치의 트윗을 요청하고 있습니다.Twitter 스트리밍 끝점이 SLRequest 객체와 함께 작동하지 않습니다.
이 코드를 사용하여 REST API (코드 및 요청이 포함되어 있지만 이 주석 처리 되었음)를 사용하여 내 타임 라인을 쿼리 할 때 프로그램이 멈추지 않고 유효한 응답을 얻습니다.
스트리밍 API를 사용하여 데이터에 액세스하려면 구현해야하는 코드가 있습니까? 어떤 추가 수정 또는 변경이 필요합니까?
ACAccountStore * accountStore = [[ACAccountStore alloc] init];
ACAccountType * twitterAccountType =
[accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];
// Ask the user permission to access his account
[accountStore requestAccessToAccountsWithType:twitterAccountType options:nil completion:^(BOOL granted, NSError *error) {
if (granted == NO) {
NSLog(@"-- error: %@", [error localizedDescription]);
}
if (granted == YES){
/***************** Create request using REST API*********************
***************** This URL is functional and returns valid data *****
NSURL * url = [NSURL URLWithString:@"https://userstream.twitter.com/1.1/user.json"];
SLRequest * request = [SLRequest requestForServiceType:SLServiceTypeTwitter requestMethod:SLRequestMethodGET URL:url parameters:@{@"screen_name": @"your_twitter_id"}];
***************************************************************/
// Create request using Streaming API Endpoint
NSURL * url = [NSURL URLWithString:@"https://stream.twitter.com/1.1/statuses/filter.json"];
NSMutableDictionary *params = [[NSMutableDictionary alloc] init];
[params setObject:@"track" forKey:@"twitter&"];
[params setObject:@"locations" forKey:@"-122.75,36.8,-121.75,37.8"];
SLRequest * request = [SLRequest requestForServiceType:SLServiceTypeTwitter requestMethod:SLRequestMethodPOST URL:url parameters:params];
NSArray * twitterAccounts = [accountStore accountsWithAccountType:twitterAccountType];
if ([twitterAccounts count] == 0) {
(NSLog(@"-- no accounts available"));
} else if ([twitterAccounts count] >0){
[request setAccount:[twitterAccounts lastObject]];
NSLog([request.account description]);
NSLog(@"Twitter handler of user is %@", request.account.username);
// Execute the request
[request performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) {
NSError * jsonError = nil;
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingAllowFragments error:&jsonError];
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
// NSLog(@"-- json Data is %@", json);
NSLog([json description]);
}];
}];
}
}
}];