One of the most popular blog entries at RemarkablePixels is an entry I wrote back in December called Parsing JSON in iOS the right way. Very frequently when people are asking me about parsing JSON they are usually talking about something related to Twitter, commonly the Twitter Search API.
The Twitter Search API is a very straightforward and powerful API, provided by Twitter to allow developers to get realtime search results. The resultset from these queries is very robust and contains lots of useful information besides just the text and username of a tweet. This is presented back to the developer in a JSON wrapper for easy parsing. Luckily iOS has a great framework for handling JSON data and asynchronous HTTP requests.
Lets get started...The project and source code is available on GitHub if you would rather see everything together. Otherwise follow along below...
Create a new project in XCode, a View-based App.
Download the JSON framework for iOS on GitHub - Here
Drag the JSON files into your project.
This simple example will include 3 pieces. A view controller to control the flow, a Search Twitter class, and a View to present the results.
Lets start with the heavy lifting first, the Search Twitter class. Add a new Objective C class to your project and name it SearchTwitter.
We will first populate our header with one instance variable and one method we need to search twitter.
The Search Twitter class will take one input, an NSString from our first view controller, which is the term that the user would like to search twitter for. We will also need to sanitize this input to substitute any white space in the search term or other characters that are common, but will break the API call. This is easily handled with a couple of NSRegularExpressions and stringByReplacingMatchesInString functions.
Once we have passed through our sanitizer, we then need to create a URL string for the Twitter Search API which will include our search term and the number of results we would like to receive. We then take this NSString and create an NSURL object which we can use in our next step to retreive the results of this call.
We then make an asynchronous call with the NSURL object created to start retreiving the results of the search.
Now that our search is in flight, we need a way to catch the JSON results as they come back. As I wrote previously, you cannot just take the first set of data that comes back, otherwise you can end up an invalid JSON resultset. To handle this we will implement the NSURLConnection delegate methods, connection didReceiveData and connectionDidFinishLoading.
With connection didReceiveData we need to allocate our tweetBlob on the first pass, and then on every subsequent call, append the data to this blob.
Once all the data has finished downloading the connectionDidFinishLoading will be called. In here we will want to take the completed blob of JSON tweets and put it through the JSON parser. We also need a way to signal back to our view that the request has completed, successfully or unsuccessfully. To do this we will send a notification using NSNotificationCenter, and then setup a listener on our view to receive the dictionary of results.
Now that we have finished the implementation of the searchTwitter class, we can move on to our view controller and view.
The view controller is pretty straightforward, we will create three instance variables, one for our SearchTwitter class, and two IBOutlets for our search view.
The implementation consists of three pieces, the allocation of our SearchTwitter class, the handling of receiving the search term, and then pushing our results view onto the view stack.
The last piece of the puzzle is the tableview that will present our results. The header file is one simple piece an NSMutableArray that we will use to populate the rows in our table view.
Within the implementation we need to now setup the NSNotification center to listen for the tweets results, and then handle the data that comes in.
The last step is to implement the tableview methods to populate our data in the cells.
Thats it we are done. You should now have a simple example of how to implement the Twitter Search API in iOS.