Я пытаюсь добавить метод обработки исключения, но программа просто сработает, а не всплывает AlertView.Вопрос об обработке исключений HTTP на iPhone
1) настроить соединение:
-(void)connect:(NSString *)strURL
{
NSURLRequest *theRequest=[NSURLRequest requestWithURL:[NSURL URLWithString:strURL]
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:60.0];
NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
if (theConnection)
{
// receivedData is declared as a method instance elsewhere
receivedData = [[NSMutableData data] retain];
}
else
{
// inform the user that the download could not be made
}
}
2) Я добавить метод для приема данных и CONVER его в строку:
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
// append the new data to the receivedData
// receivedData is declared as a method instance elsewhere
[receivedData appendData:data];
ReturnStr = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
}
3) Я добавить исключение метод ручки:
-(void) connection:(NSURLConnection *)connection didFailWithError: (NSError *)error {
UIAlertView *errorAlert = [[UIAlertView alloc]
initWithTitle: [error localizedDescription]
message: [error localizedFailureReason]
delegate:self
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[errorAlert show];
}
После того, как я изменил strURL на неправильный URL-адрес, программа просто сработает. Любые идеи, почему AlertView не появляется?
Вы пробовали работает по программе с отладчиком? Вызывается ли ваш 'didFailWithError'? Какая ошибка? –
Нет, метод 'didFailWithError' не вызывался. Я использую неправильный URL для подключения, поэтому я не понимаю, почему он не был вызван. –