繁体   English   中英

从Swift 3 iOS致电Php

[英]Call Php from Swift 3 iOS

您好我已经在新版本的swift 3中更新了旧的swift应用程序,代码已通过在后期传递值并返回json消息而连接到php页面,因为我已将应用程序更新为swift 3从以下代码对我进行编码错误,如何解决这些错误?

错误:

在此处输入图片说明

SWIFT代码:

let URL_SAVE_TEAM = "http://localhost/ios-login.php"
    var email:String = "";
    var password:String = "";


    func PrintValue(){

       // print(username);
        //print(password);
    }

    func Login() -> Bool{


        //created NSURL
        let requestURL = NSURL(string: URL_SAVE_TEAM)

        //creating NSMutableURLRequest
        let request = NSMutableURLRequest(URL: requestURL)

        //setting the method to post
        request.HTTPMethod = "POST"

        //getting values from text fields


        //creating the post parameter by concatenating the keys and values from text field
        let postParameters = "email="+email+"&password="+password;

        //adding the parameters to request body
        request.HTTPBody = postParameters.dataUsingEncoding(NSUTF8StringEncoding)


        //creating a task to send the post request
        let task = NSURLSession.sharedSession().dataTaskWithRequest(request){
            data, response, error in

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

            //parsing the response
            do {
                //converting resonse to NSDictionary
                let myJSON =  try NSJSONSerialization.JSONObjectWithData(data!, options: .MutableContainers) as? NSDictionary

                //parsing the json
                if let parseJSON = myJSON {

                    //creating a string
                    var msg : String!

                    //getting the json response
                    msg = parseJSON["message"] as! String?

                    //printing the response
                    print(msg)



                }
            } catch {
                print(error)
            }

        }
        //executing the task
        task.resume()

        return false;
    }

错误的Xcode图片:

在此处输入图片说明

PHP代码:

<?php

    header('Content-Type: application/json');
    $email= $_POST['email'];
    $password = $_POST['password'];
    $ris='Ti rispondo dal server zio';

   echo json_encode($ris);
  //  echo "prova";

?>

我会更迅速地写:)

func Login() -> Bool{

//created URL
guard let requestURL = URL(string: URL_SAVE_TEAM) else { return false }

//creating URLRequest
var request = URLRequest(url: requestURL)

//setting the method to post
request.httpMethod = "POST"

//getting values from text fields


//creating the post parameter by concatenating the keys and values from text field
let postParameters = "email=\(email)&password=\(password)"

//adding the parameters to request body
request.httpBody = postParameters.data(using: .utf8)

//creating a task to send the post request
let session = URLSession.shared

let task = session.dataTask(with: request) {
  data, response, error in

  guard error == nil else {
    print("error is \(error!.localizedDescription)")
    return
  }

  guard let data = data else {
    print("No data was returned by the request!")
    return
  }

  //parsing the response
  do {
    //converting resonse to NSDictionary
    let myJSON =  try JSONSerialization.jsonObject(with: data, options: .allowFragments) as? Dictionary<String, String?>

    //parsing the json
    guard let parseJSON = myJSON, let msg = parseJSON["message"] as? String else {
      print("Error parsing data")
      return
    }

    //printing the response
    print(msg)

  } catch {
    print(error)
  }

}

//executing the task
task.resume()

return false

}

您可能会考虑在函数中添加完成处理程序以处理登录成功!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM