こんにちは、iOSとXcodeの初心者ですが、ログイン画面を作れば、ログイン画面は何も応答しません。Obj-cからPHPへPOSTするための変数はありません
- (IBAction)btnLogin:(id)sender {
NSInteger success = 0;
@try {
if([[self.insUsuario text] isEqualToString:@""] || [[self.insSenha text] isEqualToString:@""]) {
[self alertStatus:@"Insira o email e a senha!" :@"Erro" :0];
} else {
//ViewPontos *WP = [self.storyboard instantiateViewControllerWithIdentifier:@"ViewPontos"];
//WP.gUser = self.insUsuario.text;
NSString *post =[[NSString alloc] initWithFormat:@"usuario=%@&senha=%@",[self.insUsuario text],[self.insSenha text]];
NSLog(@"PostData: %@",post);
NSURL *url=[NSURL URLWithString:@"http://emporio56/apiapp/pegarPontos.php"];
//NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSData *postData = [post dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES];
NSLog(@"DATA %@", postData);
NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:url];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
//[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setValue:@"application/x-www-form-urlencoded charset=utf-8" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];
[request setHTTPShouldHandleCookies:YES];
NSLog(@"%@", request);
NSURLConnection *conn= [[NSURLConnection alloc]initWithRequest:request delegate:self];
if(conn)
{
NSError *error = [[NSError alloc] init];
NSHTTPURLResponse *response = nil;
NSData *urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
NSLog(@"Response code: %ld", (long)[response statusCode]);
if ([response statusCode] >= 200 && [response statusCode] < 300)
{
//NSString *responseData = [[NSString alloc]initWithData:urlData encoding:NSUTF8StringEncoding];
NSLog(@"Response ==> %@", response);
NSError *error = nil;
NSDictionary *jsonData = [NSJSONSerialization JSONObjectWithData:urlData options:NSJSONReadingMutableContainers error:&error];
success = [jsonData[@"success"] integerValue];
NSLog(@"Success: %ld",(long)success);
if(success == 1)
{
NSLog(@"Login SUCCESS");
} else {
NSString *error_msg = (NSString *) jsonData[@"error_message"];
[self alertStatus:error_msg :@"Erro" :0];
}
} else {
//if (error) NSLog(@"Error: %@", error);
[self alertStatus:@"Erro!" :@"Sign in Failed!" :0];
}
NSLog(@"Conectado");
}
else
{
NSLog(@"Nao conectou");
}
//[NSURLRequest setAllowsAnyHTTPSCertificate:YES forHost:[url host]];
}
}
@catch (NSException * e) {
NSLog(@"Exception: %@", e);
[self alertStatus:@"Sign in Failed." :@"Erro" :0];
}
if (success) {
[self performSegueWithIdentifier:@"login_success" sender:self];
}
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
// A response has been received, this is where we initialize the instance var you created
// so that we can append data to it in the didReceiveData method
// Furthermore, this method is called each time there is a redirect so reinitializing it
// also serves to clear it
_responseData = [[NSMutableData alloc] init];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
// Append the new data to the instance variable you declared
[_responseData appendData:data];
}
- (NSCachedURLResponse *)connection:(NSURLConnection *)connection
willCacheResponse:(NSCachedURLResponse*)cachedResponse {
// Return nil to indicate not necessary to store a cached response for this connection
return nil;
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
// The request is complete and data has been received
// You can parse the stuff in your instance variable now
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
// The request has failed for some reason!
// Check the error var
}
ユーザ名とパスワードをテストして置くためのコードを実行して、私はそのいつもresponse = (null)
サーバからの応答を得ることはありません後、誰かが私はそれを修正するために何ができるか知っていますか?私はあなたがウェブで見つけることができるコードの大部分を試して、それは人々が言う方法、同様に動作しませんでしたので、私はアンドロイドアプリケーションを接続するために同じPHPを使用していますが、 PHPのユーザ名とパスワードをPOSTし、dbに接続します。
が、私は
Alamofireを試してみませんか?新しい開発者として、あなたのために多くの機能を包むことができます – GIJOW
@GIJOWしかしAlamofireはSwiftコードにしかありませんか? –
AFNetworking – GIJOW