Reputation: 445
I try to update the content of my today widget extension every x seconds since I try to realize something like a diashow. Therefor I have stored all required data using shared defaults. Loading data from the storage works perfect but the completionHandler of the extension:
func widgetPerformUpdateWithCompletionHandler(completionHandler: ((NCUpdateResult) -> Void)!) {
//I do load the content here
completionHandler(NCUpdateResult.NewData)
}
Is only called once. How can I implement a function that says that "newData" is available every x seconds?
Upvotes: 5
Views: 15232
Reputation: 762
The one way is NSTimer. It is useful for calling method every x seconds.
var timer : NSTimer?
override func viewDidLoad() {
super.viewDidLoad()
timer = NSTimer.scheduledTimerWithTimeInterval(3, target: self, selector: "animateFrame:", userInfo: nil, repeats: true)
}
func animateFrame(timer: NSTimer) {
// Do something
}
In the case, you can call animateFrame: every 3 seconds.
Upvotes: 3
Reputation: 8328
There is a lot of way to do this. One way is to add an Observer.
Like this
NSNotificationCenter.defaultCenter().addObserver(self, selector:"updateStuff", name:
UIApplicationWillEnterForegroundNotification, object: nil)
func updateStuff() -> Void{
// Update your Stuff...
}
So the Selection calls an function in your Today Widget class.
So your Widget will call your function when your wider will enter foreground.
Note that your widgetPerformUpdateWithCompletionHandler(completionHandler: ((NCUpdateResult) -> Void))
Is there to load content from the Web. Than you don't need an observer
I hope I could help you
Upvotes: 1