1

New here to Xcode and Swift. I am working on an IOT app that enables me to switch a light on at a distance through a button on my today extension.

So I activated app groups and I am using UserDefaults to share info between app and widget. My logic being when button is pressed on the widget it returns a bool true. And if it returns true, I activate for now what is a LED on my ESP8266.

However, this doesn't seem to work, when I click the button nothing happens. What am I doing wrong? Any help or hints would be great! Thank you.

In viewcontroller.swift

override func viewDidLoad() {

    defaults?.synchronize()


    let Distribute = defaults?.bool(forKey: "Distribute")

    if Distribute == true {

        let requestURL = NSURL(string: led1on_URL)
        // Instantiate an NSURLRequest object using the
        // requestURL NSURL
        let request = NSURLRequest(url: requestURL! as URL)
        // Use the webview to send the request to the
        // request NSURLRequest
        View1.loadRequest(request as URLRequest)

    super.viewDidLoad()




   @IBAction func LED_control(_ sender: UIButton) {


}
}

In todayviewcontroller I have:

@IBAction func ButtonSnd(_ sender: Any) {

    defaults?.set(true, forKey: "Distribute") //Bool Data Type
    //Pass anything with this line
    //defaults?.set("\(aNumber)", forKey: "userKey")
    defaults?.synchronize()
    print("Buttonpressed")

}

}
  • please don't post all code share only relevant code – Nazmul Hasan Nov 02 '17 at 15:36
  • 1
    I did remove defaults?.synchronize() and it does still work the same. Thanks! – user8867999 Nov 02 '17 at 15:54
  • @NazmulHasan I will take care of that – user8867999 Nov 02 '17 at 15:55
  • Just guessing that your current workflow is to leave the main app and go to the today view to press the button on you today extension and then after that going back to your main app. Try to use the Notification Center like explained here https://stackoverflow.com/a/5278108/5327882 after your main app did become active again to trigger reading the user defaults and the latest value of your `Distribute` key and update your view. Use the `UIApplicationDidBecomeActiveNotification` – ronatory Nov 02 '17 at 17:39
  • Hi thanks for your reply, my workflow is quitting the main app(let it run in the background if necessary), from there on, I use only the button on the widget to switch on the LED. Is notification center viable to use in this case to update the value of Distribute? – user8867999 Nov 02 '17 at 18:16
  • If you quit your main app (swipe up to close) and start the app again you should get the last value of Distribute already because `viewDidLoad` gets called. But if you just put it in the background you can try to use notification center like I suggested in the comment before. Because after you open the app again `viewDidLoad` is not called again and you don't get the last value see the linked answer to use `UIApplicationDidBecomeActiveNotification ` for that. Hope that helps – ronatory Nov 02 '17 at 20:13
  • 1
    Have you enabled the "App Groups" capability in your project and have you then initialized the UserDefaults with the appropriate suite name (i.e. `UserDefaults(suiteName: "group.com.yourcompany")`) in both the widget and your main app (or a common framework)? I am trying to understand if the button action is triggered but the defaults don't sync. – stakri Nov 02 '17 at 20:28
  • @stakri I have enabled App groups and initialized the userDefaults in both my app and the widget. – user8867999 Nov 03 '17 at 11:04

0 Answers0