You can use NSURLConnection in combination with NSMutableURLRequest as follows:
//Prepare URL for post
NSURL *url = [NSURL URLWithString: @"https://mywebsite/register.php"];
//Prepare the string for your post (do whatever is necessary)
NSString *postString = [@"username=" stringByAppendingFormat: @"%@&password=%@", uname, pass];
//Prepare data for post
NSData *postData = [postString dataUsingEncoding:NSUTF8StringEncoding];
NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];
//Prepare request object
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:url];
[request setTimeoutInterval:20];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];
//send post using NSURLConnection
NSURLConnection connection = [[NSURLConnection alloc]initWithRequest:request delegate:self];
//Connection should never be nil
NSAssert(connection != nil, @"Failure to create URL connection.");
This (with the postString adapted to your needs) should send a POST request asynchronously (in the background of your app) to your server and the script on your server is run.
Assuming that your server is also returning an answer, you can listen to this as follows:
When creating the connection with NSURLConnection connection = [[NSURLConnection alloc]initWithRequest:request delegate:self];
, we told the connection that we process what the server returns within the same object, i.e. self.
When the connection returns an answer the following three methods are called (put them in the same class as the code above):
-(void)connection:(NSURLConnection*)connection didReceiveResponse:(NSURLResponse*)response
{
//Initialize received data object
self.receivedData = [NSMutableData data];
[self.receivedData setLength:0];
//You also might want to check if the HTTP Response is ok (no timeout, etc.)
}
-(void)connection:(NSURLConnection*)connection didReceiveData:(NSData*)data
{
[self.receivedData appendData:data];
}
-(void)connection:(NSURLConnection*)connection didFailWithError:(NSError*)error
{
NSLog(@"Connection failed with err");
}
-(void)connectionDidFinishLoading:(NSURLConnection*)connection
{
//Connection finished loading. Processing the server answer goes here.
}
Keep in mind, that you have to handle the server authentication that comes with with your https connection/ request.
To allow any server certificate, you can rely on the following solution (although this is just recommended for quick prototyping and testing):
How to use NSURLConnection to connect with SSL for an untrusted cert?
Hope that helped.