ストリーミング API を使用してツイートをフィルタリングできるように、Twitter API を使用するための Apple のサンプル コードを変更しようとしています。この質問に応じて提案された方法を実装しようとしています:
TWRequest は Twitter ストリーミング API で機能しますか?
署名された NSURLRequest および NSURLConnection オブジェクトを提案どおりに作成し、接続オブジェクトのデリゲートを設定しました。有効な Twitter アカウントが選択され、URL の署名に使用されます。問題は、デリゲートの 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
ご覧のとおり、すべて正常に動作しているように見えますが、唯一の問題はデリゲート メソッドが呼び出されないことであり、現時点ではその理由がわかりません。
どんな助けでも大歓迎です...
マイク