使用NSURLConnection从php/mysql请求xcode数据

使用NSURLConnection从php/mysql请求xcode数据,php,mysql,xcode,nsurlconnection,Php,Mysql,Xcode,Nsurlconnection,我想使用NSURLConnection从PHP/Mysql请求中获取一个值(可以是1或0),下面是我的xcode和PHP/Mysql查询代码 有人能帮我将收到的数据存储在NSString而不是NSMutableData中吗? 我之所以想使用NSURLConnection,是因为它提供了无连接功能 NSURLRequest *theRequest=[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://192.168.0.20/iqkr

我想使用NSURLConnection从PHP/Mysql请求中获取一个值(可以是1或0),下面是我的xcode和PHP/Mysql查询代码

有人能帮我将收到的数据存储在NSString而不是NSMutableData中吗? 我之所以想使用NSURLConnection,是因为它提供了无连接功能

NSURLRequest *theRequest=[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://192.168.0.20/iqkradio_stream_ip_flag.php"]
                                          cachePolicy:NSURLRequestUseProtocolCachePolicy
                                      timeoutInterval:20.0];
NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
if (theConnection) {
    receivedData = [[NSMutableData data] retain];
    NSString *receivedDataString = [[NSString alloc] initWithData:receivedData encoding:NSUTF8StringEncoding];
}

else
{
    NSLog(@"no connection or Error");
    // Inform the user that the connection failed.
}
Mysql文件

 <?php
$con = mysql_connect("192.168.0.15","xxxx","xxxxx");

if (!$con) {
  die('Could not connect: ' . mysql_error());
}

mysql_select_db("qkradio", $con);
$result = mysql_query("select address from iqkradio" );
while($row = mysql_fetch_array($result)) {
echo $row['address'];
}
mysql_close($con);
?>

如果是单个字符,为什么不使用:

NSError *error;
NSString *result = [NSString stringWithContentsOfURL:[NSURL URLWithString:@"http://192.168.0.20/iqkradio_stream_ip_flag.php"] encoding:NSASCIIStringEncoding error:&error];

它比NSURLConnection要容易得多,并且不应该用这么小的负载阻塞太多。如果您担心总是可以将其包装在
dispatch\u async

中,我知道,但是当无法访问url时,我的代码会阻塞60秒。任何其他捕获阻塞的解决方案都应该在调用之前检查URL的可访问性。这是苹果公司的要求之一。如果可达性为false,则不进行调用。使用以下代码:。它将允许您测试连接是否可行。