T.Prezioso
T.Prezioso

Reputation: 41

UIApplicationSignificantTimeChangeNotification

I have been having a problem with UIApplicationSignificantTimeChangeNotification. I have tried using [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(timeChanged:) name:UIApplicationSignificantTimeChangeNotification object:nil]; in viewDidLoad to be notified when a new day has started. The problem is it doesn't seem to call the timeChanged: method when I put a breakpoint in the method. I have tested running on both on a device and in the iOS simulator by changing the date and nothing happens. What am I doing wrong?

Upvotes: 3

Views: 2667

Answers (1)

Adrian
Adrian

Reputation: 20068

I managed to make it work. My problem is that I was testing on the Xcode simulator and I was changing the time on the laptop to trigger a day change. But the time of the simulator wasn't changed.
So I had to do the same thing running on the physical device and change time over there.

Here is the code I was using inside AppDelegate (swift):

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

    // day change notification
    NSNotificationCenter.defaultCenter().addObserver(self, selector: "dayChanged:", name: UIApplicationSignificantTimeChangeNotification, object: nil)

    return true
}


@objc func dayChanged(notification: NSNotification){

    print("notification received")
}

Upvotes: 1

Related Questions