I am trying to get my app to countinously check a UserDefault
to see if it changed. If the default, when the viewDidLoad
, is at 20 and a moment afterward decreases to 15 i want the ViewController
to refresh and show the correct number. Here is my code for reading the default.
import UIKit
let Ironcnt = UserDefaults.standard
let Goldcnt = UserDefaults.standard
let Fiveth = UserDefaults.standard
let shipcnt = UserDefaults.standard
let empirecnt = UserDefaults.standard
var fiveth = Int()
var iron = Int()
var gold = Int()
var ships = Int()
var empires = Int()
class twenty: UIViewController {
@IBOutlet weak var fiveth1: UILabel!
@IBOutlet weak var iron1: UILabel!
@IBOutlet weak var gold1: UILabel!
@IBOutlet weak var ships1: UILabel!
@IBOutlet weak var empire1: UILabel!
@IBOutlet weak var webViewBG: UIWebView!
override func viewDidLoad() {
super.viewDidLoad()
let htmlPath = Bundle.main.path(forResource: "WebViewContent", ofType: "html")
let htmlURL = URL(fileURLWithPath: htmlPath!)
let html = try? Data(contentsOf: htmlURL)
self.webViewBG.load(html!, mimeType: "text/html", textEncodingName: "UTF-8", baseURL: htmlURL.deletingLastPathComponent())
if (Fiveth.value(forKey: "Fiveth") != nil){
fiveth = Fiveth.value(forKey: "Fiveth") as! NSInteger!
fiveth1.text = NSString(format: "Fiveth: %i", fiveth) as String
}
if (Ironcnt.value(forKey: "Ironcnt") != nil){
iron = Ironcnt.value(forKey: "Ironcnt") as! NSInteger!
iron1.text = NSString(format: "Iron: %i", iron) as String
}
if (Goldcnt.value(forKey: "Goldcnt") != nil){
gold = Goldcnt.value(forKey: "Goldcnt") as! NSInteger!
gold1.text = NSString(format: "Gold: %i", gold) as String
}
}