Search code examples
iosswiftalamofirexcode9.3

Save data in specific user id iOS - Alamofire, Json


I have a job application app that when a user registers through a registration page, it is automatically generating a unique user_id for each user. This app also supports multiple different data for each user (e.g education details, experience etc) so these data have to be connected with the user id of the corresponding user.

For example, suppose user A registers a new account with user_id ABCD. When this user adds new information to the system, the data have to be linked back to this user_id, ABCD. Likewise, if a user B registers a new account with user_id EFGH, any new data added to the system by user B, the data have to be linked back to his/her user_id, EFGH.

How could I do that in iOS using Swift?

Alamofire.request(URL_USER_COMPANY_PROFILE, method: .post, parameters: parameters).responseJSON
{
    response in
    // Printing response
    print(response)

    // Getting the json value from the server
    if let result = response.result.value {
        // Converting it as NSDictionary
        let jsonData = result as! NSDictionary
        UserDefaults.standard.value(forKey: "user_id")
    }
}

Solution

  • I found the answer with iPhone developer @ Muhammed Azharuddin, by creating a class for user defaults to store datas in user_id

    extension UserDefaults{
    
        static let isLoggedIn = "com.xyz.AlotTest1isLoggedIn"
        static let userId = "com.xyz.AlotTest1userId"
    
    }
    

    And call this extension in parameters after @IBAction

    let parameters: Parameters=[
    
        "institute_name":univ.text!,
        "degree_name":grad.text!,
        "start_year":cityP.text!,
        "end_year":enYear.text!,
        "degree_city":proj.text!,
        "degree_country":eduCountry.text!,
        "user_id" : AuthService.instance.userId ?? ""
    ]
    
    
    print(parameters)
    
    //Sending http post request
        Alamofire.request(URL_USER_EDUCATION, method: .post, parameters: parameters).responseJSON
            {
            response in
            //printing response
            print(response)
    
            //getting the json value from the server
            if let result = response.result.value {
    
    
    
                let re = UserDefaults.standard.value(forKey: "user_id") as! Array<Parameters>
                print(result)
    
                let defaults: UserDefaults = UserDefaults.standard
    
                //This class variable needs to be defined every class where you set or fetch values from NSUserDefaults
    
    
    
            }