-1
我的swift應用程序有問題,應用程序必須與服務器上的php文件進行通信,我的問題是,上面的參數到達PHP文件的swift代碼是以下內容:可選(「[email protected]」),可選(「pippo」)。現在我的問題是,怎樣才能得到這些參數到PHP文件,而不字符可選( 「...」)從Swift APP解析php文件接收值時出錯Error
SWIFT CODE:
var ris="";
//created URL
guard let requestURL = URL(string: URL_SAVE_TEAM) else { return "no" }
//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)
ris=msg;
} catch {
print(error)
}
}
//executing the task
task.resume()
return ris;
PHP代碼:
<?php
include 'user.php';
header('Content-Type: application/json');
$email= $_POST['email'];
$password = $_POST['password'];
$ris['message']="";
$user = new User();
//procedo con il login
if($user->login($email,$password,"MOBILE")==true){
$ris['message']="yes";
}
else{
$ris['message']="no";
}
echo json_encode($ris);
?>
現在我嘗試用你的代碼 – riki
我想你代碼但沒有改變任何東西 – riki
它開始感謝 – riki