所以我创建了一个简单的应用程序,在手表上显示一个人的以太平衡。问题是它运行时应该在watchOS 3.2上运行,它只是不会加载到watchOS 3.1(实际的手表和模拟器)。 Xcode不会显示任何错误或警告。
InterfaceController.swift的代码(这是我的第一个手表应用,所以看起来有点乱):
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)
}
}