用户注册代码。当用户注册时,它应该转到新的视图控制器(在这种情况下为主页),如果用户名已经存在,则注册时应显示消息:已存在。
这是代码。没错。用户已正确注册,如果用户名已存在,则返回api的错误响应。只是事情是它没有按要求显示警报信息,并且如果注册则不会显示。显示“所有字段都是必填项”和“请确保密码匹配”,仅显示“已存在”。
import UIKit
import SwiftyJSON
import Alamofire
import SwiftKeychainWrapper
class RegisterUserViewController: UIViewController {
@IBOutlet weak var firstNameTextField: UITextField!
@IBOutlet weak var lastNameTextField: UITextField!
@IBOutlet weak var emailAddressTextField: UITextField!
@IBOutlet weak var passwordTextField: UITextField!
@IBOutlet weak var repeatPasswordTextField: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func cancelButtonTapped(_ sender: Any) {
print("Cancel button tapped")
self.dismiss(animated: true, completion: nil)
}
@IBAction func signupButtonTapped(_ sender: Any) {
print("Sign up button tapped")
// Validate required fields are not empty
if (firstNameTextField.text?.isEmpty)! ||
(lastNameTextField.text?.isEmpty)! ||
(emailAddressTextField.text?.isEmpty)! ||
(passwordTextField.text?.isEmpty)!
{
// Display Alert message and return
displayMessage(userMessage: "All fields are quired to fill in")
return
}
// Validate password
if ((passwordTextField.text?.elementsEqual(repeatPasswordTextField.text!))! != true)
{
// Display alert message and return
displayMessage(userMessage: "Please make sure that passwords match")
return
}
let params = [
"first_name" : firstNameTextField.text! as Any,
"last_name" : lastNameTextField.text! as Any,
"email_id" : emailAddressTextField.text! as Any,
"password" : passwordTextField.text! as Any,
]
Alamofire.SessionManager.default.request("http://horn.hostingduty.com/api/v1/app_adduser", method: .post, parameters: params, encoding: URLEncoding(destination: .methodDependent))
.validate(statusCode: [200, 201])
.responseJSON
{
[unowned self] (response) in
switch(response.result)
{
case .success:
guard let json = response.result.value as!
[String:Any]? else{ return}
print("Response \(json)")
if let data = json["data"] as! [String:Any]?
{
let email_id : String = self.emailAddressTextField.text!
let userdefault = UserDefaults.standard
userdefault.set(self.emailAddressTextField.text, forKey: "email_id")
print(email_id)
UserDefaults.standard.synchronize()
DispatchQueue.main.async
{
let homePage = self.storyboard?.instantiateViewController(withIdentifier: "HomePageViewController") as! SWRevealViewController
let appDelegate = UIApplication.shared.delegate
appDelegate?.window??.rootViewController = homePage
}
}
case .failure:
DispatchQueue.main.async
{
self.displayMessage(userMessage: "Already exists !! ")
return
}
}
}
}
func displayMessage(userMessage:String) -> Void {
DispatchQueue.main.async
{}
let alertController = UIAlertController(title: "Alert", message: userMessage, preferredStyle: .alert)
let OKAction = UIAlertAction(title: "OK", style: .default) { (action:UIAlertAction!) in
// Code in this block will trigger when OK button tapped.
print("Ok button tapped")
DispatchQueue.main.async
{
self.dismiss(animated: true, completion: nil)
}
}
alertController.addAction(OKAction)
self.present(alertController, animated: true, completion:nil)
}
}
Alamofire.SessionManager.default.request("http://horn.hostingduty.com/api/v1/app_adduser", method: .post, parameters: params, encoding: URLEncoding(destination: .methodDependent))
.validate(statusCode: [200, 201])
.responseJSON
{
[weak self] (response) in
guard let sSelf = self else { return }
switch(response.result)
{
case .success:
guard let json = response.result.value as!
[String:Any]? else{ return}
print("Response \(json)")
if let data = json["data"] as! [String:Any]?
{
let email_id : String = self.emailAddressTextField.text!
let userdefault = UserDefaults.standard
userdefault.set(self.emailAddressTextField.text, forKey: "email_id")
print(email_id)
UserDefaults.standard.synchronize()
DispatchQueue.main.async
{
let homePage = sSelf.instantiateViewController(withIdentifier: "HomePageViewController") as! SWRevealViewController
let appDelegate = UIApplication.shared.delegate
appDelegate?.window??.rootViewController = homePage
}
}
case .failure:
DispatchQueue.main.async
{
sSelf.displayMessage(userMessage: "Already exists !! ")
return
}
}
}
而不是无主的自我,对自我的弱引用,然后尝试在完成处理程序中获得它的强引用
我还想指出,api电话的失败也可能是因为其他原因。您不能总是假设故障是由于系统中已存在用户而引起的
所以尝试使用http响应状态代码,否则,
做出像成功一样的事情
{
"code" : "0001",
"msg" : "",
"your custom object key here" : {
}
}
并且失败了
{
"code" : "0000", // "0000" code can be "0002" if user already exist
"msg" : "reason for failure",
}
在成功的情况下,检查“代码”值