2012-07-23 61 views
2

我试图修改Apple使用Twitter API的示例代码,以便我可以使用流媒体API来过滤推文。我试图实现该方法提出在回答这个问题:Twitter iOS流媒体API:没有收到数据

Does TWRequest work for the twitter streaming api?

我已经创建的建议已签署的NSURLRequest和NSURLConnection的对象,并设置连接对象的委托。选择一个有效的Twitter帐户并用于签署该网址。问题是委托连接: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 

正如你所看到的,一切似乎都工作正常,唯一的问题是,委托方法是永远叫,我目前不知道为什么。

任何帮助,将不胜感激......

迈克

+0

自发布以来,我得到了这个工作**没有**使用TWRequest对象。相反,我使用NSURLRequest/Connection对象,从流API接收JSON,并解析出传入的推文文本。仍然很高兴知道是否有人已经获得流媒体API与TWRequest虽然... – Michael 2012-07-24 05:26:29

+0

你可以发布一些代码呢?谢谢。 – EvilPenguin 2012-08-08 22:42:16

+0

问题在于你的NSURLConnection是从主运行循环启动的,所以线程在你的任何代表被调用之前退出。我在下面发布了一些代码。 – wbarksdale 2012-09-18 21:33:16

回答

1

我花了一些时间来得到这个启动和运行,所以我想我奥特发布我为别人的代码。在我的情况下,我试图让推特靠近某个位置,所以你会看到我使用了一个locations参数和一个位于我范围内的位置结构。你可以添加你想要的参数字典的任何参数。

另外请注意,这是裸露的骨头,你会想要做的事情,如通知用户,没有找到一个帐户,并允许用户选择他们想要使用的Twitter帐户,如果有多个帐户存在。

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" [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

你节省了我的一天:) +1 – 2013-02-11 16:54:14

+0

@wbarksdale我得到需要身份验证的resposne你对此有想法。因为我们正在使用ACAccount进行访问,所以它应该可以工作。任何帮助将不胜感激。谢谢。 – 2016-01-25 11:27:25