我必须在将数据发送到我开发的iOS应用程序中的mySQL表之前验证所有字段。 (不能有空字段)。
我写的代码如下。
发生的情况是:当我在运行应用程序时按下SEND按钮时,它首先发送数据,然后显示对话框,通知该字段不能为空。我不明白为什么!有人可以帮助我吗?
- (IBAction)send:(id)sender {
if( ([dia.text isEqualToString:@""]) || ([nome.text isEqualToString:@""]) || ([local.text isEqualToString:@""]) || ([endereco.text isEqualToString:@""]) || ([horario.text isEqualToString:@""]) || ([informacoes.text isEqualToString:@""]) || ([telefone.text isEqualToString:@""]))
{
UIAlertView *ErrorAlert = [[UIAlertView alloc] initWithTitle:@"Erro!!"
message:@"Preencha todos os campos!" delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil, nil];
[ErrorAlert show];
}
NSString *post = [NSString stringWithFormat:@"dia=%@&nome=%@&local=%@&endereco=%@&horario=%@&informacoes=%@&telefone=%@", dia.text, nome.text, local.text, endereco.text, horario.text, informacoes.text, telefone.text];
const char *urlUTF8 = [post UTF8String];
NSString *postBody = [NSString stringWithUTF8String:urlUTF8];
NSData *postData = [postBody dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postDataLength = [NSString stringWithFormat:@"%lu", (unsigned long)[postData length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:[NSString stringWithFormat:@"https://url_goes_here"]]];
[request setHTTPMethod:@"POST"];
[request setValue:postDataLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];
NSURLResponse *response;
NSError *err;
NSData *returnData = [ NSURLConnection sendSynchronousRequest: request returningResponse:&response error:&err];
NSString *content = [NSString stringWithUTF8String:[returnData bytes]];
NSLog(@"****** RESPOSTA: %@", content);
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Anuncio enviado"
message:@"Agora seu anuncio será submetido a moderação, e se aprovado será publicado."
delegate:self
cancelButtonTitle:@"Ok"
otherButtonTitles:nil];
[alert show];
}