Codementor Events

iOS Sending POST Data to your PHP server

Published Feb 15, 2017

Send data from your iOS app to PHP server

You can copy paste the function below, just change the URL to point to your php script.

To use:
Ceate a string with your POST data and call the function.

NSString *post_data = @"name=rob&color=green";
[self sendData:post_data];

-(void) sendData:(NSString*)post_data {
    
    NSData *postData = [post_data dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
    NSString *postLength = [NSString stringWithFormat:@"%lu", (unsigned long)[postData length]];
    
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
    [request setURL:[NSURL URLWithString:@"ENTER URL HERE"]];
    [request setHTTPMethod:@"POST"];
    [request setValue:postLength forHTTPHeaderField:@"Content-Length"];
    [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
    [request setHTTPBody:postData];
    
    NSURLSession *session = [NSURLSession sharedSession];
    NSURLSessionDataTask *task = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data,
                                                                                          NSURLResponse * _Nullable response,
                                                                                          NSError * _Nullable error) {
        
        NSMutableDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
        
        NSLog(@"%@", json);
      
        }
        
    }];
    
    [task resume];
}

Message me with any questions you may have!

Discover and read more posts from Rob Heller
get started
post comments6Replies
GUMMALLA SUDHEER
8 years ago

Hello Rob,
I have textfields in my View and also a button, When i clicked on button, I have to send the data to php server what ever i entered into that textFields., And the data should appear in Json format after sending, like inserting one filed in database table, I am Struggling to do this coz am new to iOS development, Please help me.
Thank You

Varun Jain
8 years ago

Hey Rob,
I figured it. Thanks

Varun Jain
8 years ago

