使用Alamofire时,我在执行任务时遇到问题 我使用了两次Alamofire,第一次是收集数据(令牌),然后将其用于发送我的Post请求。
我的两个请求之间的问题是,在第二个请求之后完成了数据的恢复。
import Foundation
import Alamofire
import SwiftyJSON
class Helper {
func alomofireGet(URL: String) -> JSON {
let queue = DispatchQueue(label: "com.test.com", qos: .background, attributes: .concurrent)
var contenuJSON = JSON()
Alamofire.request(URL, method: .get).responseJSON(queue: queue) { (reponse) in
if reponse.result.isSuccess {
contenuJSON = JSON(reponse.result.value!)
print(contenuJSON)
}
else {
contenuJSON = JSON(reponse.result.error!)
}
}
return contenuJSON
}
func alomofirePost(URL: String, Paramaters: Dictionary<String, Any>) -> JSON {
var contenuJSON = JSON()
Alamofire.request(URL, method: .post, parameters: Paramaters, encoding: JSONEncoding.default).responseJSON { (reponse) in
if reponse.result.isSuccess {
contenuJSON = JSON(reponse.result.value!)
}
else {
contenuJSON = JSON(reponse.result.error!)
}
}
return contenuJSON
}
}
在新文件中=带有内容令牌的差异
let request = Helper()
@IBOutlet weak var emailText: UITextField!
@IBOutlet weak var passwordText: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
self.hideKeyboardWhenTappedAround()
}
@IBAction func login(_ sender: Any) {
let contenuJSON = request.alomofireGet(URL: "http://192.168.1.7/app_dev.php/login/app")
print(contenuJSON)
let token = contenuJSON["csrfToken"].stringValue
print(token) // /\ EMPTY
let Paramaters = ["_csrf_token": token, "_password": self.passwordText.text!, "_redirect_url": "", "t_path": "", "_username": self.emailText.text!]
let contenuRequest = request.alomofirePost(URL: "http://192.168.1.7/app_dev.php/login_check", Paramaters: Paramaters)
print(token) // /\ FULL /\
}
}
答案 0 :(得分:0)
对Alamofire的API调用是异步过程,因此您的alamofireGet
和alamofirePost
返回的只是初始化的json对象-JSON()
,其中没有任何数据。
解决方案:
您应该使用@escaping closure
,它将保留控件,直到您从第一次API调用获得结果为止。
func alomofireGet(URL: String, onCompletion:((JSON) -> Void)) {
let queue = DispatchQueue(label: "com.test.com", qos: .background, attributes: .concurrent)
var contentJSON = JSON()
Alamofire.request(URL, method: .get).responseJSON(queue: queue) { (reponse) in
// Load contentJSON with data
if reponse.result.isSuccess {
contenuJSON = JSON(reponse.result.value!)
} else {
contenuJSON = JSON(reponse.result.error!)
}
// Send contentJSON via `onCompletion` block
onCompletion(contenuJSON)
}
}
func alomofirePost(URL: String, Paramaters: Dictionary<String, Any>, onCompletion: @escaping ((_ response: JSON) -> Void)) {
var contenuJSON = JSON()
Alamofire.request(URL, method: .post, parameters: Paramaters, encoding: JSONEncoding.default).responseJSON { (reponse) in
// Load contentJSON with data
if reponse.result.isSuccess {
contenuJSON = JSON(reponse.result.value!)
} else {
contenuJSON = JSON(reponse.result.error!)
}
// Send contentJSON via `onCompletion` block
onCompletion(contenuJSON)
}
}
在视图控制器中将其命名为:
let usernameStr = self.emailText.text!
let passwordStr = self.passwordText.text!
Helper().alomofireGet(URL: "http://192.168.1.7/app_dev.php/login/app") { contenuJSON in
print(contenuJSON)
DispatchQueue.main.async {
let token = contenuJSON["csrfToken"].stringValue
print(token)
let Paramaters = ["_csrf_token": token, "_password": passwordStr, "_redirect_url": "", "t_path": "", "_username": usernameStr]
Helper().alomofirePost(URL: "http://192.168.1.7/app_dev.php/login_check", Paramaters: Paramaters) { contenuJSON in
print(token)
}
}
}