I am adding an Agreement Module in my app, where in the user will tap the checkbox when he/she agrees. When the user tapped the checkbox it will return a true value and will enable the proceed button. Upon tapping the proceed button it will activate the user's account. The problem is, it returns a status code 400 and it does not activate the user's account. I can't figure it out which part of my code has an error or does the error comes from the API side.

I tried to check it in PostMan using the same parameters I used in my APIService.swift file it returns status code 200 and it activates the user's account.

APIService.swift

func createUser(userRole: UserRole, credential: String, email: String, secureText: String, termsConditions: Bool, completion: @escaping validateTaskCompletion) {

guard let createUserURL = URL(string: CREATE_USER_URL) else {
    completion(nil, .InvalidURL)
    return
}

let firstParameterTitle = "firstCredential"
let secondParameterTitle = "userType"
let thirdParameterTitle = "email"
let fourthParameterTitle = "password"
let fifthParameterTitle = "hasAgreedToTermsConditions"

let requestBody: Parameters = [

    firstParameterTitle: credential,
    secondParameterTitle: userRole.rawValue,
    thirdParameterTitle: email,
    fourthParameterTitle: secureText,
    fifthParameterTitle: termsConditions
]

Alamofire.request(createUserURL, method: .post, parameters: requestBody).responseJSON(completionHandler: { (response) in

    guard reachability(responseResult: response.result) else {
        completion(nil, .NoNetwork)
        return
    }

    guard let statusCode = response.response?.statusCode else {
        completion(nil, .NoStatusCode)
        return
    }

    switch(statusCode) {
    case 200:
        completion(true, nil)
    case 400:
        completion(nil, .BadRequest)
    case 404:
        completion(nil, .InvalidCredentials)
    case 409:
        completion(nil, .AlreadyActivated)
    default:
        completion(nil, .UncapturedStatusCode)
        print("**createUser** Uncaptured Status Code: \(statusCode)")
    }

})

TermsAndConditionVC.swift

var userRole: UserRole!
var firstCredential: String!
var emailString: String!
var confirmPasswordString: String!
var agreedtoTermsConditions: Bool!

@IBAction func checkedButtonTapped(_ sender: UIButton) {

    if sender.isSelected {
        agreedtoTermsConditions = false
        sender.isSelected = false
        self.proceedButton.isEnabled = false

    } else {
        agreedtoTermsConditions = true
        sender.isSelected = true
       self.proceedButton.isEnabled = true

}
}

 @IBAction func proceedButtonTapped(_ sender: UIButton) {

    createUser(userRole: userRole!, credential: firstCredential!, email: emailString, secureText: confirmPasswordString, termsConditions: agreedtoTermsConditions) { (success, error) in

        guard success == true, error == nil else {

            if let networkError = error {

                if networkError == .AlreadyActivated {
                    _ = SCLAlertView(appearance: appearance).showError("Already Activated", subTitle: "\(networkError.rawValue)")
                }else if networkError == .BadRequest {
                    _ = SCLAlertView(appearance: appearance).showError("Error", subTitle: "There is something went wrong. Please try again.")
                }else {
                    _ = SCLAlertView(appearance: appearance).showError("Network Error", subTitle: "\(networkError.rawValue)")
                }
            }

            NotificationCenter.default.post(name: NSNotification.Name(rawValue: "hideLoading"), object: nil)
            return
        }

    }

    NotificationCenter.default.post(name: NSNotification.Name(rawValue: "nextStep"), object: nil)

}

0 Answers