簡體   English   中英

如何從PHP到Swift UIAlertController的回聲

[英]How get the echo from PHP to Swift UIAlertController

這幾乎是我工作在我的應用程序的登錄頁面上的一天,我想向我的應用程序顯示錯誤(或者來自PHP的回聲)到我的xCode應用程序。 我將向您展示我的PHP文件和我的xCode

<?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';
}

這是我的Swift文件:

@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. 您指定logEmail但從logEmail任何地方使用它
  2. 您在logMobile有空格,但在使用application/x-www-form-urlencoded POST數據時不應該這樣做
  3. 在相關項中,您應該使用比連接字符串更健壯的表單編碼。
  4. 您應該使用HTTP狀態代碼來指示成功或失敗,而不是字符串。 使用HTTP 200獲取成功,使用HTTP 401獲取憑據,使用HTTP 403獲取無效憑據

盡管如此,您還沒有指定運行代碼時所看到的內容。 如果你能做到這一點,我們可以提供更具體的建議。 使用POSTMAN驗證服務器端是否正常工作,然后您可以確保您的客戶端正在使用服務器。

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

<?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))")
    }

希望對你有所幫助!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM