xcode处理无连接时的xml解析堆栈
我正在从 url 解析 xml 文件数据。当设备有互联网连接时,一切都很好,但当设备没有互联网连接时,它会被堆叠(在模拟器上)或关闭(在真实设备上)。 initWithContentsOfURL 时已入栈。如何处理错误?谢谢。
NSURL *url = [[NSURL alloc] initWithString:@"MY URL Containing .xml file"];
NSError *error = nil;
NSString *stringData = [[NSString alloc] initWithContentsOfURL:url encoding:NSUTF8StringEncoding error:&error];
NSData *responseData = [stringData dataUsingEncoding:NSUTF8StringEncoding];
I am parsing xml file data from an url. All works great when device has internet connection but when not it is been stacked (on simulator) or closed (on real device). It is been stacked when initWithContentsOfURL. How to handle the error? Thank you.
NSURL *url = [[NSURL alloc] initWithString:@"MY URL Containing .xml file"];
NSError *error = nil;
NSString *stringData = [[NSString alloc] initWithContentsOfURL:url encoding:NSUTF8StringEncoding error:&error];
NSData *responseData = [stringData dataUsingEncoding:NSUTF8StringEncoding];
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用这个
{
NSString *string = [NSString stringWithFormat:@"无法下载数据: 错误代码: %@",[parseError code]];
UIAlertView *alert =[[UIAlertView alloc]initWithTitle:@"无法下载" message:string delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil];
[警报显示];
}
Use this
{
NSString *string = [NSString stringWithFormat:@"Unable to download data : Error code : %@",[parseError code]];
UIAlertView *alert =[[UIAlertView alloc]initWithTitle:@"Unable to download" message:string delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
}
我相信您正在寻找的关键词是“可达性”。在 iOS(或几乎任何东西)中,您不想假设互联网可用。可达性允许您检查互联网是否可供您使用。这是另一篇文章中的示例,可以解决您的问题。
iOS 4可达性指南
有关更多详细信息,请在几乎任何地方搜索关键字“reachability” 。
I believe the keyword you are looking for is "Reachability". In iOS (or pretty much anything), you do not want to make assumption that internet will be available. Reachability allows you to check whether the internet is available to you. Here is an example from another post that would solve your problem.
Reachability Guide for iOS 4
For more detail, search the keyword "reachability" pretty much anywhere.