我目前正在从外部 API 读取一些 XML。
以下代码工作正常:
NSError *error = nil;
NSString *xmlString = [[NSString alloc] initWithContentsOfURL:url encoding:NSUTF8StringEncoding error:&error];
但是,我想添加一些错误处理(“无法访问服务器”、“没有 Internet 连接”等)。因此,我在 Reachability 示例代码的帮助下放置了这个块:
if ([error code] == kCFURLErrorNotConnectedToInternet) {
NSDictionary *userInfo = [NSDictionary dictionaryWithObject:NSLocalizedString(@"No Internet connection",@"Error message displayed when not connected to the Internet.") forKey:NSLocalizedDescriptionKey];
NSError *noConnectionError = [NSError errorWithDomain:NSCocoaErrorDomain code:kCFURLErrorNotConnectedToInternet userInfo:userInfo];
[self handleError:noConnectionError];
} else {
// otherwise handle the error generically
[self handleError:error];
}
handleError 方法只是显示带有错误详细信息的 UIAlertView。当我在没有连接的情况下运行应用程序时,我希望看到我的错误消息。但是,我得到的只是“无法完成操作(Cocoa 错误 256)”,据我所知,这是一个通用的读取错误。
起初我以为它可能只是 TouchXML 的方法,但如您所见,我将其更改为 NSString 的 initWithCONntentsOfURL。
任何人都可以阐明这个问题吗?
谢谢,
富有的