Xcode:从iOS TextField向远程数据库发送数据的最简单方法 [英] Xcode: Easiest Way To Send Data From iOS TextField For Example To A Remote Database
问题描述
我目前在HostGator.com上有一个在线网络托管的MySQL数据库,该数据库目前存储用于我的服务的用户注册。目前,在该数据库中存储信息的唯一方法是从我在网站上的在线表单。
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.
我的目标是通过使用文本字段等在iOS应用程序上复制该表单。我需要帮助搞清楚的是,如何从用户那里获取数据输入iOS应用程序,并将该信息发送到MySQL数据库。从我发现的,没有办法从iOS直接到MySQL,所以我也需要使用PHP。但是,如何将这些数据从iOS应用程序获取到PHP,以便我可以将数据从PHP发送到MySQL数据库?除此之外,我怎么能这样做,反之亦然,这意味着,如果我想,我怎样才能将消息从PHP发送回iOS应用程序?
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?
我有听说有人推荐SQLite,但这似乎是我不需要的iOS上的本地数据库。我不想在应用程序上本地存储任何内容。
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.
谢谢
推荐答案
您只需从本机收集数据表单,然后使用NSURLRequest / NSURLConnection将数据发送到您的php服务器页面。
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);
这篇关于Xcode:从iOS TextField向远程数据库发送数据的最简单方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!