2016-06-10 14 views
0

Ich versuche, flink zu versuchen und zu lernen, aber einen Back-End-Server für PHP wollen und müssen die PHP konvertieren, um die Appgleiche Antwort auf Login Check PHP und SWIFT Werfen aus

Jetzt JSon, was ich will, ist zu setzen E-Mail und Passwort in den Feldern auf App und es senden eine Post-Anfrage an den PHP-Server-Seite und der PHP validiert die Details und sendet die Antwort, die mich dann entscheiden, was mit der Antwort zu tun.

Ich habe es geschafft, auf der Serverseite mit Erfolg zu posten, aber wenn ich die Antwort zurück in den xcode bekomme, gibt es das gleiche Ausgabewetter, die Details sind korrekt oder inkorrekt, ich habe überall gesucht und Tutorials gefolgt aber ich bekomme immer das selbe Problem, ich vermute es ist die Art, wie ich Details in der PHP-Datei überprüfe, da ich immer den Fehler bekomme und nicht Erfolg.

Könnte jemand helfen?

login.php

$email = addslashes(strip_tags($_POST['email'])); 
$password = addslashes(strip_tags($_POST['password'])); 
$password = md5($password); 
$returnValue = array(); 

$sql = "SELECT email, user_password FROM `Accounts` WHERE `email` = '$email' AND user_password = '$password' LIMIT 1"; 
$fetchuser = mysqli_query($db_connect, $sql); 
$row = mysqli_num_rows($fetchuser); 

if($row = mysqli_num_rows($fetchuser) == 0){ 
    $returnValue["status"] = "error"; 
    $returnValue["message"] = "Account not found"; 
    echo json_encode($returnValue); 
} else { 
    $returnValue["status"] = "success"; 
    $returnValue["message"] = "Account found"; 
    echo json_encode($returnValue); 
} 

Swift Code

let userEmail = UserEmailTextField.text; 
    let userPassword = UserPasswordTextField.text; 

    if(userEmail!.isEmpty || userPassword!.isEmpty) { return; } 

    //SEND TO SERVER 
    let request = NSMutableURLRequest(URL: NSURL(string: "http://chaozsounds.com/chaozsounds/New-ChaozSounds/TeamChaozApp/register.php")!) 
    request.HTTPMethod = "POST" 
    let postString = "email=\(userEmail)&password=\(userPassword)" 
    request.HTTPBody = postString.dataUsingEncoding(NSUTF8StringEncoding) 
    let task = NSURLSession.sharedSession().dataTaskWithRequest(request) { data, response, error in 
     guard error == nil && data != nil else {               // check for fundamental networking error 
      print("error=\(error)") 
      return 
     } 

     if let httpStatus = response as? NSHTTPURLResponse where httpStatus.statusCode != 200 {   // check for http errors 
      print("statusCode should be 200, but is \(httpStatus.statusCode)") 
      print("response = \(response!)") 
     } 

     let returnValue = NSString(data: data!, encoding: NSUTF8StringEncoding) 
     print("returnValue = \(returnValue!)") 
     if(returnValue == "success") { 

      // LOGIN SUCCESSFUL 
      NSUserDefaults.standardUserDefaults().setBool(true, forKey:"isUserLoggedIn"); 
      NSUserDefaults.standardUserDefaults().synchronize(); 
      self.dismissViewControllerAnimated(true, completion: nil); 
     } 
    } 
    task.resume() 

Antwort

0

Ok hatte so um zu arbeiten, und ich es geschafft, zu tun, was ich war nach so Code gesetzt wird unter

// Send post request 

let request = NSMutableURLRequest(URL: NSURL(string: "urllinkhere")!); 
    request.HTTPMethod = "POST"; 
    let postString = "email=\(userEmail!)&password=\(userPassword!)"; 
    request.HTTPBody = postString.dataUsingEncoding(NSUTF8StringEncoding); 

    // Get success or error 

let task = NSURLSession.sharedSession().dataTaskWithRequest(request) { data, response, error in 
     if error != nil { 
      print("no data found: \(error)") 
      return 
     } 

     if let httpStatus = response as? NSHTTPURLResponse where httpStatus.statusCode != 200 {   // check for http errors 
      print("statusCode should be 200, but is \(httpStatus.statusCode)") 
      print("response = \(response!)") 
     } 

     // this, on the other hand, can quite easily fail if there's a server error, so you definitely 
     // want to wrap this in `do`-`try`-`catch`: 

     do { 
      let json = try NSJSONSerialization.JSONObjectWithData(data!, options: .MutableContainers) as? NSDictionary 
      if let parseJSON = json { 
       let returnValue = parseJSON["status"] as? String; 

       print("returnValue = \(returnValue!)") 

       if(returnValue == "success") { 

        // SUCCESS 
        NSUserDefaults.standardUserDefaults().setBool(true,forKey:"isUserLoggedIn"); 
        NSUserDefaults.standardUserDefaults().synchronize(); 

        self.dismissViewControllerAnimated(true, completion: nil); 

       } 


      } 
     } catch { 
      print(error) 
     } 
    } 

    task.resume()