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

末鹿安然 提交于 2019-11-30 20:17:03

问题


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


回答1:


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);


来源:https://stackoverflow.com/questions/9509419/xcode-easiest-way-to-send-data-from-ios-textfield-for-example-to-a-remote-datab

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!