Hello ROb,
Thanks a ton for the update.
This was really helpful.
One thing that i noticed is you have used dictionary instead of array here.
I understand these are very basic questions but i tried searching and couldn’t find a simple way to transverse through dictionary
In your previous tutorial the data jason came in a mutuable array which i then converted to row dictionary with help of index path.
I tried
NSString *sampleName= [json2 valueForKey:@“name”];
NSLog(@“Sample Name is %@”, sampleName);
But it returns null
This is data i get in response in json2 variable
({
appearance = “”;
attributes = ({
“attribute_id” = 7;
“attribute_label” = “”;
“attribute_type” = 1;
“attribute_value” = “”;
id = 9067;
“product_id” = 1;
}, {
“attribute_id” = 15;
“attribute_label” = “”;
“attribute_type” = 1;
“attribute_value” = 25 kg;
id = 9068;
“product_id” = 1;
});
“banner_image” = “http://www.chemkart.com/ima… Diamine.jpg”;
“cas_number” = “2855-13-2”;
“category_id” = “49,159,270,304,361,374,392,549,550,582,586”;
“chemical_formula” = C9H18N2O;
density = “”;
id = 1;
“large_image” = “http://www.chemkart.com/ima… Diamine.jpg”;
“long_desc” = “”;
“meta_description” = “Chemkart offers Isophorone Diamine from manufacturers, distributors, importers and exporters. Find COA, MSDS and product details only on www.chemkart.com”;
“meta_keywords” = “Isophorone Diamine, Ipd, Ipda, D 230, Luxamipd, Vestaminipd, Epikure 943, Aminomethyl-5, Aralditehy5083, Chemamminaca17, Isophorondiamin, C9H18N2O, 2855-13-2, manufacturer, importer, distributor, exporter, india, china, korea, usa, germany, indonesia, malaysia, COA, MSDS”;
“meta_title” = “Isophorone Diamine | 2855-13-2 | C9H18N2O | Chemkart”;
name = “Isophorone Diamine”;
“other_name” = “Ipd, Ipda, D 230, Luxamipd, Vestaminipd, Epikure 943, Aminomethyl-5, Aralditehy5083, Chemamminaca17, Isophorondiamin”;
“short_desc” = “”;
slug = “Isophorone-Diamine”;
subtitle = “”;
“thumb_image” = “http://www.chemkart.com/ima… Diamine.jpg”;
type = 1;
wishlist = 0;
}, {
appearance = “White or colourless crystalline solid”;
attributes = ({
“attribute_id” = 7;
“attribute_label” = “”;
“attribute_type” = 1;
“attribute_value” = “”;
id = 111;
“product_id” = 2;
}, {
“attribute_id” = 15;
“attribute_label” = “”;
“attribute_type” = 1;
“attribute_value” = 25 kg;
id = 112;
“product_id” = 2;
}, {
“attribute_id” = 7;
“attribute_label” = “”;
“attribute_type” = 1;
“attribute_value” = “”;
id = 8877;
“product_id” = 2;
}, {
“attribute_id” = 15;
“attribute_label” = “”;
“attribute_type” = 1;
“attribute_value” = “25 kg”;
id = 8878;
“product_id” = 2;
}, {
“attribute_id” = 7;
“attribute_label” = “”;
“attribute_type” = 1;
“attribute_value” = Drum;
id = 9045;
“product_id” = 2;
}, {
“attribute_id” = 15;
“attribute_label” = “”;
“attribute_type” = 1;
“attribute_value” = “200 kg”;
id = 9046;
“product_id” = 2;
}, {
“attribute_id” = 7;
“attribute_label” = “”;
“attribute_type” = 1;
“attribute_value” = “”;
id = 22525;
“product_id” = 2;
}, {
“attribute_id” = 15;
“attribute_label” = “”;
“attribute_type” = 1;
“attribute_value” = “25 kg”;
id = 22526;
“product_id” = 2;
}, {
“attribute_id” = 7;
“attribute_label” = “”;
“attribute_type” = 1;
“attribute_value” = “”;
id = 24791;
“product_id” = 2;
}, {
“attribute_id” = 15;
“attribute_label” = “”;
“attribute_type” = 1;
“attribute_value” = 1000 kg;
id = 24792;
“product_id” = 2;
}, {
“attribute_id” = 7;
“attribute_label” = “”;
“attribute_type” = 1;
“attribute_value” = Drum;
id = 25067;
“product_id” = 2;
}, {
“attribute_id” = 15;
“attribute_label” = “”;
“attribute_type” = 1;
“attribute_value” = 25 kg;
id = 25068;
“product_id” = 2;
}, {
“attribute_id” = 7;
“attribute_label” = “”;
“attribute_type” = 1;
“attribute_value” = Drum;
id = 25069;
“product_id” = 2;
}, {
“attribute_id” = 15;
“attribute_label” = “”;
“attribute_type” = 1;
“attribute_value” = 25 kg;
id = 25070;
“product_id” = 2;
}, {
“attribute_id” = 7;
“attribute_label” = “”;
“attribute_type” = 1;
“attribute_value” = Drum;
id = 25115;
“product_id” = 2;
}, {
“attribute_id” = 15;
“attribute_label” = “”;
“attribute_type” = 1;
“attribute_value” = 25 kg;
id = 25116;
“product_id” = 2;
}, {
“attribute_id” = 7;
“attribute_label” = “”;
“attribute_type” = 1;
“attribute_value” = Drum;
id = 25117;
“product_id” = 2;
}, {
“attribute_id” = 15;
“attribute_label” = “”;
“attribute_type” = 1;
“attribute_value” = 25 kg;
id = 25118;
“product_id” = 2;
});
“banner_image” = “http://www.chemkart.com/ima… Chloride.jpg”;
“cas_number” = “7786-30-3”;
“category_id” = “9,183,520,549,550,582”;
“chemical_formula” = MgCl2;
density = “2.32 g/cm3”;
id = 2;
“large_image” = “http://www.chemkart.com/ima… Chloride.jpg”;
“long_desc” = “”;
“meta_description” = “Chemkart offers Magnesium Chloride from manufacturers, distributors, importers and exporters. Find COA, MSDS and product details only on www.chemkart.com”;
“meta_keywords” = “Magnesium Chloride, Magnesium Dichloride, MgCl2, 7786-30-3, manufacturer, importer, distributor, exporter, india, china, korea, usa, germany, indonesia, malaysia, COA, MSDS”;
“meta_title” = “Magnesium Chloride | 7786-30-3 | MgCl2 | Chemkart”;
name = “Magnesium Chloride”;
“other_name” = “Magnesium Dichloride”;
“short_desc” = “”;
slug = “Magnesium-Chloride”;
subtitle = “”;
“thumb_image” = “http://www.chemkart.com/ima… Chloride.jpg”;
type = 1;
wishlist = 0;
}, {
appearance = “Colorless liquid with fruity odor”;
attributes = ();
“banner_image” = “http://www.chemkart.com/ima… Butyrate.jpg”;
“cas_number” = “105-54-4”;
“category_id” = “3,9,13,88,89,172,348,472,520”;
“chemical_formula” = C6H12O2;
density = “0.879 g/cm3”;
id = 13;
“large_image” = “http://www.chemkart.com/ima… Butyrate.jpg”;
“long_desc” = “”;
“meta_description” = “Chemkart offers Ethyl Butyrate from manufacturers, distributors, importers and exporters. Find COA, MSDS and product details only on www.chemkart.com”;
“meta_keywords” = “Ethyl Butyrate, Ethyl N-Butanoate, Ethyl N-Butyrate, Butanoic Acid Ethyl Ester, Butyric Acid Ethyl Ester, Butyric Ether, Un 1180, C6H12O2, 105-54-4, manufacturer, importer, distributor, exporter, india, china, korea, usa, germany, indonesia, malaysia, COA, MSDS”;
“meta_title” = “Ethyl Butyrate | 105-54-4 | C6H12O2 | Chemkart”;
name = “Ethyl Butyrate”;
“other_name” = “Barium Borate, Barium Boron Oxide, Barium-M-Borate, Barium Metaborate, Barium Diboron Tetraoxide, Bariumdiborate, Bbo(Opticalcrystal), Boricacid,Bariu”;
“short_desc” = “”;
slug = “Ethyl-Butyrate”;
subtitle = “”;
“thumb_image” = “http://www.chemkart.com/ima… Butyrate.jpg”;
type = 1;
wishlist = 0;
});
message = “10 Category Found”;
status = 200;
}

Robbie Heller
8 years ago

It seems like you have your JSON data setup a little different that I did. It looks like you need to use an NSMutablearray instead of NSDict.

Varun Jain
8 years ago

I did try that but that resulted null
anyways I figured ir out. I created seperate arrays for keys with objectatIndex method,

Thanks :)

GUMMALLA SUDHEER
8 years ago

How u created separate array for keys…?? i need to create array for values in my code., Can u help me with that.? Thank You :)

Show more replies