-2

I'm trying to create 'Contact Us' ViewController, which can send an e-mail , but I don't want to use the MFMailComposeViewController because I want to set the e-mail address which receive the message.

So..

I tried to create a viewController which take the all the message data from the user and send it to a PHP page which will do the rest of progress, so I tried to send the parameters using GET Method to the PHP page, which is good for short parameters such as 'name' or 'phone number', but I couldn't use this method for long text such as 'message content'. So I decided to use POST Method, but I couldn't find a way that could send the data from my app to PHP using POST Method

I'm trying to send three NSString with the names (MsgTitle, MsgCity, MsgContent)

My PHP code

<?php
    $MsgTitle = $_POST['title'];
    $MsgCity = $_POST['city'];
    $MsgContent = $_POST['description'];
?>

I hope there is an easy way to do that, and thank you in advance ...

Community
  • 1
  • 1
MujtabaFR
  • 5,956
  • 6
  • 40
  • 66
  • Did you google? Try: `objective-c php request` as search terms. – nhgrif Apr 21 '14 at 22:33
  • How to perform an [HTTP Post in iOS](http://stackoverflow.com/questions/5537297/ios-how-to-perform-an-http-post-request). – Mark S. Apr 21 '14 at 22:35
  • Your first sentence makes no sense at all. You said "I don't want to use the MFMailComposeViewController because I want to set the e-mail address which receive the message". But [MFMailComposeViewController has a "`setToReceipients`" API](https://developer.apple.com/library/ios/documentation/MessageUI/Reference/MFMailComposeViewController_class/Reference/Reference.html#//apple_ref/occ/instm/MFMailComposeViewController/setToRecipients:) that DOES allow you to set the destination e-mail addresses. – Michael Dautermann Apr 21 '14 at 22:56

1 Answers1

1

Create an NSMutableURLRequest object, set the HTTPMethod to @"POST", put the parameters in the HTTP body as a properly formatted string — the same as GET parameters:

[request setHTTPBody:@"subject=foo&message=lorem%20ipsum"];

Send the request using NSURLConnection and check for an error:

NSHTTPURLResponse *urlResponse = nil;
NSError *error = NULL;
NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&urlResponse error:&error];

if (!responseData || urlResponse.statusCode != 200) {
  NSLog(@"error: %@", error); // probably want to also tell the user that the message failed
}
Abhi Beckert
  • 32,787
  • 12
  • 83
  • 110