使用POST在Web服务器IOS应用程序中存储用户信息

时间:2016-02-20 20:40:20

标签: php ios json xcode swift

我正在使用Swift + xcode7 + php脚本+ mysql服务器端(Bluehost)构建一个IOS应用程序它实际上是我使用xcode的第一个项目

当我尝试将用户信息存储到数据库时,没有发生任何事情,也没有出现错误消息,也没有添加任何行。

这是我的代码,我试图检查它,但似乎没有任何错误。

// send user data to a server side



let myUrl = NSURL(string: "http://thetogo.net/userRegister.php");
let request = NSMutableURLRequest(URL:myUrl!);
request.HTTPMethod = "POST";

let postString = "email=\(userEmail)&password=\(userPassword)";
request.HTTPBody = postString.dataUsingEncoding(NSUTF8StringEncoding);




        let task = NSURLSession.sharedSession().dataTaskWithRequest(request) {
            data,response, error in

            if error != nil {
                print("error=\(error)")
                return
            }

            do {

            let json = try NSJSONSerialization.JSONObjectWithData(data!, options: .MutableContainers) as? NSDictionary

            if let parseJSON = json{
                let resultValue = parseJSON["status"] as? String
                print("result: \(resultValue)")

                var isUserRegistered:Bool = false;
                if(resultValue=="Success") { isUserRegistered = true; }

                var messageToDisplay:String = parseJSON["message"] as! String!;
                if(!isUserRegistered){
                    messageToDisplay = parseJSON["message"] as! String!;
                }

                dispatch_async(dispatch_get_main_queue(), {
                    //DisplayAlertMessage with confirmation
                    let myAlert = UIAlertController(title: "Alert", message:messageToDisplay, preferredStyle: UIAlertControllerStyle.Alert);

                    let okAction = UIAlertAction(title: "Ok", style:UIAlertActionStyle.Default){ action in
                        self.dismissViewControllerAnimated(true, completion: nil);
                    }
                    myAlert.addAction(okAction);
                    self.presentViewController(myAlert, animated:true, completion: nil);
                });



                }} catch {
                    print(error)
            }
         }

这是我的PHP代码

<?php 


require("Conn.php");
require("MySQLDao.php");

$email = htmlentities($_POST["email"]);
$password = htmlentities($_POST["password"]);

$returnValue = array();

if(empty($email) || empty($password))
{
$returnValue["status"] = "error";
$returnValue["message"] = "Missing required field";
echo json_encode($returnValue);
return;
}

$dao = new MySQLDao();
$dao->openConnection();
$userDetails = $dao->getUserDetails($email);

if(!empty($userDetails))
{
$returnValue["status"] = "error";
$returnValue["message"] = "User already exists";
echo json_encode($returnValue);
return;
}

$secure_password = md5($password); // I do this, so that user password cannot be read even by me

$result = $dao->registerUser($email,$secure_password);

if($result)
{
$returnValue["status"] = "Success";
$returnValue["message"] = "User is registered";
echo json_encode($returnValue);
return;
}

$dao->closeConnection();

?>

1 个答案:

答案 0 :(得分:0)

  

1。)在PHP代码中写下以下代码来检查错误:

error_reporting(E_ALL | E_STRICT);
ini_set('display_errors', 'on');
  

2。)使用$ _GET http方法直接在浏览器上点击url。

     

执行此操作后,您将看到您面临的错误。解决后   错误只需将$ _GET替换为$ _POST