(IOS Swift) Some functions operate on the simulator but not on the device

advertisements

I'm making an alarm app. There are two things that work on the simulator but not on my device.

1) According to this website, if UIApplicationExitsOnSuspend is set to "YES", then the app should always be running even if the device is locked (provided that it's in foreground). I used this information to make sure that NSTimer runs even if device is locked. On the simulator, no matter how much time passes by (even like 10+ hours) the NSTimer always works and the sound always fires (even if I lock the simulator screen). When I connect my iPhone device to the computer and run the app through xcode it works well too. But the problem is that as soon as I disconnect my phone and load the saved app, sometimes the NSTimer works and sometimes it doesn't.

2) Local Notifications: On the simulator the local notifications work well but on the iPhone device the notifications are fired at random times and sometimes they don't even fire. But on the simulator the local notifications fire accurately. I tested in different ways (such as setting local notification to fire 20 seconds after app loads and then exiting the app to be in background mode) but it doesn't fire on the specified time and like I said it sometimes doesn't even fire.

In such case, should I trust simulator or the iPhone device? Could this be a bug or is it something I'm doing wrong?


Always trust the device. After all, that's how the end user is going to run your app!

It is well known that you should not trust the Simulator for background behavior. The NSTimer issue is an excellent example. The problem is that the Simulator is your computer, not an iOS device. Timers do not in fact run in the background; the Simulator is misleading you. (The issue where the app behaves differently on the device if being run from Xcode vs. being run independently is a bit more suprising, but in this case I suppose it isn't all that surprising; you were very clever to think of testing both ways.)

And of course there are lots of iOS features that don't work on the Simulator at all.