Ios 如何使用Objective C将数据发布到web服务器?

Ios 如何使用Objective C将数据发布到web服务器?,ios,objective-c,iphone,xcode,Ios,Objective C,Iphone,Xcode,我已经创建了一个新项目,使用它我必须在服务器数据库表中存储数据。但每当我发送数据时,它都在我的表字段中存储空值。我试了很多次,但都不起作用 任何帮助都将不胜感激 PHP脚本代码 我的ViewController.h文件 您没有发送json对象。因此,在php代码中,尝试获得如下值: $name = $_POST['name']; $email = $_POST['email']; $phone = $_POST['phone']; $comment = $_POST['comment']

我已经创建了一个新项目,使用它我必须在服务器数据库表中存储数据。但每当我发送数据时,它都在我的表字段中存储空值。我试了很多次,但都不起作用

任何帮助都将不胜感激

PHP脚本代码 我的ViewController.h文件
您没有发送json对象。因此,在php代码中,尝试获得如下值:

$name = $_POST['name'];
$email = $_POST['email'];  
$phone = $_POST['phone'];  
$comment = $_POST['comment']; 

您需要给出完整的php脚本代码,而不是4行代码。您的目标c代码看起来不错。问题应该来自服务器端。你得到回应了吗?是的。顺便问一下,在您的代码中,
$jsonObj
是什么?你能在那行之前展示一些代码吗?
#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
     - (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");
}
}
$name = $_POST['name'];
$email = $_POST['email'];  
$phone = $_POST['phone'];  
$comment = $_POST['comment']; 
NSDictionary* mainJSON = [NSDictionary dictionaryWithObjectsAndKeys:
                          _txname.text,@"name",
                          _txemail.text,@"email",
                          _txcontect.text,@"phone",
                          _txcomment.text,@"comment",
                          nil];

NSError * err;
NSData * jsonData = [NSJSONSerialization dataWithJSONObject:mainJSON options:NSJSONWritingPrettyPrinted error:&err];
NSString * prams = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
NSString *urlString=@"Your URL here...";
prams = [prams stringByReplacingOccurrencesOfString:@"+" withString:@"%2B"];

NSURL *url = [NSURL URLWithString:urlString];
NSMutableURLRequest * urlRequest = [NSMutableURLRequest requestWithURL:url];
NSData *requestData = [prams dataUsingEncoding:NSUTF8StringEncoding];

if (prams.length>0) {
    [urlRequest setHTTPMethod:@"POST"];
    [urlRequest setValue:@"application/json" forHTTPHeaderField:@"Accept"];
    [urlRequest setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
    [urlRequest setValue:[NSString stringWithFormat:@"%lu", (unsigned long)[requestData length]] forHTTPHeaderField:@"Content-Length"];
    [urlRequest setHTTPBody: requestData];
}

NSURLSessionDataTask * dataTask = [[NSURLSession sharedSession] dataTaskWithRequest:urlRequest completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
    //NSLog(@"data=%@",data);

    if (data.length>0 && error==nil) {
        NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:0 error:NULL];
        NSLog(@"Dict=%@",dict);


    }
}];
[dataTask resume];