如何在iPhone上HTTP连接失败时弹出警报?

时间:2009-03-03 07:23:22

标签: iphone http exception exception-handling connection

我想编写一些代码来处理HTTP连接失败时的异常。我使用以下代码:

-(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");
}

但是当连接失败时程序崩溃了。如何在没有程序崩溃的情况下弹出警报?

1 个答案:

答案 0 :(得分:3)

您的代码没有明显错误,您需要提供更多信息。

确保您拥有breakpoint on objc_exception_throw,然后在调试器下运行该程序。然后,您可以确定抛出异常的行。

一个疯狂的猜测,但可能[error localizedDescription][error localizedFailureReason]正在返回nil