Я создал новый проект, в котором я должен хранить данные на моем сервере. но всякий раз, когда я отправляю данные, он сохраняет нулевое значение в моих полях таблицы. Я пробовал это много раз, но он не работает.Как отправить данные на веб-сервер с помощью Objective C?
Любая помощь будет принята с благодарностью.
код PHP скрипт
$name = $jsonObj['name'];
$email = $jsonObj['email'];
$phone = $jsonObj['phone'];
$comment = $jsonObj['comment'];
мой ViewController.h файл
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
{
NSMutableData*mutableData;
}
@property (strong, nonatomic) IBOutlet UITextField *txname;
@property (strong, nonatomic) IBOutlet UITextField *txemail;
@property (strong, nonatomic) IBOutlet UITextField *txcontect;
@property (strong, nonatomic) IBOutlet UITextField *txcomment;
- (IBAction)submit:(id)sender;
@end
мой ViewController.m файл
- (IBAction)submit:(id)sender
{
//Here YOUR URL
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"my URL…..."]];
//create the Method "GET" or "POST"
[request setHTTPMethod:@"POST"];
//Pass The String to server(YOU SHOULD GIVE YOUR PARAMETERS INSTEAD OF MY PARAMETERS)
NSString *userUpdate =[NSString stringWithFormat:@"name=%@&email=%@&phone=%@& comment=%@&",_txname.text,_txemail.text,_txcontect.text,_txcomment.text,nil];
//Check The Value what we passed
NSLog(@"the data Details is =%@", userUpdate);
//Convert the String to Data
NSData *data1 = [userUpdate dataUsingEncoding:NSUTF8StringEncoding];
//Apply the data to the body
[request setHTTPBody:data1];
//Create the response and Error
NSError *err;
NSURLResponse *response;
NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&err];
NSString *resSrt = [[NSString alloc]initWithData:responseData encoding:NSASCIIStringEncoding];
//This is for Response
NSLog(@"got response==%@", resSrt);
if(resSrt)
{
NSLog(@"got response");
else
{
NSLog(@"faield to connect");
}
}
вы должны дать полный код PHP скрипта, а не 4 строки – Shubhank
Вашей цель с кодом выглядит отлично. Проблема должна быть с сервера. вы получаете ответ? – Lion