Please Wait AlertView while talking to server
How can I show an AlertView while the application communicates to the server via HTTP Post? My current code that I have doesn't work correctly.
Basically, an AlertView comes up asking to confirm something. If confirmed, t开发者_如何学运维he following code takes place:
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{//this is for when the person clicks OK
NSString *title = [alertView buttonTitleAtIndex:buttonIndex];
if([title isEqualToString:@"OK"])
{
//do alert
UIAlertView *waitAlert = [[UIAlertView alloc] initWithTitle:@"Communicating with Server..."
message:nil
delegate:nil
cancelButtonTitle:nil
otherButtonTitles:nil];
UIActivityIndicatorView *loading = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(125, 50, 30, 30)];
loading.activityIndicatorViewStyle = UIActivityIndicatorViewStyleWhiteLarge;
[waitAlert addSubview:loading];
[loading startAnimating];
[waitAlert show];
//communicate with Server
RetailTransaction *retailTransaction = [[RetailTransaction alloc]init];
ServerConnection *serverConnection = [[ServerConnection alloc]init];
NSData *serverResponse = [serverConnection postData:[retailTransaction xmlToXml:
note.text:
pin.text:
total.text]];
NSString *serverResponseString = [[NSString alloc] initWithData:serverResponse
encoding:NSUTF8StringEncoding];
NSLog(@"Server Responded with: %@", serverResponseString);
There's more code to this method, but this gets the basic stuff across. The waitAlert
doesn't even come up until after the application has communicated with the server, at which point it only comes up for a split second. Does anyone know why this is happening? It should come up BEFORE communicating with the server. Thanks in advance.
Move this part to another function and run that function in a background thread.
//communicate with Server
RetailTransaction *retailTransaction = [[RetailTransaction alloc]init];
ServerConnection *serverConnection = [[ServerConnection alloc]init];
NSData *serverResponse = [serverConnection postData:[retailTransaction xmlToXml:
note.text:
pin.text:
total.text]];
NSString *serverResponseString = [[NSString alloc] initWithData:serverResponse
encoding:NSUTF8StringEncoding];
NSLog(@"Server Responded with: %@", serverResponseString);
....
The reason why the alertview does not show up before the call is that the main thread gets hung.
It's very simple using ASIHTTPRequest
-(void)sendMessage{
NSString *url = @"http://www.your-server.com";
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:url]];
[request setDelegate:self];
[request startAsynchronous];
[alertView show];
}
-(void)requestFinished:(ASIHTTPRequest *)request{
[alertView hide];
}
-(void)requestFailed:(ASIHTTPRequest *)request{
NSLog(@"%@", [[request error] description]);
}
In this case, you'd call [self sendMessage] from where you launch your alert, but I simplified it a bit. The key is to do the request asynchronously.
This example doesn't do a POST, but ASIHTTPRequest is cabable of that too.
精彩评论