Microsoft recently announced a new SDK for Cocoa and Cocoa touch applications that would easily allow developers to perform various searches using their new Bing "decision" engine. As a person actively working on iPhone applications, this is something I had to try out. This tutorial will introduce you to the Bing SDK and how to use it to build a simple web search application.
The first thing we're going to need to do is get the Bing SDK into our XCode project. Their project is hosted on codeplex and offers some pretty good "Getting Started" documentation that makes it easy to get things up and running. The first thing you need to do is download the Bing SDK source code, which comes in the form of an XCode project. Next, you need to open up your project. All that's left is to simply drag the "Bing" folder from their project to yours.
The next thing we need is a user interface. For this application, I built a very simply UI just to demonstrate how to use the SDK. It contains a search box and a UITableView for displaying search results.
I'm not going to go into any details on how to build this user interface since that's not what this tutorial is about. What we do need to look at though is the delegate function that is called when someone has entered text into the search box and has clicked the "Search" button.
// Make the keyboard go away.
[searchBar resignFirstResponder];
// Create a Bing object.
Bing *bing = [[Bing alloc] initWithApplicationID:APP_ID];
// Create a Bing request.
BingWebRequest *request = [[BingWebRequest alloc] init];
// Perform the search and save the response to the BingWebResponse property.
self.response = (BingWebResponse*)[bing search:searchBar.text
withRequest:request];
// Clean up.
[request release];
[bing release];
// Tell the UITableView to reload its data.
[self.searchResultTableView reloadData];
}
Using the Bing SDK is pretty intuitive. The first thing I do is actually alloc my Bing object passing in my application ID, which is defined in my UIViewController's header. In order to get an application ID, you'll have to head on over to bing.com/developers and answer a few questions. The next thing I do is create a BingWebRequest object that is used to tell Bing what kind of data I'm looking for. If I were looking for images, I'd use a BingImageRequest here instead. Next I do the actual search, synchronously. Bing also supports asynchronous searches, but that's a topic for another day. I save the result to a property in my UIViewController so it can be accessed later when the UITableView is updated. The last thing I do is tell the table view to reload its data, which will display the search results.
In order to use these objects, you'll have to include some headers.
#import "BingWebRequest.h"
#import "BingWebResponse.h"
#import "BingWebResult.h"
Now let's look at populating the table. The first thing we'll need to look at is the numberOfRowsInSection UITableViewDataSource method.
if(response != nil) {
return [response.results count];
}
return 0;
}
I first check to see if the response is nil. If it is, that means we haven't performed a search yet and should return 0. If it's not nil, then get the number of results by querying the count method on the results property of our BingWebResponse.
Next up is populating the table.
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc]
initWithFrame:CGRectZero
reuseIdentifier:CellIdentifier]
autorelease];
}
cell.textLabel.text = [[response.results objectAtIndex:indexPath.row] title];
return cell;
}
Most of this method is the standard code for reusing table cells. The only thing that's been added is getting the title of a search result. Setting the text directly on a UITableViewCell using the text property has been deprecated as of OS 3.0, so we're using cell.textLabel.text to do it. The search results are stored in the results collection. Simple get one by using objectAtIndex and then call the title method to get the title.
That's it for this tutorial. I'm glad to see Microsoft making it as easy as possible to use Bing in our own applications. It's also great that they've opened up the source to this SDK for us to look at. If you've got any questions, comments, or know of any apps that use the Bing SDK, drop us a comment below.
Add Comment
[language] [/language]
Examples:
[javascript] [/javascript]
[actionscript] [/actionscript]
[csharp] [/csharp]
See here for supported languages.
Javascript must be enabled to submit anonymous comments - or you can login.