由于没有连接,如何检测didFailWithError?

时间:2012-08-24 16:39:42

标签: objective-c ios restkit

有什么方法可以告诉我调用我的Restkit didFailWithError函数的原因是缺少与服务器的连接吗?

-(void) objectLoader:(RKObjectLoader *)objectLoader didFailWithError:(NSError *)error{

//What should I do here to know the server could not be reached?

}

2 个答案:

答案 0 :(得分:1)

在我使用此代码的NSURLConnection didFailWithError方法中,它可能适用于RESTKit,但我不确定。我想我会发布这个,所以你至少可以检查(这可能有帮助):)

if (error)
{
    NSLog(@"%@", [NSString stringWithFormat:@"Connection failed! Error code: %d - %@ %@", error.code, error.localizedDescription, [error.userInfo objectForKey:NSURLErrorFailingURLStringErrorKey]]);

    if (error.code == -1009)
    {
        // This is the case that a connection failed based on bad connectivity
    }
}        

如果您还需要其他信息,请告诉我们。)

答案 1 :(得分:0)

您应该可以查看NSError class reference

您将在哪里找到以下相关方法:

-(NSUInteger)code;

//A string containing the localized description of the error.
-(NSString *)localizedDescription;

因此,您将从您收到的NSError检查此方法的返回值。