Twitter API를 사용하여 Apple의 샘플 코드를 수정하여 스트리밍 API를 사용하여 트윗을 필터링 할 수 있습니다. 내가 제안 서명 NSURLRequest하고있는 NSURLConnection 개체를 만든Twitter iOS Streaming API : 데이터 수신하지 않음
Does TWRequest work for the twitter streaming api?
, 연결 객체의 대리자를 설정 :이 질문에 대한 응답 방법을 구현하는 것을 제안 시도하고있다. 유효한 트위터 계정이 선택되어 URL에 서명하는 데 사용됩니다. 문제는 delegates connection : didReceiveData : 메서드가 결코 호출되지 않는다는 것입니다.
@implementation Twitter
-(id)init{
if (self=[super init]) {
NSLog(@"Twitter init");
// Tell the notification centre to inform the app if the twitter account changes
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(twitterAccountChanged)
name:ACAccountStoreDidChangeNotification object:nil];
// Create an account store object.
ACAccountStore *accountStore = [[ACAccountStore alloc] init];
// Create an account type that ensures Twitter accounts are retrieved.
ACAccountType *accountType = [accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];
// Request access from the user to use their Twitter accounts.
[accountStore requestAccessToAccountsWithType:accountType withCompletionHandler:^(BOOL granted, NSError *error) {
if(granted) {
NSLog(@"Twitter: Access to twitter accounts granted");
// Get the list of Twitter accounts.
NSArray *accountsArray = [accountStore accountsWithAccountType:accountType];
// Pick the twitter account to use
if ([accountsArray count] > 0) {
// Grab the initial Twitter account to tweet from.
ACAccount *twitterAccount = [accountsArray objectAtIndex:0];
// This is for a status filter
NSURL *url=[NSURL URLWithString:@"https://stream.twitter.com/1/statuses/filter.json"];
// Create the parameters dictionary
NSDictionary *dictionary=[NSDictionary dictionaryWithObjectsAndKeys:@"twitter", @"track", nil];
// Create TWRequest object
TWRequest *req=[[TWRequest alloc] initWithURL:url parameters:dictionary requestMethod:TWRequestMethodPOST];
// Set the account
[req setAccount:twitterAccount];
// Get a signed URL request
NSURLRequest *signedRequest=[req signedURLRequest];
// Initate the connection
[NSURLConnection connectionWithRequest:signedRequest delegate:self];
} else {
NSLog(@"Twitter: No twitter accounts to access");
}
} else {
NSLog(@"Twitter: Access to twitter accounts denied");
}
}];
return self;
}
return nil;
}
-(void)twitterAccountChanged{
NSLog(@"Twitter twitterAccountChanged");
}
-(void) connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{
NSLog(@"data received");
}
프로그램의 출력은 다음과 같습니다 : 여기
내 코드의2012-07-24 09:50:03.668 TwitterAPITest[36722:10403] Twitter init
2012-07-24 09:50:03.836 TwitterAPITest[36722:11d03] Twitter: Access to twitter accounts granted
당신이 볼 수 있듯이이 모든 것이 OK 작업 표시, 유일한 문제는 위임 방법은 결코 없다는 것입니다 불렀고 나는 현재 이유를 모른다.
어떤 도움이 많이 주시면 감사하겠습니다 ...
마이크
글을 올린 후 TWRequest 객체를 ** 사용하지 않고이 작업을 ** 수행했습니다. 대신 NSURLRequest/Connection 객체를 사용하고, 스트리밍 API에서 JSON을 받고, 들어오는 트윗 텍스트를 파싱합니다. 비록 누군가가 TWRequest와 함께 작동하는 스트리밍 API를 얻었는지 알기에 여전히 좋을 것입니다 ... – Michael
이 코드를 게시 할 수 있습니까? 감사. – EvilPenguin
문제는 간단히 NSURLConnection이 주 실행 루프에서 시작 되었기 때문에 스레드가 호출되어 대리자를 호출 할 수 없기 때문입니다. 아래에 몇 가지 코드를 게시했습니다. – wbarksdale