Search This Blog

Wednesday, August 19, 2015

Sample code

    func displayKeyboard(){
        self.userInputTextField .becomeFirstResponder()
    }
   
    func disableKeyboard(){
        self.userInputTextField .resignFirstResponder()
    }

=========================  

 func getCurrentCurrencyData() -> Void{
        let currencyURL = NSURL(string: "http://api.fixer.io/latest?base=USD")
       
       
        let sharedSession = NSURLSession.sharedSession()
        let downloadTask: NSURLSessionDownloadTask = sharedSession.downloadTaskWithURL(currencyURL!, completionHandler: { (location: NSURL!, response:NSURLResponse!, error: NSError!) -> Void in
           
           
            if(error == nil){
                let dataObject = NSData(contentsOfURL: location)
                let currencyDictionary: NSDictionary = NSJSONSerialization.JSONObjectWithData(dataObject!, options: nil, error: nil) as! NSDictionary
               
               
                let rates = Currency(currencyDictionary: currencyDictionary)
               
                dispatch_async(dispatch_get_main_queue(), { () -> Void in
                   
                    var userInputStringValue: String = self.userInputTextField.text
                    var userInputDoubleValue : Double = NSString(string: userInputStringValue).doubleValue
                   
                   
                    if(userInputDoubleValue > 0){
                       
                        var gbpCurrentValue = rates.gbp * userInputDoubleValue
                        var eurCurrentValue = rates.eur * userInputDoubleValue
                        var jpyCurrentValue = rates.jpy * userInputDoubleValue
                        var brlCurrentValue = rates.brl * userInputDoubleValue
                       
                        var resultString = "UK Pounds - (GBP): " + gbpCurrentValue.description + "\n" +
                            "EU Euro - (Euro)     : " + eurCurrentValue.description + "\n" +
                            "Japan Yen - (JPY)   : " + jpyCurrentValue.description + "\n" +
                            "Brazil Reais - (BRL): " + brlCurrentValue.description as NSString
                       
                       
                        self.resultTextView.text = resultString as String
                        self.resultTextView.font = UIFont(name: "ArialMT", size: 15)
                       
                    }
                       
                    else{
                        self.resultTextView.text = "Please enter a valid dollar amount which is greater then 0"
                    }
                   
                   
                })
               
               
            }
               
            else{
               
                let networkIssueController = UIAlertController(title: "Error", message: "Unable to load data. Connectivity error!", preferredStyle: .Alert)
               
                let okButton = UIAlertAction(title: "OK", style: .Default, handler: nil)
                networkIssueController.addAction(okButton)
               
               
               
                //                let cancelButton = UIAlertAction(title: "Cancel", style: .Cancel, handler: nil)
                //                networkIssueController.addAction(cancelButton)
               
                self.presentViewController(networkIssueController, animated: true, completion: { () -> Void in
                    //
                })
               
                dispatch_async(dispatch_get_main_queue(), { () -> Void in
                    self.resultTextView.text = "Unable to load data due to connectivity error! Please press the CONVERT button again to get the conversion rates!"
                    self.resultTextView.font = UIFont(name: "ArialMT", size: 15)
                    self.resultTextView.textColor = UIColor.redColor()
                })
               
                println(error)
            }
           
        })
        downloadTask.resume()
    }

No comments:

Post a Comment