Untitled
unknown
swift
2 years ago
4.8 kB
2
Indexable
Never
@IBAction func registerUser(_ sender: Any) { if (firstNameField.text?.isEmpty)! || (lastNameField.text?.isEmpty)! || (emailField.text?.isEmpty)! || (passwordField.text?.isEmpty)! || (confirmPasswordField.text?.isEmpty)!{ let alert = UIAlertController(title: "Incomplete form", message: "The form is not complete", preferredStyle: .alert) alert.addAction(UIAlertAction(title: "Go back", style: .cancel, handler: nil)) present(alert, animated: true) } if ((confirmPasswordField.text?.elementsEqual(passwordField.text!)) != true) { let alert = UIAlertController(title: "Unmatched passwords", message: "The passwords do not match", preferredStyle: .alert) alert.addAction(UIAlertAction(title: "Go back", style: .cancel, handler: nil)) present(alert, animated: true) } let url = URL(string: "https://weaweg.mywire.org:8080/api/users/register") var request = URLRequest(url: url!) request.httpMethod = "PUT" // request.httpMethod = "POST" request.addValue("application/json", forHTTPHeaderField: "content-type") request.addValue("application/json", forHTTPHeaderField: "Accept") let postUser = [ "name" : firstNameField.text! + lastNameField.text!, "email" : emailField.text!, "password" : passwordField.text! ] as [String: String] do { request.httpBody = try JSONSerialization.data(withJSONObject: postUser, options: .prettyPrinted) } catch let error { print(error.localizedDescription) return } let task = URLSession.shared.dataTask(with: request) { (data: Data?, response: URLResponse?, error: Error?) in if error != nil { print("error=\(String(describing: error))") return } do { let json = try JSONSerialization.jsonObject(with: data!, options: .mutableContainers) as? NSDictionary if let parseJSON = json { let userId = parseJSON["userId"] as? String print("User id: \(String(describing: userId!))") if (userId?.isEmpty)! { self.displayMessage(userMessage: "Could not successfully perform this request. Please try again later") return } else { self.displayMessage(userMessage: "Successfully Registered a New Account. Please proceed to Sign in") } } else { self.displayMessage(userMessage: "Could not successfully perform this request. Please try again later") } } catch { self.displayMessage(userMessage: "Could not successfully perform this request. Please try again later") print(error) } } task.resume() ListsContainer.userBank.append(User.init(name: ((firstNameField.text)! + (lastNameField.text)!), password: passwordField.text, email: emailField.text)) if confirmPasswordField.text != passwordField.text { } } func removeActivityIndicator (activityIndicator: UIActivityIndicatorView) { DispatchQueue.main.async { activityIndicator.stopAnimating() activityIndicator.removeFromSuperview() } } 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 print("Ok button tapped") DispatchQueue.main.async { self.dismiss(animated: true, completion: nil) } } alertController.addAction(OKAction) self.present(alertController, animated: true, completion:nil) } }