如何在NSObject中保存用户

时间:2017-08-05 05:18:25

标签: swift nsuserdefaults

我创建了一个注册页面,其中有用户名,密码,确认密码,电子邮件ID,手机号码。这些是详细信息,我是Swift的新用户,我希望使用NSUserDefaults来帮助我保存应用程序中注册页面的用户详细信息,以及如何发布请求。

2 个答案:

答案 0 :(得分:0)

在UserDefaults中设置数据 -

UserDefaults.standard.set("your value", forKey: "your key")
UserDefaults.standard.synchronize()

在UserDefaults中获取数据

UserDefaults.standard.value(forKey: "your key")

发布示例

func postAction(_ sender: Any) {
        let Url = String(format: "your url")
        guard let serviceUrl = URL(string: Url) else { return }
        //        let loginParams = String(format: LOGIN_PARAMETERS1, "test", "Hi World")
        let parameterDictionary = ["username" : "Test", "password" : "123456"]
        var request = URLRequest(url: serviceUrl)
        request.httpMethod = "POST"
        request.setValue("Application/json", forHTTPHeaderField: "Content-Type")
        guard let httpBody = try? JSONSerialization.data(withJSONObject: parameterDictionary, options: []) else {
            return
        }
        request.httpBody = httpBody

        let session = URLSession.shared
        session.dataTask(with: request) { (data, response, error) in
            if let response = response {
                print(response)
            }
            if let data = data {
                do {
                    let json = try JSONSerialization.jsonObject(with: data, options: [])
                    print(json)
                }catch {
                    print(error)
                }
            }
            }.resume()
    }

答案 1 :(得分:-1)

保存数据

 UserDefaults.standard.set(username, forKey: "username")
 UserDefaults.standard.synchronize()

获取数据

  UserDefaults.standard.value(forKey: "username")     

要发布数据,您可以使用Alamofire

创建一个pod文件add

     pod 'Alamofire', '~> 4.0'

在你的viewcontroller中

 import Alamofire

创建func

 funcLogin() {


    let url = "http://example.com"

    let parameters : [String:Any] = ["username": UserDefaults.standard.value(forKey: "username")  as! String,
                                     "password": UserDefaults.standard.value(forKey: "password")  as! String,
                                     "email":UserDefaults.standard.value(forKey: "email")  as! String]


    Alamofire.request(url,method:.post, parameters: parameters).validate(contentType: ["application/json"])
        .responseJSON{ response in


            switch response.result {
            case .success:

                let statusCode = (response.response?.statusCode)!
                print("HTTP code @apiGetUser: \(statusCode)")
                print("yoo")
                print(response.result.value! )



                // ...

            case .failure(let error):
                print(error)
                //   print("yo")
                break
                // ...
            }

    }

您也可以使用此

扩展uiviewcontroller

     extension UIViewController{


      class user {


    var user_name : String
    var user_Email : String
    var user_password : String
    var item_Mobile : String

    init(user_name : String, user_Email : String, user_password : String, item_Mobile : String) {


        self.user_name = user_name
        self.user_Email = user_Email
        self.user_password = user_password

        self.item_Mobile = item_Mobile




    }


}

在ViewController中

           var userData = [user]()

如果想要全球访问

          static  var userData = [user]()

添加值使用

//对于本地​​

        userData.append(cartItem(user_name: "Demo1",
                                                    user_Email: "demo@demo.com",
                                                    user_password: "hhhhhhh",
                                                    item_Mobile: "9889878888"))

//使用全局

       viewControllername.userData.append(cartItem(user_name: "Demo1",
                                                    user_Email: "demo@demo.com",
                                                    user_password: "hhhhhhh",
                                                    item_Mobile: "9889878888"))

访问数据

           userData[i]. user_name