Twitter iOS Streaming API:未收到数据

2024-03-29

我正在尝试修改Apple使用Twitter API的示例代码,以便我可以使用流API来过滤推文。我正在尝试实施针对此问题建议的方法:

TWRequest 是否适用于 Twitter 流 API? https://stackoverflow.com/questions/8566742/does-twrequest-work-for-the-twitter-streaming-api

我已按照建议创建了签名的 NSURLRequest 和 NSURLConnection 对象,并设置了连接对象的委托。选择有效的 Twitter 帐户并用于对 URL 进行签名。问题是委托连接: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

正如您所看到的,一切似乎都工作正常,唯一的问题是委托方法从未被调用,我目前不知道为什么。

任何帮助将非常感激...

Mike


我花了一些时间来启动并运行它,所以我想我应该为其他人发布我的代码。就我而言,我试图让推文靠近某个位置,所以你会看到我使用了locations参数和我在范围内的位置结构。您可以将所需的任何参数添加到参数字典中。

另请注意,这只是基本内容,您将需要执行一些操作,例如通知用户未找到帐户,并允许用户在存在多个帐户时选择他们想要使用的 Twitter 帐户。

流媒体快乐!

//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) 
                 }];
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Twitter iOS Streaming API:未收到数据 的相关文章

随机推荐

  • Spark Dataframe Write to CSV 在独立集群模式下创建 _temporary 目录文件

    我在跑步spark job在有 2 个工作节点的集群中 我使用下面的代码 spark java 将计算的数据帧作为 csv 保存到工作节点 dataframe write option header false mode SaveMode
  • 如何克隆案例类实例并仅更改 Scala 中的一个字段?

    假设我有一个案例类 代表不同社交网络上的人物角色 该类的实例是完全不可变的 并保存在不可变的集合中 最终由 Akka actor 进行修改 现在 我有一个包含许多字段的案例类 我收到一条消息 说我必须更新其中一个字段 如下所示 case c
  • Python 2.7:从文本中检测表情符号

    我希望能够检测文本中的表情符号并查找它们的名字 我没有使用 unicodedata 模块 我怀疑我不是 了解 UTF 8 约定 我猜想我需要将我的文档加载为 utf 8 然后将 unicode 字符串 分解为 unicode 符号 迭代这些
  • 调用类型参数的方法

    有没有办法做这样的代码 class GenericClass
  • iOS:如何注册推送通知?

    我正在尝试按照 Ray Wenderlich 的指南为我的 iOS 5 应用程序实现推送通知 http www raywenderlich com 3443 apple push notification services tutorial
  • 如何过滤chrome的devtool控制台历史记录

    在 bash 中 我使用历史搜索向前和历史搜索向后功能来允许我输入要运行的命令的几个字符 然后按向上箭头循环浏览历史记录中与这些字符匹配的项目 我想要 chrome devtool 控制台也有同样的东西 我经常使用向上箭头来循环浏览我的历史
  • PHP 相等(== 双等于)和恒等(=== 三等)比较运算符有何不同?

    有什么区别 and 松散究竟如何 比较工作 具体如何严格 比较工作 有哪些有用的例子 之间的区别 and 松散的区别 等于运算符和严格运算符 相同的运算符在中得到了准确的解释manual http php net manual en lan
  • iOS 将大数字转换为较小格式

    如何将所有超过 3 位的数字转换为 4 位或更少的数字 这正是我的意思 10345 10 3k 10012 10k 123546 123 5k 4384324 4 3m 四舍五入并不完全重要 但却是一个额外的优点 我已经研究过 NSNumb
  • 我如何在 Skype 上玩 google?

    此代码用于在 Skype 上向某人发送消息 但我不知道如何设置https play google com store apps details id com skype raider https play google com store
  • 如何从Excel电子表格中读取数据?

    在我正在工作的一个项目中 客户在最后一刻要求我添加从 Excel 电子表格导入数据的功能 他发送给我的示例具有 xlsx 扩展名 因此我假设它们来自 Excel 2010 但如果可能的话 我希望支持所有版本 有没有一种快速 简单的方法可以从
  • Python - 循环多维字典[重复]

    这个问题在这里已经有答案了 如果我解释我认为我在做什么 我希望有人能解释我哪里出错了 我有以下字典 ls The Wolf Gift 13 cover V Books Anne Rice The Wolf Gift 13 cover jpg
  • Linux 上的调试符号是否加载到内存中?

    从可执行文件 或共享库 中剥离调试符号是否会减少内存使用量 我知道它减少了磁盘文件的大小 我感兴趣的是实际使用的 RAM 德雷珀的论文http www akkadia org drepper dsohowto pdf http www ak
  • 每 14 天(每两周)触发一次 UILocalNotification Swift

    这个问题已经在SO上得到了回答 这是参考 iOS 通知触发 每两周和 或每季度 https stackoverflow com questions 41441124 ios notification trigger fortnightly
  • Pandas 的 T 检验

    如果我想计算 Pandas 中两个类别的平均值 我可以这样做 data Category cat2 cat1 cat2 cat1 cat2 cat1 cat2 cat1 cat1 cat1 cat2 values 1 2 3 1 2 3 1
  • 如何覆盖具有多个子项目的 SBT 项目中的子项目中的设置

    我有一个项目 其中子项目作为 git 子模块添加到子目录中 每个独立项目都有自己的build sbt文件 根项目依赖于并聚合这些子项目 如何覆盖设置值 例如organization or version 在那些子项目里面 lazy val
  • mysql使用连接池时为什么需要释放连接?

    我正在尝试实现以下nodejs mysql数据库this https medium com mhagemann create a mysql database middleware with node js 8 and async awai
  • 有没有办法在Python中按第n个分隔符分割字符串?

    例如 如果我有以下字符串 这是一个字符串 我可以将它分割为每个第二个 而不是每个 以便它返回两个值 this is 和 a string 而不是返回四个值吗 这是另一个解决方案 span 2 words this is a string s
  • 使用jquery求div内元素的总和

    我正在对一组用户进行排序 我有 4 个分组 如下所示 显示 2 个 div class groupWrapper div class groupItem div class itemHeader div class first John d
  • 如何在不链接libc.so的情况下访问段寄存器?

    我正在尝试在 Ubuntu 20 10 上使用 NASM 版本 2 15 04 在 64 位程序集中编写一个简单的堆栈金丝雀 执行下面的代码会导致在使用命令进行汇编和链接时出现分段错误nasm felf64 canary asm ld ca
  • Twitter iOS Streaming API:未收到数据

    我正在尝试修改Apple使用Twitter API的示例代码 以便我可以使用流API来过滤推文 我正在尝试实施针对此问题建议的方法 TWRequest 是否适用于 Twitter 流 API https stackoverflow com