I am working with the Twitter REST/Streaming APIs. When I want to access the REST API, I create a NSMutableURLRequest
(that contains parameters such as access tokens and queries). I then use the request in conjunction with NSURLSession
to load the data. I am using a library which creates the mutable request object for me (if I don't use the request object, then the Twitter API will not allow me to access the relevant user data).
Now I am trying to load a Twitter timeline via the streaming API. One problem I am having is that I can't figure out how to use my custom mutable request object, with a NSStream
object. The only thing I can do is to set the host
URL link. But that is not good enough because I need to pass the user OAuth data (that is included in the mutable request object), in order for the Twitter API to allow me access to the user data.
How can I attach a request object to the stream? Here is my code:
NSURL *website = [NSURL URLWithString:@"https://userstream.twitter.com/1.1/user.json"];
CFReadStreamRef readStream;
CFWriteStreamRef writeStream;
CFStreamCreatePairWithSocketToHost(NULL, (__bridge CFStringRef)[website host], 80, &readStream, &writeStream);
NSInputStream *inputStream = (__bridge_transfer NSInputStream *)readStream;
NSOutputStream *outputStream = (__bridge_transfer NSOutputStream *)writeStream;
[inputStream setDelegate:self];
[outputStream setDelegate:self];
[inputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
[outputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
[inputStream open];
[outputStream open];