Building an iOS Application to Search Twitter
This article will cover building a simple Twitter client that allows users to search for tweets, save those search terms, and recall them at any time. The sample in this article will use Xcode 4 and the iPhone SDK 4.3. All examples are in Objective-C. You can find the code for this article at http://github.com/subdigital/code-mag-twitter-searcher. I encourage you to download the code to help out if you get stuck. This will be a whirlwind tour, so grab a snack, pull up to the nearest Mac and try it out with me. A Primer on Objective-C iOS applications are written in Objective-C, which is a dynamic language based on C. Objective-C takes a little getting used to; so if it is foreign to you, don’t worry. At first it can seem confusing, but eventually the syntax becomes clear. Here is a small primer to get your started: //calling methods [someOrder cancel];
/* This is also described as sending the “cancel” message to the a variable called “someOrder” */ //calling methods with input [customer setArchived:YES]; [customer setFirstName:@"Darth" lastName:@"Vader"];
/* Notice how the name of the method also helps name the arguments. Read these examples over again in your head a few times until you understand what’s going on here. */ //declare a variable, allocate an instance SomeObject *someVariable = [[SomeObject alloc] init];
/* Notice here how you can nest square brackets. Here the “alloc” message is sent to the “SomeObject” class, allocating some memory for storage. The memory is junk, however, until it has been initialized. The return value of “alloc” is sent the “init” message. In Objective-C, constructors are called initializers and always start with the word “init”. */
//declare a method - (void)doSomething {
}
/* The “-“ signifies it as an instance method (a “+” would indicate static, or class, methods). The return type is void and the method name is “doSomething” */ //declare a method with input - (BOOL)isEven:(NSInteger)input { return input % 2 == 0; }
/* Here the return type is BOOL (a boolean) and the method takes an integer argument called “input”. */
For a more detailed tutorial, check out the excellent site, CocoaDevCentral: http://cocoadevcentral.com/d/learn_objectivec/. So now that you’re an expert in Objective-C syntax, let’s delve into building an iPhone application. First, we need an IDE. Getting Xcode 4 Xcode 4 is freely available for Apple Developer Members. If you’re not a member, you can pay $4.99 at the Mac App Store to download Xcode 4. Using Xcode you can build and run applications utilizing the built-in iPhone Simulator. However if you plan to deploy your iPhone apps to a real device you’ll need to cough up $99/year to the Apple iOS Developer Program, located at http://developer.apple.com/ios. Being a member has its benefits though, such as allowing you to run your applications on a device, distribute your applications in the App Store, browsing the forums for help & advice, and getting access to pre-release versions of Xcode and the iOS SDK. If you have Xcode 3 (version 3.2.6 to be precise) then you can most likely follow along, however, Xcode 4 has changed things significantly. When you’re ready, launch Xcode 4. Creating a New iPhone Project in Xcode 4 When you first launch Xcode 4, you’re presented with a launch screen inviting you to create an application. Let’s do that now. You’ll want to select Create a new Xcode project. There are a few project templates to help you get started. For this article, choose the Navigation-based Application, as shown in Figure 2.  Figure 1: Xcode 4 Welcome screen. Figure 2: Starting off with the Navigation-based Application template.Choosing the navigation-based template will generate a bare-bones application that is useful for display lists of data. It includes a UINavigationController (useful for traversing lists of data and progressing through various screens) as well as a view controller that derives from UITableViewController. The UITableViewController is perfect for our needs because we’d like to display the Twitter search results in a list. Go ahead and select Next and give the application a name. You’ll also need to pick a bundle identifier. Typically this is in the format of com.companyname.appname. Don’t worry too much about choosing the correct value here. You can always change it later. Xcode 4 will generate a basic project that is ready to run. Figure 4 shows the starter project.  Figure 3: Giving the application a name and bundle identifier. Figure 4: The initial project created by Xcode. | & | | 
By: Ben Scheirman
Ben Scheirman is a software developer, speaker, author, and blogger. He has extensive experience programming on a multitude of platforms, such as .NET, Ruby on Rails, and iOS. He’s written two books, a couple dozen iPhone apps, and worked on countless web applications. He is the Director of Development for ChaiONE where he builds awesome Ruby and iPhone applications. You can catch him on Twitter at http://twitter.com/subdigital and on his blog online at http://flux88.com.
subdigital@gmail.com |