Search code examples
mysqlxcodeios4textfield

Xcode: Easiest Way To Send Data From iOS TextField For Example To A Remote Database


I currently have an online web hosted MySQL database with HostGator.com that currently stores user sign ups for a service of mine. Currently, the only way to store information in that database is from my online form on a website.

My goal is to replicate that form on an iOS app by using textfields etc. What I need help figuring out is, how can I take the data input from the user on the iOS app, and send that information to the MySQL database. From what I found, there is no way to go from iOS straight to MySQL, so I would need to use PHP as well. However, how do I get that data from the iOS app to PHP so that I can send the data from PHP to the MySQL database? As well as, how I can I do the vice versa, meaning, if I wanted, how can I send a message back from PHP to the iOS app?

I have heard people suggest SQLite, but that appears to be a local database on the iOS which I do not need. I do not want to store anything locally on the app.

Thanks


Solution

  • You can just gather the data from a native form, then use a NSURLRequest / NSURLConnection to send the data to your php server page.

    //Example form with one php variable only. Use get URL argument notation to add more args.
    NSString *rawStr = [NSString stringWithFormat:@"var=%@",textBox.text];
    NSData *data = [rawStr dataUsingEncoding:NSUTF8StringEncoding];
    
    NSURL *url = [NSURL URLWithString:@"http://myurl.com/script.php"];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    
    [request setHTTPMethod:@"POST"];
    [request setHTTPBody:data];
    
    NSURLResponse *response;
    NSError *err;
    NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&err];
    NSLog(@"responseData: %@", responseData);