0

I'm currently working with a PHP developer to set up some server-side code, however, I'm not sure how to send the server API information to be stored in a database. He has asked me to send it in a url like this: exampleserver.com/register?deviceToken=[deviceToken]&otherstuff[otherStuff]. I have no problem with creating the URL string, my issue is actually doing something with it. I know this is a pretty stupid question, but I'm pretty new to Objective-C let alone communicating with servers! I have pulled information from servers using NSURLRequest and AFJSONRequestOperation before. Is it the same idea or are we no longer doing Requests? I've seen the word Post around a couple of times, but I'm unsure if this is what I'm after. Any help clearing this up would be really appreciated. Also, whatever the solution, I need it to be asynchronous!

Thanks for the help,
Regards,
Mike

Mackey18
  • 2,322
  • 2
  • 25
  • 39

2 Answers2

1

NSURLRequest still works and is fine. If you want a more powerful library that handles post, get, put etc. and can work asynchronously and link directly to core data, I recommend RestKit (https://github.com/RestKit/RestKit).

For more on NSURL, see my answer here: NSURLConnection delegate method

Community
  • 1
  • 1
Eric
  • 4,063
  • 2
  • 27
  • 49
  • Thank you, so all I have to do is use NSURLRequest to send the information to the server? Thank you! – Mackey18 Dec 09 '12 at 17:54
1

This works for me:

    NSURL *aURL = [NSURL URLWithString:@"http://www.google.co.uk/search?hl=en&q=hello%20world"];
NSURLRequest *aURLRequest = [NSURLRequest requestWithURL:aURL];
[NSURLConnection sendAsynchronousRequest:aURLRequest queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error){
    NSLog(@"response=%@", response);
    NSLog(@"data=%@", data);
    NSLog(@"error=%@", error);
}];

The URL you show has data tacked onto the end of the URL, which is normally done with a http GET operation, the "normal" method. Just asking for the page at that URL is enough to send the data to the server. An http POST operation is typically used to send form data to a server, where the pairs like deviceToken=<deviceToken> are transferred in the body of the message rather than the URL. The advantage of that is typically that the body will be encrypted if the connection is https:, so the data stays secure. But for a simple insecure transaction, using a GET with the parameters in the URL is fine. There's a description of a POST transaction at iOS: how to perform a HTTP POST request?

Community
  • 1
  • 1
emrys57
  • 6,679
  • 3
  • 39
  • 49
  • This is such a fantastic answer. Thank you for explaining everything so clearly and providing examples. Answer awarded for sheer clarity and effort! – Mackey18 Dec 09 '12 at 18:27
  • Pleased to be able to help! I had to sort out exactly the same thing a few months ago, so I know how you feel. And thanks for the tick! – emrys57 Dec 09 '12 at 18:32