gpt4 book ai didi

swift - 本地通知拉入后台功能触发

转载 作者:行者123 更新时间:2023-11-30 11:00:20 26 4
gpt4 key购买 nike

当我终止我的应用程序并两次点击主页按钮时,我如何在 swift 4 上的后台服务中调用我的函数。因为我从事本地推送通知工作,在 API 调用之后获取数据,然后将数据放入通知中。

最佳答案

我不完全确定这就是你的意思,但是如果你想在你的应用程序进入后台之前调用一个函数/做一些事情(当有人双击主页按钮时会发生什么)你可以使用默认的 applicationWillResignActive(_:) 或AppDelegate 类中的 applicationDidEnterBackground(_:) 函数。有关应用程序状态和功能的更多信息,请参阅 the apple docs。如果您想在应用程序终止时执行代码,请使用 applicationWillTerminate(_:)

所以在 AppDelegate.swift 中(为了澄清,我故意留下了原始的 Xcode 默认注释):

class AppDelegate: UIResponder, UIApplicationDelegate {

func applicationWillResignActive(_ application: UIApplication) {
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
yourGoToBackgroundFunction()
}

func applicationDidEnterBackground(_ application: UIApplication) {
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
yourSaveDataFunction()
}

func applicationWillTerminate(_ application: UIApplication) {
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
yourSaveDataFunction()
}
}

关于swift - 本地通知拉入后台功能触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53458192/

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