我想編寫一些代碼來處理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");
}
但是,程序只是在連接失敗時崩潰。如何讓警報彈出而不會導致程序崩潰?
這是在主線程還是在後臺線程中運行?在後臺線程中操作UI *對象往往不起作用。除此之外,墜毀事件發生在哪條線上?使用調試器或移動NSLog線查找。 – 2009-03-03 12:22:09