Twitter iOS Streaming API: no data being received

女生的网名这么多〃 提交于 2019-12-06 10:50:59

问题


I'm trying to modify Apple's sample code for using the Twitter API so that I can use the streaming API to filter tweets. I am attempting to implement the method suggested in response to this question:

Does TWRequest work for the twitter streaming api?

I have created the signed NSURLRequest and NSURLConnection objects as suggested, and set the delegate for the connection object. A valid twitter account is selected and used to sign the url. The problem is that the delegates connection:didReceiveData: method is never being called.

Here's my code:

@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");
}

The output from the program is:

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

As you can see, everything appears to work OK, the only problem is that the delegate method is never called and I currently have no idea why.

Any help would be much appreciated...

Mike


回答1:


It took me a bit of time to get this up and running, So I thought I aught to post my code for others. In my case I was trying to get tweets close to a certain location, so you will see that I used a locations parameter and a location struct I had in scope. You can add whatever params you want to the params dictionary.

Also note that this is bare bones, and you will want to do things such as notify the user that an account was not found and allow the user to select the twitter account they would like to use if multiple accounts exist.

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" forKey@"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) 
                 }];


来源:https://stackoverflow.com/questions/11621211/twitter-ios-streaming-api-no-data-being-received

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!