2017-04-21 124 views
1

這幾乎是我在我的應用程序的登錄頁面上工作了將近一天,我想向我的應用程序顯示我的xCode中的錯誤(或任何PHP回聲)應用程序。我會告訴你我的PHP文件和我的Xcode如何從PHP返回Swift UIAlertController

<?php 
if($_SERVER['REQUEST_METHOD']=='POST') 
{ 

$password = $_POST['password']; 
$email = $_POST['email']; 

if($password == '' || $email == '') 
{ 
    echo 'Please fill all values.'; 
} 
else 
{ 
    require_once('GBconnect.php'); 
    $sql = "SELECT * FROM Users WHERE email='$email' AND password='$password' OR mobile_no='$email' AND password='$password'"; 
    $check = mysqli_fetch_array(mysqli_query($connection,$sql)); 

     if(isset($check)) 
     { 
      echo 'Login Success'; 
     } 
     else 
     { 
      echo 'Email/Phone or Password is wrong.'; 
     } 
     mysqli_close($connection); 
} 
} 
else 
{ 
    echo 'error'; 
} 

這是我的雨燕文件:

@IBAction func signUp(_ sender: Any) 
{ 
    let request = NSMutableURLRequest(url: NSURL(string: "http://34.205.37.201/restapi/GBlogin3.php")! as URL) 
    request.httpMethod = "POST" 

    let logEmail = "email=\(username.text!)&& password=\(password.text!)" 
    let logMobile = "mobile_no=\(username.text!)&& password=\(password.text!)" 

    if (username.text?.isEmpty)! || (password.text?.isEmpty)! 
    { 
     //display message 
     LoginInfoMyAlertMessage(userMessage: "Please input your Email or Phone and Password."); 
    } 
    if let checkNum = Int(username.text!) 
    { 
     print(checkNum) 
     request.httpBody = logMobile.data(using: String.Encoding.utf8) 
    } 

    let task = URLSession.shared.dataTask(with: request as URLRequest) 
    { 
     data, response, error in 

     if error != nil 
     { 
      print("error=\(String(describing: error))") 
     } 

     print("response = \(String(describing: response))") 
     let responseString = NSString(data: data!, encoding: String.Encoding.utf8.rawValue) 
     print("responseString = \(String(describing: responseString))") 
    } 
    task.resume() 

    username.text = "" 
    password.text = "" 
    return 
+1

你現在看到了什麼結果?你還應該看看https://github.com/Alamofire/Alamofire,這是一個非常好的庫,用於處理Swift – dmorrow

+0

'$ password = $ _POST ['password'];' - >然後我們看不到加密的網絡請求?在PHP方面,你應該不**保存純文本密碼,而是使用PHP內置的'password_hash/password_verify' – OldPadawan

回答

0

一對夫婦的事情,我看到:

  1. 您指定logEmail但從來沒有使用它任何地方
  2. 您在logMobile中有一個空格,但是您不應該在使用application/x-www-form-urlencoded POST數據時
  3. 在相關項目中,您應該使用比連接字符串更穩健的表單編碼。
  4. 您應該使用HTTP狀態碼來指示成功或失敗,而不是字符串。使用HTTP 200獲得成功,HTTP 401獲取憑證,HTTP 403獲取無效憑證

這些都說明了,您沒有指定在運行代碼時看到的內容。如果你能做到這一點,我們可以提供更具體的建議。使用POSTMAN來驗證你的服務器端工作正常,那麼你可以確保你的客戶端正在使用服務器。

0

您可以對響應進行編碼,然後在源應用程序中解包以處理不同的消息。

<?php 
$password = $_POST['password']; 
$email = $_POST['email']; 

if($password != '' || $email != ''){ 
$check = false; //Your connection 

if($check){ 
    echo json_encode([ 
     "Message" => "Login Success", 
     "Status" => "Ok" 
    ]); 
} 
else{ 
    echo json_encode([ 
     "Message" => "Email/Phone or Password is wrong.", 
     "Status" => "Error" 
    ]); 
} 
} 
?> 

然後

@IBAction func signup(_ sender: Any) { 
    let request = NSMutableURLRequest(url: NSURL(string: "http://localhost:8888/chava/login.php")! as URL) 
    request.httpMethod = "POST" 

    let logEmail = "email=\(emial.text!) && password=\(password.text!)" 
    print(logEmail) 
    if (emial.text?.isEmpty)! || (password.text?.isEmpty)!{ 
     print("Please input your Email or Phone and Password."); 
    } 
    request.httpBody = logEmail.data(using: String.Encoding.utf8) 
    let task = URLSession.shared.dataTask(with: request as URLRequest){ 
     data, response, error in 

     if error != nil{ 
      print("error=\(String(describing: error))") 
     }else{ 
      //print(String(data:data!,encoding: .utf8) ?? "") 
      if let resp = data { 
       do{ 
        let jsonResult = try JSONSerialization.jsonObject(with: resp) as! [String:AnyObject] 
        if let message = jsonResult["Message"]{ 
         print(message) 
        } 
       }catch{ 

       } 
      } 
     } 

     //print("response = \(String(describing: response))") 
     //let responseString = NSString(data: data!, encoding: String.Encoding.utf8.rawValue) 
     //print("responseString = \(String(describing: responseString))") 
    } 

我希望幫助你!