0

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];
Jaume
  • 913
  • 2
  • 17
  • 31

2 Answers2

1

Use this

  • (void)parser:(NSXMLParser *)parser parseErrorOccurred:(NSError *)parseError {

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]; }

krishnendra
  • 673
  • 7
  • 8
0

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.

Community
  • 1
  • 1
Byte
  • 2,920
  • 3
  • 33
  • 55