1

I need to execute a function (that basically start updating location and distance driven) without opening any view controller (on background)

I need the widget to work like a "remote control" of a button inside my app.

Then when I open the app, the process should be running and showing the respective view controller

alvarogalia
  • 1,601
  • 2
  • 12
  • 10

1 Answers1

0

You can't communicate directly with your app's process. If you don't want to launch the app immediately, you can just pass it some data via your shared container:

let sharedUserDefaults = NSUserDefaults(suiteName: "group.com.mycompany.myapp")!
sharedUserDefaults.setBool(true, forKey: "tracking location")
sharedUserDefaults.synchronize()

Then in your app's applicationDidBecomeActive:, you can read that value out:

let sharedUserDefaults = NSUserDefaults(suiteName: "group.com.mycompany.myapp")!
if sharedUserDefaults.boolForKey("tracking location") {
    startTrackingLocation()
}
sharedUserDefaults.removeObjectForKey("tracking location")
sharedUserDefaults.synchronize()

Your today widget controller can also be a CLLocationManagerDelegate and track the user's location while the widget is visible.

Christopher Pickslay
  • 17,523
  • 6
  • 79
  • 92
  • 1
    That's the problem, the widget will be visible just for a couple of seconds, just to hit "play", it need to be catching data a lot of time... Then, when the user finish the process, he can review all data on app. Isn't possible to run an app without showing it to user? – alvarogalia Aug 15 '15 at 15:46