Hi, I have swift 4.2 code that grabs html content and converts it to a string. Code locates character strings "sensor on" or "sensor off" and sends to UIlabel on my viewcontroller.
Runs well one time and stops. When "sensor on " or Sensor off " changes in the html content, my code will not change the Label because the code has stopped.
Xcode console output shows the code working well, but only for one time.
Any ideas why my code works once only ?.
// ViewController.swift
// Show sensor ON or OFF in UILabel
import UIKit
import WebKit
class ViewController: UIViewController
{ //A
@IBOutlet weak var alarm: UILabel!
//first time view loaded, viewcontroller will call viewdidload.
override func viewDidLoad() { //B
super.viewDidLoad()
print("step 1")
if let url = URL(string: "http://192.168.4.1")
{ //J
do { //I
let contents = try String(contentsOf: url, encoding: .ascii)
print("step 2")
print("HTML : \(contents)")
//Test for paper on roll
//Paper ?
if contents.contains("Sensor is:</2>ON") { //F
print("** sensor is on")
alarm.text = "HAS PAPER"
print("step 3")
} //F
if contents.contains("Sensor is:</2>OFF") { //G
print("step 4")
print("sensor is off")
alarm.text = "NO PAPER"
} //G
} //J
catch { //E
print("check wifi")
} //E
} //I
} //B
override func didReceiveMemoryWarning() { //L
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
} //L
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
print("\(self) " + #function)
guard let tc = self.transitionCoordinator else {print("no tc1"); return}
guard tc.initiallyInteractive else {return}
tc.notifyWhenInteractionChanges { // "changes" instead of "ends"
context in
if context.isCancelled {
print("we got cancelled")
}
}
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
print("\(self) " + #function)
guard let tc = self.transitionCoordinator else {print("no tc2"); return}
guard tc.initiallyInteractive else {return}
tc.notifyWhenInteractionChanges { // "changes" instead of "ends"
context in
if context.isCancelled {
print("we got cancelled")
}
}
}
override func viewDidDisappear(_ animated: Bool) {
super.viewDidDisappear(animated)
print("\(self) " + #function)
guard let tc = self.transitionCoordinator else {print("no tc3"); return}
guard tc.initiallyInteractive else {return}
tc.notifyWhenInteractionChanges { // "changes" instead of "ends"
context in
if context.isCancelled {
print("we got cancelled")
}
}
}
} //A