我尝试添加一个方法来处理异常,但程序只是崩溃而不是弹出一个 AlertView。
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)我添加方法来接收数据并将其转换为字符串:
- (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 的任何想法?