Does TWRequest work for twitter streaming?

I am trying to make a basic iphone application that shows nearby tweets. I used the TWRequest object to accomplish this using the twit search api. Unfortunately, I would really like to mark the tweets on the map using their GPS coordinates, and the api search does not seem to return the actual location in which the tweet was made with better accuracy than the name of the city.

As such, I think I need to switch to a streaming api. I am wondering if it is possible to continue to use the TWRequest object in this case, or do I need to switch to using NSURLConnection? Thanks in advance!

Avtar

+4
source share
2 answers

Yes, you can use the TWRequest object. Create your TWRequest object using the appropriate URL and parameters from the Twitter API, and set the TWAquest.account property to an ACAccount object for your Twitter account.

You can then use the signedURLRequest method for TWRequest to get the NSURLRequest, which can be used to create an asynchronous NSURLConnection using connectionWithRequest: delegate :.

Once this is done, the delegate connection: the didReceiveData: method will be called whenever data is received from Twitter. Please note: each NSData object received may contain more than one JSON object. You will need to split them (separated by "\ r \ n") before converting each of the JSON using NSJSONSerialization.

+10
source

It took me a while to get this to work, so I thought I should post my code to others. In my case, I tried to get tweets in a specific place, so you will see that I used the locations parameter and the location structure that I had in scope. You can add any parameters you want to the params dictionary.

Also note that these are bare bones, and you will want to do things like notifying the user that the account was not found, and allowing the user to select the Twitter account that they would like to use if there are multiple accounts.

Happy streams!

 //First, we need to obtain the account instance for the user 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) }]; 
+3
source

Source: https://habr.com/ru/post/1387087/


All Articles