Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/22.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
在iOS中检索HTTPResponse/HTTPRequest状态代码?_Ios_Objective C_Nsurlconnection - Fatal编程技术网

在iOS中检索HTTPResponse/HTTPRequest状态代码?

在iOS中检索HTTPResponse/HTTPRequest状态代码?,ios,objective-c,nsurlconnection,Ios,Objective C,Nsurlconnection,我需要检查和评估iPhone应用程序中的HTTP状态代码。我有一个NSURLRequest对象和一个成功(我想)连接到站点的NSURLConnection: // create the request NSURLRequest *theRequest=[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.apple.com/"] cachePolicy:NSURLRequest

我需要检查和评估iPhone应用程序中的HTTP状态代码。我有一个NSURLRequest对象和一个成功(我想)连接到站点的NSURLConnection:

// create the request
NSURLRequest *theRequest=[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.apple.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 that will hold
    // the received data
    // receivedData is declared as a method instance elsewhere
    receivedData=[[NSMutableData data] retain];
} else {
    // inform the user that the download could not be made
}
我在这里得到了这个代码:

但它并没有(就我所见)说明任何关于访问HTTP状态码的内容

如何与站点建立连接,然后检查该连接的HTTP状态代码?

我就是这样做的:

    #pragma mark -
    #pragma mark NSURLConnection Delegate Methods
    - (void)connection:(NSURLConnection*)connection didReceiveResponse:(NSURLResponse*)response {
         NSHTTPURLResponse* httpResponse = (NSHTTPURLResponse*)response;
         int responseStatusCode = [httpResponse statusCode];
    }

但我使用的是异步NSURLConnection,所以这可能对您没有帮助。但我希望不管怎样都是这样

这确实完成了我所需要的。唯一的问题是,如果URL/IP地址不存在,则不会收到响应,因此应用程序将永远挂在那里。我目前正在寻找解决这个问题的方法。。。如果您有任何建议,我们将不胜感激……看看苹果公司的可达性代码。但仅使用通知方法(默认情况下代码中未启用,请参阅init方法中的注释),并始终设置要检查的主机。