Guest User

Untitled

a guest
Jul 22nd, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.98 KB | None | 0 0
  1. func USDtoEUR(_ completionHandler: @escaping (_ success: Bool, _ quotes: [String:AnyObject]?, _ error: String?) -> Void) {
  2.  
  3. let urlString = "https://www.alphavantage.co/query?function=CURRENCY_EXCHANGE_RATE&from_currency=USD&to_currency=EUR&apikey=NP3M8LL62YJDO0YX"
  4. let session = URLSession.shared
  5. let url = URL(string: urlString)!
  6.  
  7. let request = URLRequest(url: url)
  8. let task = session.dataTask(with: request, completionHandler: { data, response, error in
  9. if error != nil {
  10. completionHandler(false, nil, error!.localizedDescription)
  11. }
  12. else {
  13. do {
  14. let result = try JSONSerialization.jsonObject(with: data!, options: JSONSerialization.ReadingOptions.allowFragments) as! NSDictionary
  15. if let dictionary = result["Realtime Currency Exchange Rate"] as? [String:AnyObject]! {
  16. completionHandler(true, dictionary, nil)
  17. }
  18. else {
  19. completionHandler(false, nil, nil)
  20. }
  21. } catch {
  22. completionHandler(false, nil, "Unable to process retrieved data.")
  23. }
  24. }
  25.  
  26. })
  27. task.resume()
  28.  
  29. }
  30.  
  31. func usdQUotesRequest() {
  32.  
  33. USDClient().USDtoEUR() { success, newQuote, error in
  34.  
  35. if success {
  36. self.usdtoeurquote = newQuote
  37. DispatchQueue.main.async {
  38. self.stopActivityIndicator()
  39. self.Refresh.isEnabled = true
  40. }
  41.  
  42. } else {
  43. DispatchQueue.main.async {
  44. self.displayAlert("Unable to Retrieve Latest Conversion Rates", message: "(error!)")
  45. self.stopActivityIndicator()
  46. self.Refresh.isEnabled = true
  47.  
  48. }
  49. }
  50. }
  51.  
  52. @IBAction func usdConversions(_ sender: Any) {
  53.  
  54. self.displayAlert("Alert!", message: "USD Selected")
  55.  
  56. let usdVal = (outputCurrency1.text! as NSString).floatValue
  57.  
  58. let euroValue = usdVal * (usdtoeurquote["5. Exchange Rate"] as! Float)
  59. outputCurrency2.text = String(format: "%.2f", euroValue)
  60.  
  61. let gbpVal = usdVal * (usdtogbpquote["5. Exchange Rate"] as! Float)
  62. outputCurrency3.text = String(format: "%.2f", gbpVal)
  63.  
  64. let cnyVal = usdVal * (usdtocnyquote["5. Exchange Rate"] as! Float)
  65. outputCurrency2.text = String(format: "%.2f", cnyVal)
  66.  
  67. let cadVal = usdVal * (usdtocadquote["5. Exchange Rate"] as! Float)
  68. outputCurrency2.text = String(format: "%.2f", cadVal)
  69.  
  70. let inrVal = usdVal * (usdtoinrquote["5. Exchange Rate"] as! Float)
  71. outputCurrency2.text = String(format: "%.2f", inrVal)
  72.  
  73. let sekVal = usdVal * (usdtosekquote["5. Exchange Rate"] as! Float)
  74. outputCurrency2.text = String(format: "%.2f", sekVal)
  75.  
  76. let rubVal = usdVal * (usdtorubquote["5. Exchange Rate"] as! Float)
  77. outputCurrency2.text = String(format: "%.2f", rubVal)
  78.  
  79. let nzdVal = usdVal * (usdtonzdquote["5. Exchange Rate"] as! Float)
  80. outputCurrency2.text = String(format: "%.2f", nzdVal)
  81.  
  82. }
Add Comment
Please, Sign In to add comment