In my classifieds project there is a user registration page and user id generated in php so in that particular user id all other data of particular user need to save. I would share my swift codes for registration and swift data form to save in that particular user id. But registration user id is getting saved successfully but the datas entering in data form is not saving in unique user id. Please help me in fix that...
Registration page swift
let parameters: Parameters=[
"user_name":nameTextField.text!,
"user_email":emailTextField.text!,
"user_mobile":mobileNumb.text!,
"password":passwordTextField.text!
]
//Sending http post request
Alamofire.request("https://alot.ae/api/user_reg.php", 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
let defaults: UserDefaults = UserDefaults.standard
//This class variable needs to be defined every class where you set or fetch values from NSUserDefaults
defaults.setValue("password", forKey: "user_id")
defaults.synchronize()
//Call this when you're done editing the defaults. It saves you
}}
And my swift code for other form which have to save data in that particular user id
//creating parameters for the post request
let parameters: Parameters=[
"full_name":profN.text!,
"position":position.text!,
"email":emailCompa.text!,
"phone_number":numComP.text!
]
//Sending http post request
Alamofire.request(URL_USER_EMPLOYER_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
let defaults = UserDefaults.standard
defaults.set("URL_USER_EMPLOYER_PROFILE", forKey: "user")
}
}
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
//creating parameters for the post request
let parameters: Parameters=[
"full_name":profN.text!,
"position":position.text!,
"email":emailCompa.text!,
"phone_number":numComP.text!,
"user_id" : AuthService.instance.userId ?? ""
]
// Sending http post request
Alamofire.request(URL_USER_EMPLOYER_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 {
UserDefaults.standard.value(forKey: "user_id")
// converting it as NSDictionary
let jsonData = result as! NSDictionary
let defaults: UserDefaults = UserDefaults.standard
//This class variable needs to be defined every class where you set or fetch values from NSUserDefaults
}
}
let employer = self.storyboard?.instantiateViewController(withIdentifier: "employer") as! EmployerViewController
self.present(employer, animated: true)
}