2017-04-21 125 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))") 
    } 

我希望帮助你!