Alamofire异步通话 [英] Alamofire async call
问题描述
我有一个简单的问题。我正在连接到我的RESTful服务器并尝试连接。我确实得到了响应,并且一切正常,但是当我尝试调用函数来设置变量时,它只是没有调用它。
I have a simple problem. I'm connecting to my RESTful server and trying to connect. I do get a response and everything works perfect, however when I try to call a function to set a variable, it just doesn't call it.
代码:
//
// LoginClass.swift
// LoginApp
//
// Created by Tarek Adel on 12/3/16.
// Copyright © 2016 Tarek Adel. All rights reserved.
//
import Foundation
import Alamofire;
import UIKit;
class LoginClass {
var result: Bool = false;
var username: String
var password: String
init(username: String, password:String) {
self.username = username;
self.password = password;
}
func login() -> Void {
let data = [
"grant_type" : "password",
"username" : self.username,
"password" : self.password,
"client_secret":"xxxxxx",
"client_id":"xxxxxx",
"scope": ""
]
Alamofire.request("http://localhost:8000/oauth/token", method: .post, parameters: data)
.responseJSON { response in
//to get status code
if let status = response.response?.statusCode {
switch(status){
case 200:
self.loginSuccess();
default:
print("error with response status: \(status)")
}
}
//to get JSON return value
if let result = response.result.value {
let JSON = result as! NSDictionary
print(JSON)
}
}
}
func loginSuccess() {
self.result = true;
}
}
这是我检查 self.result
:
@IBAction func loginButton(_ sender: UIButton) {
let username = usernameTextField.text!;
let password = passwordTextField.text!;
let loginClass = LoginClass(username: username, password:password)
loginClass.login()
if(loginClass.result == true) {
resultLabel.text = "Correct!"
} else {
resultLabel.text = "Wrong Credentials"
}
}
推荐答案
您需要在 login
方法中使用完成处理程序,因为它正在异步调用,因此使用您的登录方法创建一个 completionHandler
,然后在该 completionHandler
中执行if条件。
You need to use completion handler with your login
method because it is making an Async call, so make one completionHandler
with your login method and than execute that if condition inside that completionHandler
.
func login(completionHandler: (_ result: Bool) -> ()) {
let data = [
"grant_type" : "password",
"username" : self.username,
"password" : self.password,
"client_secret":"xxxxxx",
"client_id":"xxxxxx",
"scope": ""
]
Alamofire.request("http://localhost:8000/oauth/token", method: .post, parameters: data)
.responseJSON { response in
//to get status code
if let status = response.response?.statusCode {
switch(status){
case 200:
//to get JSON return value
if let result = response.result.value {
let JSON = result as! NSDictionary
print(JSON)
}
completionHandler(true)
default:
print("error with response status: \(status)")
completionHandler(false)
}
}
}
}
现在像这样调用此登录方法。
Now call this login method like this.
self.login { (result) in
if(result)
{
resultLabel.text = "Correct!"
}
else{
resultLabel.text = "Wrong Credentials"
}
}
注意::如果您还想要JSON响应,则使用两个参数Bool和Dictionary创建完成处理程序,并同时传递JSON。
Note: If you want JSON response also then make completion handler with two parameter Bool and Dictionary and pass JSON also with that.
这篇关于Alamofire异步通话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!