2009-03-03 99 views
5

我想编写一些代码来处理HTTP连接失败时的异常。我使用下面的代码:如何在iPhone上的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"); 
} 

但是,程序只是在连接失败时崩溃。如何让警报弹出而不会导致程序崩溃?

+0

这是在主线程还是在后台线程中运行?在后台线程中操作UI *对象往往不起作用。除此之外,坠毁事件发生在哪条线上?使用调试器或移动NSLog线查找。 – 2009-03-03 12:22:09

回答

3

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

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

一个疯狂的猜测,但也许[error localizedDescription][error localizedFailureReason]返回nil

+0

当我使用错误的URL时,didFailWithError方法甚至没有被调用。任何想法为什么发生这种情况 – 2009-03-04 02:21:20