0

So I created a simple app that shows one's ether balance on the watch. The problem is while it runs as it is supposed to on watchOS 3.2 it just won't load on watchOS 3.1 (both an actual watch and the simulator). Xcode doesn't show any errors or warnings.

The code for InterfaceController.swift (it's my first watch app so it might look a little bit messy):

class InterfaceController: WKInterfaceController {

    override func awake(withContext context: Any?) {
        super.awake(withContext: context)

        // Configure interface objects here.
    }

    var ether : Float = 0;

    override func willActivate() {
        // This method is called when watch view controller is about to be visible to user

        super.willActivate()

        let url = URL(string: "https://api.nanopool.org/v1/eth/balance/0x90e47be1e1cc1dc51d975bcd98403e49b85322ad")
        let task = URLSession.shared.dataTask(with: url!) { (data, response, error) in
            if error != nil{
                print("error")
            }
            else{
                if let content = data{
                    do{
                        let myJSON = try JSONSerialization.jsonObject(with: content, options: JSONSerialization.ReadingOptions.mutableContainers) as AnyObject

                        if let balance = myJSON.object(forKey: "data"){
                        let amount = myJSON.object(forKey: "data") as! Float
                            self.ether = amount
                        }
                    }
                    catch{

                    }
                }
            }
        }
        task.resume()
    }

    override func didDeactivate() {
        // This method is called when watch view controller is no longer visible
        super.didDeactivate()
    }

    @IBOutlet var showBalance: WKInterfaceLabel!

    @IBAction func updateBalance() {
        let calculate = (ether/0.2)*100
        let percent = round(calculate)
        let balance = "\(ether) ETH\n\(percent)%"
        showBalance.setText(balance)
    }
}
Meet Doshi
  • 4,241
  • 10
  • 40
  • 81
kapits
  • 59
  • 1
  • 8

0 Answers0