Advertisement
Guest User

Untitled

a guest
May 10th, 2017
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 2.11 KB | None | 0 0
  1. import Foundation
  2. import Alamofire
  3.  
  4.  
  5. class User {
  6.    
  7.    
  8.     let defaults = UserDefaults.standard
  9.    
  10.     var email: String
  11.     var password: String
  12.     var isLoggedIn: Bool
  13.    
  14.     init(email: String, password: String) {
  15.         self.email = email
  16.         self.password = password
  17.         self.isLoggedIn = false
  18.     }
  19.    
  20.    
  21.     // Выполнение авторизации
  22.     func authenticateUser(user: User, completionHandler: @escaping (_ result: Bool) -> ()) {
  23.         makeAuthenticateUserCall(user: user, completionHandler: completionHandler)
  24.     }
  25.    
  26.     // Выполнение POST запроса
  27.     func makeAuthenticateUserCall(user: User, completionHandler: @escaping (Bool) -> ()) {
  28.  
  29.         let parameters = [
  30.             "email" : user.email,
  31.             "password" : user.password
  32.         ]
  33.        
  34.         Alamofire.request("http://apistaging.busnet.co.il/api/v1/login", method: .post, parameters: parameters, encoding:  URLEncoding.httpBody).responseJSON {response in
  35.                
  36.                 switch response.result {
  37.                 case .success:
  38.                     if let json = response.result.value as? [String: Any],
  39.                         let status = json["status"] as? String,
  40.                         status == "success" {
  41.                         completionHandler(true)
  42.                     }
  43.                 case .failure:
  44.                     completionHandler(false)
  45.                 }
  46.         }
  47.     }
  48. }
  49.  
  50.  
  51. // Код из ViewController:
  52.  
  53.  @IBAction func loginButtonPressed(_ sender: UIButton) {
  54.         if let email = _email.text {
  55.             if let password = _password.text {
  56.                 let user = User(email: email, password: password)
  57.                 print(user.isLoggedIn)
  58.                 user.authenticateUser(user: user){resultBool in
  59.                         user.isLoggedIn = resultBool
  60.                     }
  61.                 print(user.isLoggedIn)
  62.                 if user.isLoggedIn {
  63.                     self.performSegue(withIdentifier: "MainPageViewController", sender: nil)
  64.                 }
  65.             }
  66.         }
  67.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement