@IBAction func signUpPressed(_ sender: Any) { if nameTextField.text == "" { self.showToast(message: "Please enter your name") return } else if emailTextField.text == "" { self.showToast(message: "Please enter your email") return } else if passwordTextField.text == "" { self.showToast(message: "Please enter password") return } let parameters: [String: Any] = [ "name" : nameTextField.text ?? "", "email" : emailTextField.text ?? "", "password" : passwordTextField.text ?? "", "fcmToken" : "asfnasdbhj", "osType" : "iPhone" ] AppDelegate.signUpUrl = "https://vital-server.herokuapp.com/signup" SVProgressHUD.show() AF.request(AppDelegate.signUpUrl , method: .post, parameters: parameters, encoding: JSONEncoding.default) .responseDecodable(of: RegistrationAPI.self) { (response) in print("resp : ", response) switch response.result { case .success(let response): print("Success :: ", response.message) if response.message == "Password is invalid" { self.showToast(message: "Please enter a valid password") } else if response.message == "Account already registered with this email" { self.showToast(message: response.message) } else { print("Get token and continue") UserDefaults.standard.set(response.token, forKey: "token") } break case .failure(let error): // Handle error print("Failure : ", error) } SVProgressHUD.dismiss() } } class RegistrationAPI: Codable { var message: String = "" var token: String = "" private enum CodingKeys: String, CodingKey { case token case message } required init(from decoder: Decoder) throws { let container = try decoder.container(keyedBy: CodingKeys.self) if let token = try container.decodeIfPresent(String.self, forKey: .token) { self.token = token } if let message = try container.decodeIfPresent(String.self, forKey: .message) { self.message = message } } }
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter