Xcode:从 iOS TextField 发送数据到远程数据库的最简单方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9509419/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 23:29:13  来源:igfitidea点击:

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

mysqlxcodeios4textfield

提问by John

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.

我目前在 HostGator.com 上有一个在线 Web 托管的 MySQL 数据库,该数据库当前存储用户注册我的服务。目前,在该数据库中存储信息的唯一方法是从我在网站上的在线表单。

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?

我的目标是通过使用文本字段等在 iOS 应用程序上复制该表单。我需要帮助弄清楚的是,如何在 iOS 应用程序上获取用户输入的数据,并将该信息发送到 MySQL 数据库。根据我的发现,无法从 iOS 直接转到 MySQL,因此我还需要使用 PHP。但是,如何将数据从 iOS 应用程序获取到 PHP,以便我可以将数据从 PHP 发送到 MySQL 数据库?以及,我如何反之亦然,这意味着,如果我愿意,如何将消息从 PHP 发送回 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.

我听说有人建议使用 SQLite,但这似乎是我不需要的 iOS 本地数据库。我不想在应用程序上本地存储任何内容。

Thanks

谢谢

回答by futureelite7

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

您可以只从本机表单收集数据,然后使用 NSURLRequest / NSURLConnection 将数据发送到您的 php 服务器页面。

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