共享偏好Swift 4- iOS-独特的Alamofire用户ID

时间:2018-05-21 06:12:14

标签: ios json swift alamofire xcode9

在我的分类广告项目中,有一个用户注册页面和用户ID在php中生成,因此在该特定用户ID中,特定用户的所有其他数据都需要保存。我会分享我的swift代码进行注册和swift数据表单以保存在特定的用户ID中。但注册用户ID已成功保存,但以数据形式输入的数据未保存在唯一用户ID中。请帮我修复......

注册页面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

            }}

我的其他表单的快速代码,必须在该特定用户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")

            }
    }

1 个答案:

答案 0 :(得分:0)

我为iPhone开发人员@ Muhammed Azharuddin找到了答案,方法是为用户默认创建一个类以将数据存储在user_id中

extension UserDefaults{

    static let isLoggedIn = "com.xyz.AlotTest1isLoggedIn"
    static let userId = "com.xyz.AlotTest1userId"

}

并在@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)
      }