Send Data to server in iOS using POST OR GET – A Simple Example

By | November 9, 2014

First Make sure you have this kind of layout and you have linked all the views to their respective variables.

For server side I am using XAMPP for Localhost for now and My php file will be residing in Applications ▸ XAMPP ▸ xamppfiles ▸ htdocs

You can download “XAMPP For Mac” from this link.

When copying this code, make changes according to your server.


iOS Server Connection using POST and GET


So I will go directly to the code.


#import <UIKit/UIKit.h>

@interface ViewController : UIViewController<NSURLConnectionDelegate>
    IBOutlet UITextField *username, *passsword;
    IBOutlet UILabel *serverResponse;



#import "ViewController.h"

@interface ViewController ()


@implementation ViewController{
    NSMutableData *mutableData;
    #define URL            @"http://localhost/test.php"  // change this URL
    #define NO_CONNECTION  @"No Connection"
    #define NO_VALUES      @"Please enter parameter values"

- (void)viewDidLoad {
    [super viewDidLoad];

    [self sendDataToServer :@"POST"];

    [self sendDataToServer : @"GET"];

-(void) sendDataToServer : (NSString *) method{
    NSString *usrname  = username.text;
    NSString *pass = passsword.text;
    if(usrname.length > 0 && pass.length > 0){
        serverResponse.text = @"Getting response from server...";
        NSURL *url = nil;
        NSMutableURLRequest *request = nil;
        // Only Difference between POST and GET is only in the way they send parameters
        if([method isEqualToString:@"GET"]){
            NSString *getURL = [NSString stringWithFormat:@"%@?username=%@&password=%@", URL, usrname, pass ];
            url = [NSURL URLWithString: getURL];
            request = [NSMutableURLRequest requestWithURL:url];
        }else{  // POST
            NSString *parameter = [NSString stringWithFormat:@"username=%@&password=%@",usrname, pass ];
            NSData *parameterData = [parameter dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
            url = [NSURL URLWithString: URL];
            request = [NSMutableURLRequest requestWithURL:url];
            [request setHTTPBody:parameterData];
        [request setHTTPMethod:method];
        [request addValue: @"application/x-www-form-urlencoded; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
        NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
        if( connection )
            mutableData = [NSMutableData new];
            serverResponse.text = NO_CONNECTION;
        serverResponse.text = NO_VALUES;

#pragma mark NSURLConnection delegates

-(void) connection:(NSURLConnection *) connection didReceiveResponse:(NSURLResponse *)response
    [mutableData setLength:0];

-(void) connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
    [mutableData appendData:data];

-(void) connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
    serverResponse.text = NO_CONNECTION;

-(void)connectionDidFinishLoading:(NSURLConnection *)connection
    NSString *responseStringWithEncoded = [[NSString alloc] initWithData: mutableData encoding:NSUTF8StringEncoding];
    //NSLog(@"Response from Server : %@", responseStringWithEncoded);
    NSAttributedString * attrStr = [[NSAttributedString alloc] initWithData:[responseStringWithEncoded dataUsingEncoding:NSUnicodeStringEncoding] options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType } documentAttributes:nil error:nil];
    serverResponse.attributedText = attrStr;


Here is the server side PHP code that can handle both POST and GET Requests.
Since you are getting the values, You can modify this code to say check with the database and
send appropriate result.

Here I am just sending back the values send from the Client Side (From iPhone or any other device)


    echo " Response From Server Side";
    echo "Username : " . $_REQUEST['username'];
    echo "Password : " . $_REQUEST['password']; // Do any database operations here... 


You can download the complete source code from here.



Website Pin Facebook Twitter Myspace Friendfeed Technorati Digg Google StumbleUpon Premium Responsive

Leave a Reply

Your email address will not be published. Required fields are marked *