2

I need some tutorial, how to use HTTP POST (or GET) request from my iOS app. I want to send one string to my server and than write it to database. I've found this piece of code:

NSString *post = @"key1=val1";
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];

NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];

NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL URLWithString:@"http://www.nowhere.com/sendFormHere.php"]];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];

I add it to UIAction button, but it don't send anything to my server. On my server, I have a PHP script that takes that "key1" from post and than it write it to db.

<?
    $postr = $_POST["key1"];
    $con0 = mysql_connect("server","db","pass");
    mysql_select_db("table", $con0);
    mysql_set_charset('utf8',$con0);
    mysql_query("INSERT INTO tok (token) VALUES ('$postr')");
    mysql_close();
?>

Can anybody tell me what I am doing wrong?

stepik21
  • 2,610
  • 3
  • 22
  • 32
  • No, of course I want to send it from iOS APP... Just need to know what is wrong – stepik21 Apr 24 '13 at 18:32
  • Then write what you meant. Xcode and your iOS app are two different things. The question as it stands has absolutely nothing to do with Xcode. –  Apr 24 '13 at 18:34
  • Why? I need code to xcode... But ok.. So can you now help me? :) – stepik21 Apr 24 '13 at 18:39
  • No, do you honestly think you'd write something else depending on the IDE? Of course not. See, I've been making iOS apps for 3 years, and I've been using Linux in the first two and a half years. No Xcode there, only a text editor and "make". I still barely use Xcode. –  Apr 24 '13 at 18:41
  • BTW, that `allowLossyCompression:YES` looks nasty. Also, your server-side script is vulnerable to SQL injection attacks. –  Apr 24 '13 at 18:44
  • But xcode is THE IDE for iOS app :) – Sebastian May 31 '20 at 10:55

2 Answers2

2

AFNetworking is a very powerful library that can help you to reduce the effort on create your HTTP requests, you can find several examples on how to use this library on its github page.

You may find this answer useful for your needs: AFNetworking Post Request

Community
  • 1
  • 1
ararog
  • 1,092
  • 10
  • 18
2

Look at AFnetworking and work through their api. Sending a post request using the api is very fast, and is what most apps use for web connectivity.

Also please dont be discouraged by sarcastic comments. You should tag iOS projects as iOS and not xcode (unless you actually need help with the actual program xcode). But I don't think it's productive to harrass everyone that comes in and makes this mistake. A more tactful way would be a personal message as opposed to a public retort

https://github.com/AFNetworking/AFNetworking

William Falcon
  • 9,813
  • 14
  • 67
  • 110