Advertisement
Guest User

Untitled

a guest
Nov 11th, 2019
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 1.04 KB | None | 0 0
  1. class NetworkManager {
  2.    
  3.     private init() {}
  4.     static let shared = NetworkManager()
  5.    
  6.     private let apikey = "Your Api Key"
  7.     private let url = "https://free.currconv.com/api/v7/convert"
  8.    
  9.     func getCurrentCurrency(complition: @escaping (Response)->Void) {
  10.         var urlComponents = URLComponents(string: url)
  11.         urlComponents?.queryItems = [
  12.             URLQueryItem(name: "q", value: "USD_BYN,BYN_USD"),
  13.             URLQueryItem(name: "compact", value: "ultra"),
  14.             URLQueryItem(name: "apiKey", value: "\(apikey)")
  15.         ]
  16.         let url = urlComponents?.url
  17.         let task = URLSession.shared.dataTask(with: url!) {
  18.             (data, response, error) in
  19.            
  20.             guard let data = data else { return }
  21.             guard let currency: Response = try? JSONDecoder().decode(Response.self, from: data) else {
  22.                 print("Error can't parse")
  23.                 return
  24.             }
  25.             return complition(currency)
  26.         }
  27.         task.resume()
  28.        
  29.     }
  30.    
  31. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement