Hey, Iam new to objective c and really dont know much about it. I have been given a query that is gonna I need to send data in to the server.Query is like this http://abc.com/insertipademail.php?name=name&email=email I need to enter the name and email I have constructed the string But I dont know how to send it to the server. Can someone help me out please. Or point me in the right direction. Thanks
A:
For starters, take a look a NSString's stringWithContentsOfURL:encoding:error: method. You could do something like:
// NSString * myURLString = whatever you do to build the url
NSURL * myURL = [NSURL URLWithString: myURLString];
NSString * response = [NSString stringWithContentsOfURL: myURL encoding: NSUTF8StringEncoding error: NULL];
NSLog(@"The response was: %@", response);
As written, this will ignore all errors and perform the request synchronously. In a real app, you probably want to handle any error that occur, and perhaps perform the request in the background. See the URL Loading System Programming Guide for further documentation. You can also try using any of several open source libraries such as those suggested in David M.'s answer.
Jason Jenkins
2010-09-08 17:00:10
You can use NSURLConnection, as well, but in my experience, the HTTP libraries listed mean a lot less work for the same result. Also, it sounds like the questioner wants to POST, so stringWithContentsOfURL won't work.
David M.
2010-09-08 17:39:16
His sample url includes a query string, so it looks like a GET to me. At any rate, given his stated new-to-Objective-C status, I though it would be best to give the simplest possible solution first in just a couple of lines of code, then build complexity on from there. I didn't want to overwhelm him with learning how to incorporate a 3rd party library into his app before he understands the basics of the SDK and the language. Walk before you fly...
Jason Jenkins
2010-09-08 21:11:23
The poor soul's script's name begins with "insert", so it probably *should* be a POST, but probably really is a GET. Oh well.
David M.
2010-09-08 22:21:31
Thanks Jason, you are right I couldnt understand the complex stuff yet. What you posted worked like a charm. Gonna do more digging on the other libraries now. Thanks for the help guys, really appreciate it.
Zubair
2010-09-09 06:23:58
@Zubair - If you feel my answer was correct, please accept the answer by clicking on the check box outline to the left. You may also want to vote up any other answers to the question that to think are helpful by clicking on the up triangle above the number to the left.
Jason Jenkins
2010-09-09 17:17:34