gpt4 book ai didi

ios - Twitter Streaming API iOS 的 NSInputStream?

转载 作者:行者123 更新时间:2023-11-29 02:13:31 25 4
gpt4 key购买 nike

简单的问题:我可以使用 NSInputStream 从流式 API 获取数据吗?

如果是这样,有人可以概述一下这看起来像什么吗。

我试过类似的东西

_twitterStream = [[NSInputStream alloc]initWithURL:[NSURL URLWithString:@"https://sitestream.twitter.com/1.1/site.json"]];;
_twitterStream.delegate = self;

[_twitterStream scheduleInRunLoop:[NSRunLoop currentRunLoop]
forMode:NSDefaultRunLoopMode];

并添加委托(delegate)方法,但这不起作用。我意识到我需要指定我需要的用户,但是,上面的代码是我目前所拥有的。

谢谢!

最佳答案

一种方法是使用输入流,我不认为您能够轻松地使用它,因为连接到流式 api 需要几个关键的 http 请求 header 。

CFReadStreamRef readStream;
CFStreamCreatePairWithSocketToHost(nil, (CFStringRef)@"Your IP Here", PORT_NUMBER, &readStream, nil);
self.stream = (__bridge NSInputStream *)(readStream);
[self.stream setDelegate:self];
[self.stream scheduleInRunLoop:[NSRunLoop currentRunLoop]
forMode:NSDefaultRunLoopMode];

是使用输入流连接到主机服务器的典型模式。另一种方法是像您一样直接点击 URL。

这些方法的问题在于它们没有解决身份验证问题。

我发现最简单的方法是使用设置了正确请求 header 的 NSURLSession 任务。然后你开始在sessionDataDelegate中接收数据包。

生成 Twitter OAuth header 可能比前列腺检查更糟糕(相信我)。

https://dev.twitter.com/oauth/overview/authorizing-requests

这是一个演示项目,我在其中点击流并出列在 TableView 中搜索推文。

https://github.com/GregPrice24/TwitterStreamingAPI

关于ios - Twitter Streaming API iOS 的 NSInputStream?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29019167/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com