Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c 正在连接NSURLRequest_Objective C_Nsurlrequest - Fatal编程技术网

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

这里有一些示例代码