使用Alamofire验证API是否生成错误

时间:2018-08-22 20:40:00

标签: swift api alamofire

我正在用Swift 4做一个登录系统。 在我的API中,如果未执行登录,则会带来error = true(在Xcode中显示 error = 1 )。 我需要执行if来检查它是否给出了错误。

API代码:

<?php

//pulls the functions of api
require_once '../DbOperation.php';

$db = new DbOperation();

$reponse = array("error" => false );

if(isset($_GET['email']) && isset($_GET['senha'])){

    $email = $_GET['email'];
    $senha = $_GET['senha'];

    $user = $db->Login($email,$senha);
    if ($user != false){

        $reponse['error'] = false;
        $reponse['unicId'] = $user["unicId"];
         if ($user["ativo"] == 'sim' ){

            $reponse['ativo'] = true;
        } else {
            $reponse['ativo'] = false;
        }
        $reponse['message'] = "login feito com sucesso";
        echo json_encode($reponse);


    }else{
        $reponse['error'] = true;
        $reponse["message"] = "email ou senha invalido";
        echo json_encode($reponse);
    }
}else{
    echo "coloque paramentros" ;
}
?>

快捷代码

import Alamofire

@IBAction func btnEntrar_Action(_ sender: Any) {
    if (txtEmail.text?.isEmpty)! || (txtSenha.text?.isEmpty)!{
        ToastView.shared.long(self.view, txt_msg: "Campos não Inseridos Corretamente!")
    }
    else{
        let url = "APILINK"

        let parameters : Parameters=[
            "email"         : txtEmail.text!,
            "senha"         : txtSenha.text!]

        Alamofire.request(url, method: .get, parameters: parameters).responseJSON { response in
            print(response)

             // check for errors
            if response.result.error == nil{
                print("Not error")

            } else {
             print("Error!!")
             return
            }
        }
    }
}

我正在尝试使用response.result.error == nil,但是不起作用。

0 个答案:

没有答案