0

I am new to objective-c, I am doing the same app for android and iPhone.

When in Java I use a try-catch block to catch a network exception and handle it.

In Objective-C I am using:

[[NSXMLParser alloc] initWithContentsOfURL:url]

So, How can I handle a network exception when parsing that input stream, so if network goes down or there's any problem I can notify user and keep my app working?.

Clyde Lobo
  • 8,747
  • 6
  • 34
  • 58
Nanoc
  • 2,354
  • 1
  • 18
  • 32

3 Answers3

1

If you are using NSXMLParser then use its delgate method..

- (void)parser:(NSXMLParser *)parser parseErrorOccurred:(NSError *)parseError
{
NSLog(@"Error = %@", parseError);
}
Kalpesh
  • 5,268
  • 23
  • 41
  • This will get called only after the data download completes. Questions is about handling network errors. – Amar Sep 18 '13 at 09:14
  • if network failure at starting then this method should be called – Kalpesh Sep 18 '13 at 09:15
  • No, this method is called only if XML parser fails to parse the data. In case the [`initWithContentsOfURL` fails](https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSXMLParser_Class/Reference/Reference.html#//apple_ref/doc/uid/20001984-BAJCCBCH), the `NSXMLParser` instance is `nil`. This is not sufficient to know what network error occured. – Amar Sep 18 '13 at 09:21
  • this reports a fatal error to the delegate. The parser will stop parsing if network failure or exception occured. Before down voting pleas check – Kalpesh Sep 18 '13 at 09:21
  • I dont need to know the error, only if parsing finished ok or not, is this method called if network or server or anything goes wrong? – Nanoc Sep 18 '13 at 09:23
  • @Nanoc in apple documentation they said this method is called only if XML parser fails to parse the data – Kalpesh Sep 18 '13 at 09:24
  • 1
    @Kalpesh please edit you answer quoting the Apple documentation. And will you also be able to see the HTTP status code? – rckoenes Sep 18 '13 at 09:26
  • @Nanoc you can check if this method is called then your xml parsing is failed – Kalpesh Sep 18 '13 at 09:26
  • @rckoenes op wants only his parsing successfully or not thats why i am giving this answer . – Kalpesh Sep 18 '13 at 09:34
  • Error = Error Domain=NSXMLParserErrorDomain Code=5 "The operation couldn’t be completed. (NSXMLParserErrorDomain error 5.)" Perfect for me, i even dont need to know what error is. – Nanoc Sep 18 '13 at 09:53
1

It is never a good idea to use the initWithContentsOfURL: methods.

You are better of retrieving the the data via NSURLConnection or some library like AFNetworking. This will allow you to handle the HTTP status code and network error.

For example the NSURLConnectionDelegate has a call back method – connection:didFailWithError: which will be called in case of an error. You can then examen the error object to see what went wrong.

rckoenes
  • 68,081
  • 8
  • 130
  • 162
  • Seems a good way to do it, but i dont need to know html error code, only if parsing was succesful or not. – Nanoc Sep 18 '13 at 09:25
  • I have accepted Kalpesh answer for faster implementation, and better fit of my need, anyway i think your answer was a better way to handle socket errors. – Nanoc Sep 18 '13 at 09:50
0

Before you call the URL You can check the internet connectivity.

I suggest you to look the below link How to check for an active Internet connection on iOS or OSX?

And I suggest for parsing XML file use raptureXML Which is very simple. https://github.com/ZaBlanc/RaptureXML

Community
  • 1
  • 1
Deepak
  • 1,228
  • 3
  • 14
  • 23