Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pop up alert when HTTP connection fails on iPhone?

I want to write some code to handle exceptions when HTTP connection fails. I use the following codes:

-(void) connection:(NSURLConnection *)connection
  didFailWithError: (NSError *)error {
    UIAlertView *errorAlert = [[UIAlertView alloc]
                    initWithTitle: [error localizedDescription]
                    message: [error localizedFailureReason]
                    delegate:nil
                    cancelButtonTitle:@"OK"
                    otherButtonTitles:nil];
    [errorAlert show];
    [errorAlert release];
    [activityIndicator stopAnimating];
    NSLog (@"Connection Failed with Error");
}

But the programme just crashes when the connection fails. How to let the alert pop up without program crash?

like image 544
Chilly Zhong Avatar asked Oct 26 '22 06:10

Chilly Zhong


1 Answers

Nothing is obviously wrong with your code, you will need to supply more information.

Make sure you have a breakpoint on objc_exception_throw and then run the program under the debugger. Then you can determine on what line the exception is thrown.

A wild guess, but perhaps [error localizedDescription] or [error localizedFailureReason] is returning nil.

like image 194
benzado Avatar answered Nov 03 '22 00:11

benzado