Advertisement
Guest User

Untitled

a guest
Aug 5th, 2017
455
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.49 KB | None | 0 0
  1. UserDefaults.standard.set(username, forKey: "username")
  2. UserDefaults.standard.synchronize()
  3.  
  4. UserDefaults.standard.value(forKey: "username")
  5.  
  6. pod 'Alamofire', '~> 4.0'
  7.  
  8. import Alamofire
  9.  
  10. funcLogin() {
  11.  
  12.  
  13. let url = "http://example.com"
  14.  
  15. let parameters : [String:Any] = ["username": UserDefaults.standard.value(forKey: "username") as! String,
  16. "password": UserDefaults.standard.value(forKey: "password") as! String,
  17. "email":UserDefaults.standard.value(forKey: "email") as! String]
  18.  
  19.  
  20. Alamofire.request(url,method:.post, parameters: parameters).validate(contentType: ["application/json"])
  21. .responseJSON{ response in
  22.  
  23.  
  24. switch response.result {
  25. case .success:
  26.  
  27. let statusCode = (response.response?.statusCode)!
  28. print("HTTP code @apiGetUser: (statusCode)")
  29. print("yoo")
  30. print(response.result.value! )
  31.  
  32.  
  33.  
  34. // ...
  35.  
  36. case .failure(let error):
  37. print(error)
  38. // print("yo")
  39. break
  40. // ...
  41. }
  42.  
  43. }
  44.  
  45. extension UIViewController{
  46.  
  47.  
  48. class user {
  49.  
  50.  
  51. var user_name : String
  52. var user_Email : String
  53. var user_password : String
  54. var item_Mobile : String
  55.  
  56. init(user_name : String, user_Email : String, user_password : String, item_Mobile : String) {
  57.  
  58.  
  59. self.user_name = user_name
  60. self.user_Email = user_Email
  61. self.user_password = user_password
  62.  
  63. self.item_Mobile = item_Mobile
  64.  
  65.  
  66.  
  67.  
  68. }
  69.  
  70.  
  71. }
  72.  
  73. var userData = [user]()
  74.  
  75. static var userData = [user]()
  76.  
  77. userData.append(cartItem(user_name: "Demo1",
  78. user_Email: "demo@demo.com",
  79. user_password: "hhhhhhh",
  80. item_Mobile: "9889878888"))
  81.  
  82. viewControllername.userData.append(cartItem(user_name: "Demo1",
  83. user_Email: "demo@demo.com",
  84. user_password: "hhhhhhh",
  85. item_Mobile: "9889878888"))
  86.  
  87. userData[i]. user_name
  88.  
  89. UserDefaults.standard.set("your value", forKey: "your key")
  90. UserDefaults.standard.synchronize()
  91.  
  92. func postAction(_ sender: Any) {
  93. let Url = String(format: "your url")
  94. guard let serviceUrl = URL(string: Url) else { return }
  95. // let loginParams = String(format: LOGIN_PARAMETERS1, "test", "Hi World")
  96. let parameterDictionary = ["username" : "Test", "password" : "123456"]
  97. var request = URLRequest(url: serviceUrl)
  98. request.httpMethod = "POST"
  99. request.setValue("Application/json", forHTTPHeaderField: "Content-Type")
  100. guard let httpBody = try? JSONSerialization.data(withJSONObject: parameterDictionary, options: []) else {
  101. return
  102. }
  103. request.httpBody = httpBody
  104.  
  105. let session = URLSession.shared
  106. session.dataTask(with: request) { (data, response, error) in
  107. if let response = response {
  108. print(response)
  109. }
  110. if let data = data {
  111. do {
  112. let json = try JSONSerialization.jsonObject(with: data, options: [])
  113. print(json)
  114. }catch {
  115. print(error)
  116. }
  117. }
  118. }.resume()
  119. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement