Reputation: 775
I was just wondering if it's possible to run some code after and while an app is terminated. I want to have data on a remote database checked on every few seconds by the app, and if anything changes, the user should be notified (using local notifications, I guess?). While the app is running I can easily do this, using the interval method of NSTimer, but I was wondering what the alternative would be if the app was terminated. If I Google around, it almost seems as if there's literally NO WAY of doing this, but that seems kinda strange and unlikely to me, since there's a boatload of apps out there (any social networking app, for example) that can notify you when something happens/changes in your account, even when the app is completely closed. I'm pretty sure those apps have to run some code eventually if they want to notify me on my device, right? How do they do it?
Upvotes: 1
Views: 1900
Reputation: 775
So after figuring out what everyone's own definition of "running in the background" and "terminating an app" is, I'm guessing the most adequate and yet so simple answer to this much asked question is: NO, YOU CANNOT! The only way to trigger some code which is written on your client-side (in Xcode in my case), is to have the client notified that some events have occurred on a remote server, which in iOS would have to happen by sending notifications to APNS, to which the client would have to 'respond' to the notification in an appropriate way. However, what I was asking about, whether or not you could just write some code in Xcode which would keep running even after the app has been terminated by the user (double-pressed Home button to kill your app by swiping up the screenshot): NO! There is NO WAY of doing this without a remote server getting involved in the process.
Upvotes: 2