How to change stored JSON data when i clicked save button? swift 3 -
i stored user json data on "user" when login. how can change value when click save button?something : crew_preferred_name "xiao pang".
userdefaults.standard.set(json, forkey: "json") user = userdefaults().value(forkey: "json")as? nsdictionary this json output
} crew ={ "crew_avatar" = "http://ec2-52-221-231-3.ap-southeast-1.compute.amazonaws.com/gv/images/profile_image/pang_kang_ming_916210_0e9.jpg"; "crew_contact" = 0123456789; "crew_email" = "pang@xover.com.my"; "crew_gender" = male; "crew_id" = pp000001; "crew_name" = "pang kang ming"; "crew_preferred_name" = pkm; "crew_qrcode" = "images/qrcode/qrcode_085960293a5378a64bec6ebfa3c89bb7.png"; } message = "login sucessfully"; result = success; } this button code, i'm posting action php don't know how change or save changes in "user" data. without code have login again see new update.
@ibaction func savebtn(_ sender: any) { let prefername = prefernameeditlabel.text!; if let crew = user!["crew"] as? [string:any], let crewid = crew["crew_id"] as? string, let crewemail = crew["crew_email"] as? string, let crewcontact = crew["crew_contact"] as? string { let param = ["action": "update profile", "crew": ["crew_id": crewid, "crew_preferred_name": prefername, "crew_email": crewemail, "crew_contact": crewcontact]] [string : any] let headers = [ "content-type": "application/json", "cache-control": "no-cache" ] if let postdata = (try? jsonserialization.data(withjsonobject: param, options: [])) { let request = nsmutableurlrequest(url: url(string: "http://52.221.231.3/gv/app_api.php")!, cachepolicy: .useprotocolcachepolicy, timeoutinterval: 10.0) request.httpmethod = "post" request.allhttpheaderfields = headers request.httpbody = postdata _ = urlsession.shared if prefernameeditlabel.text != ""{ let task = urlsession.shared.datatask(with: request urlrequest) { (data, response, error) -> void in dispatchqueue.main.async(execute: { if let json = (try? jsonserialization.jsonobject(with: data!, options: [.mutablecontainers])) as? nsdictionary { let result = json["result"] as? string if (result == "success") { print(result!) self.view.endediting(true) let alert = uialertcontroller(title: "updated", message: "update successfully", preferredstyle: uialertcontrollerstyle.alert) let okbutton = uialertaction(title: "ok", style: uialertactionstyle.cancel, handler: nil) alert.addaction(okbutton) self.present(alert, animated: true, completion: nil) }else{ let alert = uialertcontroller(title: "error", message: "update failed", preferredstyle: uialertcontrollerstyle.alert) let okbutton = uialertaction(title: "ok", style: uialertactionstyle.cancel, handler: nil) alert.addaction(okbutton) self.present(alert, animated: true, completion: nil) print(result!) } } }) } task.resume() }else{ let alert = uialertcontroller(title: "error", message: "empty!", preferredstyle: uialertcontrollerstyle.alert) let okbutton = uialertaction(title: "ok", style: uialertactionstyle.cancel, handler: nil) alert.addaction(okbutton) self.present(alert, animated: true, completion: nil) } } } }
simply write line after getting latest data
userdefaults.standard.set(json, forkey: "json") in way "json" key contain latest data , can as
user = userdefaults().value(forkey: "json")as? nsdictionary
Comments
Post a Comment