Advertisement
Guest User

Untitled

a guest
May 8th, 2016
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.49 KB | None | 0 0
  1. import Foundation
  2. import Alamofire
  3. import SwiftyJSON
  4. class Login {
  5.  
  6. var userName:String!
  7. var passWord:String!
  8.  
  9. init(userName : String, passWord : String) {
  10.  
  11. let parameters = [
  12. "username": userName,
  13. "password": passWord
  14. ]
  15.  
  16. Alamofire.request(.POST, "http://anyapi.com", parameters: parameters)
  17. .responseJSON { response in
  18. print(response.request)
  19. print(response.response)
  20. print(response.data)
  21. print(response.result)
  22. if let result = response.result.value {
  23. print("Did receive JSON data: (result)")
  24. let value = JSON(result)
  25. if let api_key = value["api_token"].string {
  26. print("The token is " + api_key)
  27. } else{
  28. print("error parsing api token")
  29. }
  30. //pass data to Users class
  31. _ = Users.init(Name: value["name"].string, Email: value["email"].string, Id: value["id"].string, ProfilePicture: value["profile_picture"].string, Username: value["username"].string)
  32. }
  33. else {
  34. print("JSON data is nil.")
  35. }
  36.  
  37.  
  38. }
  39.  
  40. }
  41. }
  42.  
  43. import Foundation
  44. import Alamofire
  45. import SwiftyJSON
  46.  
  47. class Users {
  48. private var _name: String!
  49. private var _email: String!
  50. private var _userId: String!
  51. private var _profilePicture: String!
  52. private var _username: String!
  53.  
  54. var particulars: [String] = []
  55.  
  56. var name: String {
  57. _name = particulars[0]
  58. return _name
  59. }
  60.  
  61. var email: String {
  62. _email = particulars[1]
  63. return _email
  64. }
  65.  
  66. var userId: String {
  67. _userId = particulars[2]
  68. return _userId
  69. }
  70.  
  71. var profilePicture: String {
  72. _profilePicture = particulars[3]
  73. return _profilePicture
  74. }
  75.  
  76. var username: String {
  77. _username = particulars[4]
  78. return _username
  79. }
  80.  
  81.  
  82. required init?(Name: String?, Email: String?, Id: String?, ProfilePicture: String?, Username: String?) {
  83. particulars += ["(Name)"]
  84. particulars += ["(Email)"]
  85. particulars += ["(Id)"]
  86. particulars += ["(ProfilePicture)"]
  87. particulars += ["(Username)"]
  88. }
  89.  
  90. }
  91.  
  92. import UIKit
  93.  
  94. class ViewController: UIViewController {
  95.  
  96. @IBOutlet weak var usernameLabel: UILabel!
  97.  
  98. // var usernameLabelText = String()
  99.  
  100. var usernameLabelText: String? {
  101. return String(Users.username)
  102. }
  103.  
  104. override func viewDidLoad() {
  105. usernameLabel.text = "Welcome (usernameLabelText)"
  106. }
  107.  
  108. override func didReceiveMemoryWarning() {
  109. super.didReceiveMemoryWarning()
  110. // Dispose of any resources that can be recreated.
  111. }
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement