当应用程序被杀死时,向Web服务发送请求? (快速3) [英] Send request to web service when app gets killed? (Swift 3)

查看:87
本文介绍了当应用程序被杀死时,向Web服务发送请求? (快速3)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于每次用户杀死我的应用程序时通知我的应用程序是非常重要的(不是将其移至后台,而是通过双击主页"按钮并向上滑动我的应用程序屏幕截图实际上完全将其退出).我知道applicationWillTerminate()几乎没有被调用过,所以我想知道当用户杀死该应用程序时最安全的通知方式是什么.苹果似乎没有提供任何形式的解决方案的可能性很小,因为许多iOS用户将它当成一种习惯,当它们用完后就杀死了这些应用程序,因此,如果真的这样做,那真的很不可思议.无法赶上此事件.我唯一需要做的就是解除URLRequest以便向我的Web服务发送一些更新指令.

It's very important for my app to be notified every time the user kills my app (not moving it to the background, but actually quitting it altogether by double-tapping the Home button and swiping my app screenshot up). I know applicationWillTerminate() barely ever gets called, so I was wondering what the safest way is to be notified when a user will kill the app. It seems very unlikely that Apple hasn't provided ANY form of solution for this, as a lot of iOS users make it a custom to kill apps when they're done using it, so it would be really really REALLY weird if it would be impossible to catch this event. The only thing I need to do is fire off a URLRequest to send some updating instructions to my web service.

推荐答案

当我添加观察者来处理UIApplicationWillTerminateNotification时,我会获得更多的一致性.当用户双击主页"按钮并滑动应用以将其杀死时,它将被调用.

I get more consistency when I add an observer to handle UIApplicationWillTerminateNotification. It gets called when the user double-taps the Home button and swipe the app to kill it.

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool 
{
  NSNotificationCenter.defaultCenter().addObserver(self, selector:#selector(AppDelegate.onAppWillTerminate(_:)), name:UIApplicationWillTerminateNotification, object:nil)
  return true
}


func onAppWillTerminate(notification:NSNotification)
{
  print("onAppWillTerminate")
}

这篇关于当应用程序被杀死时,向Web服务发送请求? (快速3)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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