iOS-如何保证applicationWillTerminate将被执行 [英] iOS - How to guarantee that applicationWillTerminate will be executed

查看:33
本文介绍了iOS-如何保证applicationWillTerminate将被执行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有一种方法可以保证会击中 AppDelegate 委托中的 applicationWillTerminate 方法?像是info.plist文件中的键之类的东西??

Is there A way to guarantee that the applicationWillTerminate method in the AppDelegate delegate will be hit? Something like a key in the info.plist file, etc..?

我的目标:我正在一个beacon应用程序中,这段代码在本文.我的问题是,即使我在信标旁边,来自didEnterRegion的消息仍会弹出.为了解决这个问题,我设置了一个标志来控制消息.我的代码如下:

My goal: I'm working in a beacon app, the piece of code is in this article. My problem is that the message from the didEnterRegion keeps poping even when i'm beside the beacon. To solve that I'm setting a flag to control the message. My code below:

if(!UserDefaults.standard.bool(forKey: Constants.EnterZoneMsgShowName)){

    let notification = UILocalNotification()
    notification.alertBody = "Hi, you are about to arrive at CIDMA's office. Please open de demo app and turn on the bluetooth on your device to enrich your experience. "
    UIApplication.shared.presentLocalNotificationNow(notification)

    UserDefaults.standard.set(true, forKey: Constants.EnterZoneMsgShowName)
} 

我想在关闭应用程序时将此标志设置为false.我试图将其放在 applicationWillTerminate 上,但这种方法并非每次都可用.

I want to set this flag to false when I close the app. I tried to put it at the applicationWillTerminate but this method is not hit every time.

我想知道如何保证将击中此代码,或者是否有更好的放置代码的地方: UserDefaults.standard.set(false,forKey:Constants.EnterZoneMsgShowName)

I would like to know how to guarantee that this code will be hit or if there is a better place to put the code: UserDefaults.standard.set(false, forKey: Constants.EnterZoneMsgShowName)

推荐答案

applicationWillTerminate(_ :)-告诉委托人应用程序即将开始的时间终止.

applicationWillTerminate(_:) - Tells the delegate when the app is about to terminate.

对于不支持后台执行或与iOS 3.x或更早版本链接的应用,当用户退出该应用时,该方法会始终被调用.

For apps that do not support background execution or are linked against iOS 3.x or earlier, this method is always called when the user quits the app.

对于支持后台执行的应用程序,用户退出应用程序时通常不会不调用此方法,因为在这种情况下,应用程序只是移至后台.但是,在应用程序在后台运行(未挂起)并且系统出于某种原因需要终止它的情况下,可以调用此方法.

For apps that support background execution, this method is generally not called when the user quits the app because the app simply moves to the background in that case. However, this method may be called in situations where the app is running in the background (not suspended) and the system needs to terminate it for some reason.

您要调用的是 applicationDidEnterBackground (如果您的应用程序支持后台执行),则在用户退出时调用此方法而不是 applicationWillTerminate:.

What you want to call is applicationDidEnterBackground if your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.

这篇关于iOS-如何保证applicationWillTerminate将被执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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