Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- UserDefaults.standard.set(username, forKey: "username")
- UserDefaults.standard.synchronize()
- UserDefaults.standard.value(forKey: "username")
- pod 'Alamofire', '~> 4.0'
- import Alamofire
- 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
- // ...
- }
- }
- 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
- }
- }
- 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
- UserDefaults.standard.set("your value", forKey: "your key")
- UserDefaults.standard.synchronize()
- 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()
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement