5

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?

Florian Chrometz
  • 445
  • 2
  • 9
  • 22

2 Answers2

3

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.

pixyzehn
  • 762
  • 6
  • 15
1

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

BilalReffas
  • 8,132
  • 4
  • 50
  • 71