views:

305

answers:

1

Trying to alert user when internet is unavailable (and retry when they dismiss message). The screen slightly dims (in preparation for alert), but the alert never displays.

Is the while loop interfering with the alert?

-(NSArray*)getResponse:(NSString*)page {
NSError *error;
NSURLResponse *response;
NSData *dataReply;
NSString *stringReply;
NSString *legalAddressURL;
NSArray *separatedData;
legalAddressURL = [NSString stringWithFormat:@"%@%@", SERVER, 
       [page stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding]];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL: [NSURL URLWithString: legalAddressURL]];
[request setHTTPMethod: @"GET"];
dataReply = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
while ([error code]){
 if (isNetAvailable){
  UIAlertView *alert = [[[UIAlertView alloc] initWithTitle:@"Internet Connection" 
   message:@"Server is down" delegate:self cancelButtonTitle:@"Try again" 
   otherButtonTitles:nil] autorelease];
  [alert show];
  dataReply = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
 } else {
  UIAlertView *alert = [[[UIAlertView alloc] initWithTitle:@"Internet Connection" 
   message:@"No access to net" delegate:self cancelButtonTitle:@"Try again" 
   otherButtonTitles:nil] autorelease];
  [alert show];
  dataReply = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
 }
}
stringReply = [[NSString alloc] initWithData:dataReply encoding:NSUTF8StringEncoding];
separatedData = [stringReply componentsSeparatedByCharactersInSet:
     [NSCharacterSet characterSetWithCharactersInString:@","]];
return separatedData;

}

+1  A: 

After showing the alert using [alert show], you should exit the getResponse method, because the internal message loop should continue.

You can't simply continue looping and showing alerts.

I could be wrong, but I'm pretty sure that's the reason.

Philippe Leybaert
Is there any way to reach the goal (alert the user in this method until problem fixed - so I don't violate DRY)? I rearranged the code to avoid multiple [alert show]s (added test if user has been alerted), but no dice.
BankStrong
Yes, pUIAlertView] isn't like MsgBox in Windows. You need to exit your current event handler and then it will call your callback method when the user selects an option.
U62