应用终止后运行代码...可能吗? (Xcode 8,Swift 3) [英] Run code after the app has terminated... Possible? (Xcode 8, Swift 3)

查看:96
本文介绍了应用终止后运行代码...可能吗? (Xcode 8,Swift 3)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是想知道是否可以在终止应用程序之后和之后运行一些代码.我希望应用程序每隔几秒钟检查一次远程数据库中的数据,如果有任何更改,应该通知用户(我想是使用本地通知).当应用程序运行时,我可以使用NSTimer的interval方法轻松地做到这一点,但是我想知道如果终止该应用程序会有什么选择.如果我在Google周围搜索,似乎几乎没有办法这样做,但这对我来说似乎有点奇怪,不太可能,因为那里有大量的应用程序(例如任何社交网络应用程序)可以在何时通知您即使应用程序完全关闭,您的帐户也会发生某些变化.我很确定那些应用程序如果要在我的设备上通知我,最终必须运行一些代码,对吗?他们是如何做到的?

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?

推荐答案

因此,在弄清每个人对在后台运行"和终止应用程序"的定义之后,我猜想是最合适的,但如此对此问题的简单回答是:不,您不能!触发某些在客户端(在我的情况下为Xcode)编写的代码的唯一方法是,让客户端通知远程服务器上发生了一些事件,而在iOS中,必须通过向以下服务器发送通知来发生这些事件客户端必须以适当的方式对通知进行响应"的APNS.但是,我要问的是,是否可以用Xcode编写一些代码,即使用户终止了该应用程序,该代码仍将继续运行(双击主屏幕按钮以扫掠屏幕截图以杀死您的应用程序):不!没有远程服务器参与该过程,就无法做到这一点.

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.

这篇关于应用终止后运行代码...可能吗? (Xcode 8,Swift 3)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