Objective c 正在连接NSURLRequest
我正在使用以下代码:Objective c 正在连接NSURLRequest,objective-c,nsurlrequest,Objective C,Nsurlrequest,我正在使用以下代码: NSString *recievedData; NSURLRequest *theRequest=[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.site.com/"] cachePolicy:NSURLRequestUseProtocolCachePolicy
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.");
}
这是一个修改版的
我的问题是它总是回来
无论我是否连接到互联网,它总是走向成功 NSURLConnection不是那样工作的。您启动连接,然后在收到数据时接收回调 如果希望通过简单调用检索远程数据,请使用NSData的dataWithContentsOfURL方法。但是,您应该只在辅助线程上使用它,因为否则它将在调用期间锁定您的用户界面,如果花费的时间太长,系统可能会终止您的应用程序
查看完整代码。您尚未收到任何数据,您只是实例化了将保存收到的数据的对象。您需要实现委托方法来处理响应和故障,通常最好异步使用NSURLConnection 这里有一些示例代码