0

I am very new to iOS and I've just begun reading about HTTP requests and POST and GET methods. Let's say, for example, I want to have the user input a string, and then send that data to a website, (for this example, say www.rhymezone.com), search with that string, and get the results of that search within my application. Is this done with an HTTP post method? Or what? Any help / examples would be greatly appreciated. Also, if there are tutorials for this stuff, that would be appreciated as well.

For sake of example, here is what I've tried:

NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.rhymezone.com/r/rhyme.cgi?Word=test&typeofrhyme=perfect&org1=syl&org2=l&org3=y"]];

NSURLConnection *connection = [NSURLConnection connectionWithRequest:request delegate:self];



- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    NSString *dataAsString=[[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
    NSLog(@"data: %@",dataAsString);
 }

This outputs the entire source of the website (searching for rhymes of the word test). While I can certainly write a method to go through the source of the website and extract the words it returns, I feel like this is not correct. My way of getting rhymes of different words is simply to change the URL here, so where it says 'test' I change it to whatever the user inputs.

Thanks

mmc
  • 17,354
  • 2
  • 34
  • 52
gg13
  • 574
  • 6
  • 19
  • 1
    If you're asking about RhymeZone.com, I would suggest contacting the authors of that site and ask if they have a public programmatic interface (either JSON or XML, most likely). They almost certainly do (because their apps are using it, unlikely to be scraping HTML output). The only question is whether this is a public interface or not. Contact them via their web site. But it's not cool to be using a third party web site as a feed to your app without coordinating with the web site administrators, unless, of course, they have a public API. – Rob Dec 25 '12 at 20:31

3 Answers3

1

If I understand correctly what you are trying to do, I fear that the only option for you is sending the HTTP request (GET or POST according to what the website expects, just like you are doing) and then parse the result to filter all the information that is not relevant.

An alternative approach would be possible if you were using a website offering a REST API, or a JSON API so that you send the query and you get back just the information you need (in a specific format).

So, it depends strongly on the website you are using, but for the generic case, the only option you have is parsing.

(Or, you could display the full content of the page through UIWebView. This would not require explicitly setting up a connection, but I am not sure it is what you are trying to do.)

sergio
  • 68,819
  • 11
  • 102
  • 123
1

You are looking for a way to communicate with your website from your iOS application. The common approach is to get the string entered by the user, encode and send it as http request to a sort of script (webservice). This script will do all the stuff you want (search with this string). Then re-send the result to the client (your iOS app) as a http response which will be parsed in your iOS app(with a JSON parser for instance).

There is good resources around that, as an example, you may read this: http://www.raywenderlich.com/2965/how-to-write-an-ios-app-that-uses-a-web-service

Malloc
  • 15,434
  • 34
  • 105
  • 192
  • Hey, sorry for getting back so late but thank you! I did end up using a JSON parser. – gg13 Dec 31 '12 at 21:34
1

Look into AFNetworking and RestKit.

It's easiest if you're calling a public API that uses JSON/XML, and then use a built in parser or a parser library to extract the data you want.

Simply downloading the contents of a URL is an HTTP GET request, such as going to a website.

This link talks a bit more about the difference between GET and POST. When do you use POST and when do you use GET?

Community
  • 1
  • 1
Rohan Agarwal
  • 2,167
  • 1
  • 24
  • 34