Gordon Peterson
Gordon Peterson

Reputation: 23

Searching an API in Xcode

I am developing an iPhone application that is using an API to find recipes from http://www.recipepuppy.com/about/api/.

There are three parameters that must be put into return results. There are i, q, and p, Where i are the ingredients, q in a normal search query and p is the page number. I am able to specifically add these parameters and then load the results into a table view in Xcode.

I also want to implement a search that allows the users to search for recipes based on whatever they feel like and return the results. Could someone point me in the correct direction on how to do this. I know I will have to take what ever the user inputs and place it into a string but how do I implement that string into the parameters of the URL?

Upvotes: 0

Views: 690

Answers (3)

Marin Todorov
Marin Todorov

Reputation: 6397

To build a solid app that involves JSON communication over the network, you can use the JSONModel library. It provides you with data models, and takes care of sending and receiving JSON forth and back to your API service.

You can have a look at the GitHub page: https://github.com/icanzilb/JSONModel/

Or also to this example on how to use the YouTube JSON API with JSONModel: http://www.touch-code-magazine.com/how-to-make-a-youtube-app-using-mgbox-and-jsonmodel/

On the GitHub page there are more code samples. Good luck :)

(Disclosure: I'm JSONModel's author)

Upvotes: 1

Robert Wagstaff
Robert Wagstaff

Reputation: 2674

I'd use AFNetworking for all your network requests: https://github.com/AFNetworking/AFNetworking

To make the request just construct an NSString and send it off

[NSString stringWithFormat:@"http://www.recipepuppy.com/api/?i=%@&q=%@&p=%d", self.ingredients, self.query, self.page] 

Upvotes: 0

Aaron Wojnowski
Aaron Wojnowski

Reputation: 6480

To answer your question:

I know I will have to take what ever the user inputs and place it into a string but how do I implement that string into the parameters of the URL?

You can use the stringWithFormat method of NSString. For example:

NSString *ingredients = @"ingredients";
NSString *query = @"soups";
NSString *page = @"1";

NSString *url = [NSString stringWithFormat:@"http://www.recipepuppy.com/api/?i=%@&q=%@&p=%@",ingredients,query,page];

Before using this URL, it's recommended that you URL encode it.

NSString *encodedURL = [url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

Now that you have your URL, just start a connection to the web (NSURLConnection, AFNetworking, the choice is yours), parse the data returned, and load that into an array to display in the table view.

Upvotes: 1

Related Questions