Guest User

Untitled

a guest
May 10th, 2019
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 2.63 KB | None | 0 0
  1. login(json: json) {status, token, code in
  2.                 if(token != nil && code == 200){
  3.                     print(token)
  4.                 }
  5.                 else{
  6.                     print(1)
  7.                     print(token)
  8.                     print(code)
  9.                 }
  10.             }
  11.  
  12.  
  13. func login(json:Any,completionHandler: (_ granted: Bool, _ token: String?, _ code: Int?) -> ()){
  14.     var codeToSend:Int = 0
  15.     var token:String = ""
  16.     POST(json: json){ data, code in
  17.         if(code == 200)
  18.         {
  19.             do{
  20.                 if let json_response = try JSONSerialization.jsonObject(with: data, options: [])as? [String:Any]{
  21.                     if let temp_token = json_response["auth_token"]{
  22.                         print(temp_token as! String)
  23.                         token = temp_token as! String
  24.                         codeToSend = 200
  25.                     }
  26.                 }
  27.                 else{
  28.                     print("неизвестная ошибка")
  29.                 }
  30.             }
  31.             catch{
  32.                 print("errasd")
  33.                 print(error)
  34.             }
  35.         }
  36.         else if(code == 400)
  37.         {
  38.             token = ""
  39.             codeToSend = 400
  40.             print("Неверное имя пользователя или пароль")
  41.         }
  42.         else{
  43.             token = ""
  44.             codeToSend = 0
  45.         }
  46.     }
  47. //    return (token, codeToSend)
  48.     completionHandler(true, token,codeToSend)
  49. }
  50. func POST(json: Any, callback: @escaping (_ data: Data, _ code: Int?) -> ())
  51. {
  52.     guard let url = URL(string: ngrok+"/api/auth/token/create")else{return}
  53.     var request = URLRequest(url: url)
  54.     request.httpMethod = "POST"
  55.     request.setValue("application/json", forHTTPHeaderField:"Content-Type")
  56.    
  57.     guard let httpBody = try? JSONSerialization.data(withJSONObject: json, options: [])else {return}
  58.     request.httpBody = httpBody
  59.     let sessionConfig = URLSessionConfiguration.default
  60.     sessionConfig.timeoutIntervalForRequest = 5.0
  61.     sessionConfig.timeoutIntervalForResource = 60.0
  62.     URLSession(configuration: sessionConfig).dataTask(with: request){(data, response, error) in
  63.         if  error != nil{
  64.             print("Ошибка севреа")
  65.             DispatchQueue.main.async {callback(Data("".utf8), 0)}
  66.         }
  67.         if let response = response{
  68.             print(response)
  69.             if let httpResponse = response as? HTTPURLResponse{
  70.                 DispatchQueue.main.async {callback(
  71.                     data!, httpResponse.statusCode)
  72.                 }
  73.             }
  74.         }
  75.         }.resume()
  76. }
Advertisement
Add Comment
Please, Sign In to add comment