当可以访问webservice时,代码工作正常,但是当它无法访问时,应用程序崩溃。为什么控件没有进入@catch ??
-(void)callRegisterWebService:(NSString *)fname lastName:(NSString *)lName email:(NSString *)email
{
[self.view endEditing:YES];
UIActivityIndicatorView *activity = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
[self.view addSubview:activity];
activity.center = self.view.center;
[self.view bringSubviewToFront:activity];
activity.hidesWhenStopped = YES;
[activity setHidden:NO];
[activity startAnimating];
NSOperationQueue *q = [NSOperationQueue mainQueue];
NSString *serviceURL = [NSString stringWithFormat:@"https://aagaka];
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:serviceURL]];
@try
{
[NSURLConnection sendAsynchronousRequest:request queue:q completionHandler:^(NSURLResponse *serviceResponse, NSData *responseData, NSError *err) {
NSMutableDictionary *parsedData = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableContainers error:&err];
[activity stopAnimating];
if(!parsedData)
{
NSLog(@"data not parsed");
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"ERROR" message:@"Problem in Network. Please Try Again!" delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];
[alert show];
}
else
{
NSLog(@"parsed");
NSLog(@"parsed.. the size is %lu", (unsigned long)[parsedData count]);
NSLog(@"%@", parsedData);
NSString *status = [parsedData objectForKey:@"Status"];
NSLog(@"the status is %@", status);
if([status isEqualToString:@"Success"])
{
NSString *uniqueNumber = [parsedData objectForKey:@"UniqueNum"];
[self saveEmailAndUniqueNumberToDatabase:fname lastName:lName Email:email Number:uniqueNumber];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:NULL message:@"Authentication Token Has Been Sent To Your Email-ID!" delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];
[alert show];
[self.verifyView setHidden:NO];
[self.registerView setHidden:YES];
[self.verifyButtonTab setEnabled:YES];
[self.verifyButtonTab setSelected:YES];
[self.registerButtonTab setSelected:NO];
}
else if([status isEqualToString:@"Failed"])
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Not An Authorized User" message:@"Please Contact Admin To Get Access" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil];
[alert show];
[self.verifyView setHidden:YES];
[self.registerView setHidden:NO];
[self.verifyButtonTab setEnabled:NO];
[self.registerButtonTab setSelected:YES];
}
else
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"ERROR" message:@"Problem in Network. Please Try Again!" delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];
[alert show];
[self.verifyView setHidden:YES];
[self.registerView setHidden:NO];
[self.verifyButtonTab setEnabled:NO];
[self.registerButtonTab setSelected:YES];
}
}
}];
}
@catch (NSException *exception)
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:NULL message:@"Problem In Network Connection. Please Try Again!" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
[self.verifyView setHidden:YES];
[self.registerView setHidden:NO];
[self.verifyButtonTab setEnabled:NO];
[self.registerButtonTab setSelected:YES];
}
@finally {
}
}
答案 0 :(得分:2)
如果te阻塞发生错误,则try catch将不会被调用,因为块在不同的上下文中执行。
在completionHandler:
的块中移动try try catch,它应该可以工作。