9

ユーザーがフィールド (タイトル、説明、都市) にデータを入力し、[保存] をクリックした後に実行される次のコードを試しています。

- (IBAction)saveDataAction:(id)sender {
    NSString *lEventTitle = [NSString stringWithFormat:@"var=%@",eventTitle.text];
    NSString *lEventDesc = [NSString stringWithFormat:@"var=%@",eventDescription.text];
    NSString *lEventCity = [NSString stringWithFormat:@"var=%@",eventCity.text];

    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://mydomain.com/ios/form.php"]];
    [request setHTTPMethod:@"POST"];
    [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"content-type"];
    //this is hard coded based on your suggested values, obviously you'd probably need to make this more dynamic based on your application's specific data to send

    NSString *postString = [[NSString alloc] initWithFormat:@"title=%@&description=%@&city=%@", lEventTitle, lEventDesc, lEventCity];
    NSString *clearPost = [postString
                                     stringByReplacingOccurrencesOfString:@"var=" withString:@""];
    NSLog(@"%@", clearPost);
    [request setHTTPBody:[clearPost dataUsingEncoding:NSUTF8StringEncoding]];
    [request setValue:clearPost forHTTPHeaderField:@"Content-Length"];
    [NSURLConnection connectionWithRequest:request delegate:self];
    NSLog(@"%@", request);
}

clearPost 文字列 (サーバーに送信される変数を含む文字列) を NSLog すると、次のように表示されます。

title=xmas&description=Celebration&city=NY

PHP側では、次のように簡単です:

<?php
 $title = $_POST['title'];
 $description = $_POST['description'];
 $city = $_POST['city'];

 echo "Title: ". $title;
 echo "Description: ". $description;
 echo "City: ". $city;
?>

後で操作できるように、PHPでデータを取得しようとしています。今すぐ実行すると

domain.com/ios/form.php で、タイトル、説明、都市が空であることを示しています。

申し訳ありませんが、私は iOS と Objective-C に非常に慣れていません。

4

3 に答える 3

15

オブジェクティブ C

// Create your request string with parameter name as defined in PHP file
NSString *myRequestString = [NSString stringWithFormat:@"title=%@&description=%@&city=%@",eventTitle.text,eventDescription.text,eventCity.text];

// Create Data from request
NSData *myRequestData = [NSData dataWithBytes: [myRequestString UTF8String] length: [myRequestString length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL: [NSURL URLWithString: @"http://www.youardomain.com/phpfilename.php"]];
// set Request Type
[request setHTTPMethod: @"POST"];
// Set content-type
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"content-type"];
// Set Request Body
[request setHTTPBody: myRequestData];
// Now send a request and get Response
NSData *returnData = [NSURLConnection sendSynchronousRequest: request returningResponse: nil error: nil];
// Log Response
NSString *response = [[NSString alloc] initWithBytes:[returnData bytes] length:[returnData length] encoding:NSUTF8StringEncoding];
NSLog(@"%@",response);

迅速

// Create your request string with parameter name as defined in PHP file
var myRequestString: String = "title=\(eventTitle.text!)&description=\(eventDescription.text!)&city=\(eventCity.text!)"
// Create Data from request
var myRequestData: NSData = NSData.dataWithBytes(myRequestString.UTF8String(), length: myRequestString.characters.count)
var request: NSMutableURLRequest = NSMutableURLRequest(URL: NSURL(string: "http://www.youardomain.com/phpfilename.php")!)
// set Request Type
request.HTTPMethod = "POST"
// Set content-type
request.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "content-type")
// Set Request Body
request.HTTPBody = myRequestData
// Now send a request and get Response
var returnData: NSData = NSURLConnection.sendSynchronousRequest(request, returningResponse: nil, error: nil)
// Log Response
var response: String = String(bytes: returnData.bytes(), length: returnData.characters.count, encoding: NSUTF8StringEncoding)
NSLog("%@", response)
于 2013-03-23T17:16:51.887 に答える
0

これを試してください:

[request setValue:[NSString stringWithFormat:@"%d",[clearPost length]]  forHTTPHeaderField:@"Content-Length"];

わかりました、リクエスト コードを追加します: 参考になるかもしれません

   NSMutableURLRequest *request = [[NSMutableURLRequest alloc]
                                        initWithURL:[NSURL
                                                     URLWithString:@"http://www.sth.com/action/verify.php"]];

        [request setHTTPMethod:@"POST"];
        [request setValue:[NSString stringWithFormat:@"%i",[sent length]]  forHTTPHeaderField:@"Content-length"];
    [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
        [request setHTTPBody:[sent dataUsingEncoding:NSASCIIStringEncoding]];
于 2013-03-23T16:44:48.223 に答える