Linked Questions

Popular Questions

Struggling with writing the code for my DIY smart home app

Asked by At

I'm a still a novice at writing code and need some help with my swift code. I am currently writing my own app to be able to control my sonoff smart wifi switches via an app on my iPhone, iMac and Apple watch. I have used IFTTT applets to setup webhooks that allow me to change the state of the device by GET Contents of URL or POST to a url. I have written the code and connected it to the buttons on my main.storyboard but I need to find a way for the code to only run certain parts of the code when a certain switch is triggered and not the entire code.

What currently is happening is I can't turn on my one light without turning on the other one as well because the app wants to run the entire code to turn on the first light and in order to do this the second button has to be pressed.

Is there any way of fixing this problem or is there an easier way to do this? Thanks in advance.

    import UIKit

class ViewController: UIViewController {

@IBOutlet weak var lampON: UIButton! //Floor Lamp ON
@IBOutlet weak var lampOFF: UIButton! //Floor Lamp OFF
@IBOutlet weak var ledsON: UIButton! //Desk LEDS ON
@IBOutlet weak var ledsOFF: UIButton! //Desk LEDs OFF

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a
    nib.
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated
}
//Turns Floor Lamp ON
@IBAction func tappedButton(_ sender: Any) {

    let url = URL(string:
        "https://maker.ifttt.com/trigger/")
    //The string url continues but I removed the rest for security purposes

    let task = URLSession.shared.dataTask(with: url!) { (data,
        response, error) in

        if error != nil {

            print(error)

        }
        else{

            let htmlContent = NSString(data: data!, encoding:
                String.Encoding.utf8.rawValue)

            print(htmlContent)

        }

    }

    task.resume()

 }//End of Code That Turns Floor Lamp ON
 //Turns Floor Lamp OFF
 @IBAction func tappedOffButton(_ sender: Any) {

    let url = URL(string: "https://maker.ifttt.com/trigger/")
    //The url string continues but I removed the rest for security purposes

    let task = URLSession.shared.dataTask(with: url!) { (data,
        response, error) in

        if error != nil {

            print(error)

        }
        else{

            let htmlContent = NSString(data: data!, encoding:
                String.Encoding.utf8.rawValue)

            print(htmlContent)

        }

    }

    task.resume()
 } //End of Code That Turns Floor Lamp OFF
 //Turns Desk LEDs ON
 @IBAction func deskLEDSON(_ sender: Any) {

    let url = URL(string:   "https://maker.ifttt.com/")
        //The url string continues but I removed the rest for security purposes

    let task = URLSession.shared.dataTask(with: url!) { (data,
        response, error) in

        if error != nil {

            print(error)

        }
        else{

            let htmlContent = NSString(data: data!, encoding:
                String.Encoding.utf8.rawValue)

            print(htmlContent)

        }

    }

    task.resume()

  } //End of Code That Turns Desk LEDs ON
 // Turns Desk LEDs OFF
 @IBAction func deskLEDsOFF(_ sender: Any) {

    let url = URL(string: "https://maker.ifttt.com/trigger/")
    //The url string continues but I removed the rest for security purposes

    let task = URLSession.shared.dataTask(with: url!) { (data,
        response, error) in

        if error != nil {

            print(error)

        }
        else{

            let htmlContent = NSString(data: data!, encoding:
                String.Encoding.utf8.rawValue)

            print(htmlContent)

        }

    }

    task.resume()
  } //End of Code that Turns Desk LEDs OFF

  } //End of App Code

Related Questions