gpt4 book ai didi

iOS - 如何保证 applicationWillTerminate 将被执行

转载 作者:行者123 更新时间:2023-11-28 12:39:13 27 4
gpt4 key购买 nike

有没有办法保证 AppDelegate 委托(delegate)中的 applicationWillTerminate 方法会被命中?像 info.plist 文件中的 key 之类的东西......?

我的目标:我在一个信标应用程序中工作,这段代码在 this article 中.我的问题是,即使我在信标旁边,来自 didEnterRegion 的消息也会不断弹出。为了解决这个问题,我设置了一个标志来控制消息。我的代码如下:

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 处,但并非每次都命中此方法。

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

最佳答案

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

对于不支持后台执行或链接到 iOS 3.x 或更早版本的应用,此方法总是在用户退出应用时调用。 p>

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

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

关于iOS - 如何保证 applicationWillTerminate 将被执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39920541/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com