繁体   English   中英

如何将“聊天消息”从iOS TextField发送到远程数据库

[英]How To Send “chat message” From iOS TextField To A Remote Database

因此,我尝试创建一个简单的iOS程序,在其中可以将聊天消息保存到远程MySQL服务器,然后将其加载回。

我的应用程序具有可在其中输入数据的文本框和一个将数据保存到外部数据库的按钮。

我的php文件如下所示:

<?php

$DB_HostName = "hostname";
$DB_Name = "dbname";
$DB_User = "dbuser";
$DB_Pass = "dbpass";
$DB_Table = "dbtable";

if (isset ($_GET["name"]))
    $name = $_GET["name"];
else
    //$name = "Ghalia";
    echo ('Please enter a name');

$con = mysql_connect($DB_HostName,$DB_User,$DB_Pass) or die(mysql_error()); 
mysql_select_db($DB_Name,$con) or die(mysql_error()); 

$sql = "insert into $DB_Table (name) values('$name');";
$res = mysql_query($sql,$con) or die(mysql_error());

mysql_close($con);
if ($res) {
    echo "success";
}else{
    echo "faild";
}
?>

我的ViewController.h文件如下所示:

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController
@synthesize txtName;

- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

- (IBAction)insert:(id)sender {
//create string contains url address for php file, the file name is phpfile.php, it receives a     parameter :name

NSString *strURL = [NSString stringWithFormat:@"http://xxdsadxx.net/phpFile.php?name=%@", txtName.text];

//to execute php code
NSData *dataURL = [NSData dataWithContentsOfURL:[NSURL URLWithString:strURL]];
//to receive the returned value
NSString *strResult = [[NSString alloc] initWithData:dataURL encoding:NSUTF8StringEncoding];

NSLog(@"%@", strResult);

}
@end

现在,我已将其编码为仅存储名称。

我想存储一个聊天消息,例如“你好,你好吗?”,因此该字符串无法正确存储到外部服务器。 知道我该如何实现吗? 谢谢。

您不应使用dataWithContentsOfURL。 那是阻塞电话。 它将冻结UI,直到网络请求完成。 您想使用NSURLConnection进行HTTP获取。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM