hi i am doing login page in my app.i get error in console and invalid message. help me to fined result.thanks in advance. my Xcode is here
- (IBAction)login:(id)sender {
NSInteger success = 0;
@try {
if([[self.username text] isEqualToString:@""] || [[self.password text] isEqualToString:@""] ) {
[self alertStatus:@"Please enter Email and Password" :@"Sign in Failed!" :0];
} else {
NSString *post =[[NSString alloc] initWithFormat:@"username=%@&password=%@",[self.username text],[self.password text]];
NSLog(@"PostData: %@",post);
NSURL *url=[NSURL URLWithString:@"http://192.168.1.5:81/priya/login.php"];
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%lu", (unsigned long)[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 setHTTPBody:postData];
//[NSURLRequest setAllowsAnyHTTPSCertificate:YES forHost:[url host]];
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 ==> %@", responseData);
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 :@"Sign in Failed!" :0];
}
} else {
//if (error) NSLog(@"Error: %@", error);
[self alertStatus:@"Connection Failed" :@"Sign in Failed!" :0];
}
}
}
@catch (NSException * e) {
NSLog(@"Exception: %@", e);
[self alertStatus:@"Sign in Failed." :@"Error!" :0];
}
if (success) {
[self performSegueWithIdentifier:@"login_success" sender:self];
}}
and my php coding
<?php
$host = "localhost";
$user = "xcode";
$pass = "xcode";
$db="xcode";
$r = mysql_connect($host, $user, $pass);
if (!$r) {
echo "Could not connect to server\n";
trigger_error(mysql_error(), E_USER_ERROR);
} else {
echo "Connection established\n";
}
echo mysql_get_server_info() . "\n";
$r2 = mysql_select_db($db);
if (!$r2) {
echo "Cannot select database\n";
trigger_error(mysql_error(), E_USER_ERROR);
} else {
echo "database selected\n";
}
$u=$_POST['username'];
$pw=$_POST['password'];
$result=mysql_query("select username,password from login where username='$u' and password='$pw'");
$row=mysql_fetch_array($result);
if($row["username"]==$u && $row["password"]==$pw)
{
echo '{"success":1}';
}
else
{
'{"success":0,"error_message":"Username and/or password is invalid."}';
}
mysql_close();
?>
and i got result in console
2014-02-22 13:44:48.172 school apps[1605:a0b] PostData: username=admin&password=xcode
2014-02-22 13:44:49.252 school apps[1605:a0b] Response code: 200
2014-02-22 13:44:49.252 school apps[1605:a0b] Response ==> Connection established
5.5.34
database selected
{"success":1}
2014-02-22 13:44:49.253 school apps[1605:a0b] Success: 0
if any tutorial is there means give me url.i am very new to Xcode.
You need to convert the mySQL database output to json format.
//set the mime type
$document->setMimeEncoding('application/json');
echo json_encode($yourdata);
Once converted, you need
jexit();
to exclude the html in the source code.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments