部队等待阿拉莫菲尔的回应,然后迅速返回4



我遇到了一种情况,我必须在注册和登录时通过OTP代码检查提供的手机号码。我在Accountscalss中设置了所有内容,以便在SignUpViewControllerSignInViewController上重用它们。

ViewControllers中,我想先发送OTP,然后检查它是否是从服务器响应发送来推送VerificationViewController的,我使用Alamofire进行联网,使用SwiftyJSON解析JSON响应。现在让我们深入挖掘我的问题的核心:

这是我的SignUpViewController级的一部分

var acc: Accounts = Accounts()
@IBAction func signupAction(_ sender: Any) {
if validateFields() {
let fname = self.firstName.text!
let lname = self.lastName.text!
let pnum = self.phoneNumber.text!
self.acc.setFirstName(fn: fname)
self.acc.setLastName(ln: lname)
self.acc.setPhoneNumber(pn: pnum)
let result = self.acc.sendOTP()
if (result) {
let targetVC = UIStoryboard.init(name: "Main", bundle: Bundle.main).instantiateViewController(withIdentifier: "signupVerfication") as? SignupVerficationViewController
self.navigationController?.pushViewController(targetVC!, animated: true)
}
else {
CustomComponents().alertWithTitle(title: "Error", message: "Unable to Verify Phone Number,Please Try again!", ViewController: self)
}
}
}

这是我的Accounts类的一部分

func sendOTP() -> Bool {
let conf = APIConfig()
var returnedResult = false
conf.setURL(url: "patient/auth/send-totp")
conf.setHeader(head: ["Content-Type": "Application/json"])
conf.setBody(body: ["phone_number": self.phoneNumber])
print(self.phoneNumber)
Alamofire.request(conf.getURL(), method: .post, parameters: conf.getBody(), encoding: JSONEncoding.default, headers: conf.getHeader()).validate(statusCode: 200..<600).responseData { response in
let status = response.response?.statusCode
print(status!)
let swiftJSONVar = JSON(response.result.value!)
print(swiftJSONVar)
if (status == 200) {
returnedResult = true
}
}
return returnedResult
}

无论何时运行应用程序,我都会将警报作为输出,尽管正如您所看到的,我通过在此处打印print(status!)和此处打印print(swiftJSONVar)来调试响应。打印的输出是200作为状态代码,我得到了其余的响应。

我在网上看了很多教程,比如Swift 4的Grand Central Dispatch教程和Swift 4中的Secure Coding With Concurrency等等,但无法在我的代码块上实现GCD。

我需要你的帮助吗?

sendOTP方法的返回类型为Bool,当Alamofire请求为asynchronous时会立即返回,并且您在顶部设置returnedResult值为false,该值会立即返回而无需等待响应。在Alamofire发出响应后,returnedResult被设置为true,但无法通知调用者。当sendOTP方法返回false时,它显示错误警报。

使用闭包而不是从sendOTP返回bool,后者接受bool并返回void

func sendOTP(_ completion: @escaping (Bool) -> Void) {
let conf = APIConfig()
var returnedResult = false
conf.setURL(url: "patient/auth/send-totp")
conf.setHeader(head: ["Content-Type": "Application/json"])
conf.setBody(body: ["phone_number": self.phoneNumber])
print(self.phoneNumber)
Alamofire.request(conf.getURL(), method: .post, parameters: conf.getBody(), encoding: JSONEncoding.default, headers: conf.getHeader()).validate(statusCode: 200..<600).responseData { response in
let status = response.response?.statusCode
print(status!)
let swiftJSONVar = JSON(response.result.value!)
print(swiftJSONVar)
if (status == 200) {
completion(true)
} else {
completion(false)
}
}
}

现在你可以像一样打电话了

self.acc.sendOTP { (result) in
if result {
//show vc
let targetVC = UIStoryboard.init(name: "Main", bundle: Bundle.main).instantiateViewController(withIdentifier: "signupVerfication") as? SignupVerficationViewController
self.navigationController?.pushViewController(targetVC!, animated: true)
} else {
//show error alert
CustomComponents().alertWithTitle(title: "Error", message: "Unable to Verify Phone Number,Please Try again!", ViewController: self)
}
}

除了GCD教程,我还推荐URLSession和Alamofire教程,以了解网络在iOS中的工作原理。

相关内容

  • 没有找到相关文章

最新更新