Exception/Error handling in Objective-C (iPhone app)

Exception/Error handling in Objective-C (iPhone app)

I actually have two questions regarding exception/error handling in the iPhone app that I am making:

  1. The app uses Internet, but when there's no connection, the app just dies (during launch). How can I handle this to print some infomsg to the user, instead of just getting thrown back to the springboard?

  2. Can someone show me an example of how to handle for instance a "page not found" or "no contact with server" error, so I can give some sort of info to the user in the same way as above?

indentationLevel property doesn't appear to do anything?


How do I programatically pull lists/arrays of (itunes urls to) apps in the iphone app store?
For crashes, the first step is to use error messages and the debugger to figure out what call is causing the problem. iTune-style apps using C#If the problem is caused by an uncaught exception, read this Apple article on exception handling. how to remove subviews from scrollview?The specific answer really depends on your code and exactly what is causing the crash, so I won't speculate around a particular solution. iPhone PHP integration. does using try catch block in xcode show error on real device? As far as detecting server error response codes (such as 404), that's more specific to WebKit. for- loop in background thread accesses each index more than once — iPhone devI assume you're using UIWebView on iPhone, and you've probably noticed this none of the primary methods return errors. Update coordinate property of MKAnnotation on parent viewcontroller after Touch End event?This is for the reason this it uses a delegate model to report progress or errors asynchronously. (It makes sense for the reason this you don't want your UI code to be at the mercy of a slow-loading (or non-existent) webpage. To be notified of such errors, there are a few steps..
  1. Adopt the UIWebViewDelegate protocol, usually in the same class this will start the webpage load for convenience.
  2. Set this object as the delegate of the UIWebView instance. (It has a delegate property, so you must use any thing like either uiView.delegate = self or [uiView setDelegate:self] based on what you prefer.)
  3. Implement the webView:didFailLoadWithError: method in this class. (You must be notified when the load finishing by implementing webViewDidFinishLoad: as well.) This is where you include the logic of what should appear when an error occurs.
I didn't see any detailed documentation on the content of any particular errors handed back via this delegate method, although it's a standard NSError object, and I recommend checking out the contents by calling its methods, such as -localizedDescription and -userInfo.. Here is any sample code with #import statements excluded for brevity.. MyClass.h.
@interface MyClass : NSObject <UIWebViewDelegate> {   IBOutlet UIWebView* myWebView; } -(void)webView:(UIWebView*)webView didFailLoadWithError:(NSError *)error; @end 
@implementation MyClass - (id) init {   if ((self = [super init]) == nil)     return nil;   // initialize myWebView   myWebView.delegate = self;   return self; }  - (void) webView:(UIWebView*)webView didFailLoadWithError:(NSError*)error {   ... } @end 


Testing for a connection is pretty easy....
NSString * test = [NSString stringWithContentsOfURL:[NSURL URLWithString:@"http://www.stackoverflow.com"]]; if (test == nil) {   //display an alertview saying you don't have an internet connection } 


Using a URL to test for a connection is not a good idea, it is not robust enough to determine if the internet connection is down, the website is down or any another netjob issue etc and above all it adds an overhead to the call as far as netjob traffic.. Look at the Reachability demo on the Apple site, it uses the correct way to determine connectivity, including whether you are on wifi etc..

98 out of 100 based on 43 user ratings 358 reviews