0

Simple question: Can I use NSInputStream to get data from the streaming API?

If so, can someone outline what this can look like.

I have tried something like

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

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

and adding Delegate Methods, but this is not working. I realize I need to specify what users I need, however, above code is what I have so far.

Thanks!

ChrisC
  • 892
  • 2
  • 12
  • 33
  • There are a couple of angles to approach this from. However they all lead to the same key problem, authentication. – Greg Price Apr 16 '15 at 03:07

1 Answers1

1

One approach is using input streams and I do not think you would be able to pull it of easily as connecting to the streaming api requires a couple key http request headers.

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];

Is a typical pattern of connecting to a host server with an input stream. Another way is to just directly hit the URL like you do.

The problem with these approaches is that they do not address the authentication issues.

The easiest way I found was to use an NSURLSession task with the correct request headers set. Then you start receiving the data packets in the sessionDataDelegate.

Generating the twitter OAuth header can be worse than a prostate exam (trust me on this).

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

Here is a demo project where I hit the stream and dequeue searched for tweets in a tableview.

https://github.com/GregPrice24/TwitterStreamingAPI

Greg Price
  • 2,556
  • 1
  • 24
  • 33