2012-05-13 26 views
0

连接我用这个代码:用的NSURLRequest

NSString *recievedData; 

NSURLRequest *theRequest=[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.site.com/"] 
              cachePolicy:NSURLRequestUseProtocolCachePolicy 
             timeoutInterval:60.0]; 
// create the connection with the request 
// and start loading the data 
NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self]; 
if (theConnection) { 
    // Create the NSMutableData to hold the received data. 
    // receivedData is an instance variable declared elsewhere. 
    recievedData = [NSMutableData data]; 

    NSLog(@"%@", recievedData); 
} else { 
    // Inform the user that the connection failed. 
    NSLog(@"Unsuccessful."); 
} 

它是this修改后的版本。

我的问题是,它总是返回

<>

无论我是连接到互联网或没有,它总是转到成功。

回答

0

您还没有收到任何数据,您刚刚实例化了将保存接收数据的对象。您需要实现委托方法来处理响应和失败,通常最好异步使用NSURLConnection。

有一些示例代码Using NSURLConnection

0

NSURLConnection不能这样工作。您开始连接,然后在收到数据时收到回调。

如果您想要简单的调用来检索远程数据,请使用NSData的dataWithContentsOfURL方法。但是,您应该只在次要线程上使用它,否则它会在通话期间锁定您的用户界面,并且如果时间过长,系统可能会终止您的应用程序。

查看完整密码在NSURLConnection example