gpt4 book ai didi

iphone - 如何使用 NSUserDefaults 和 UIApplicationWillTerminateNotification 在用户注销时存储应用程序的当前状态?

转载 作者:行者123 更新时间:2023-11-28 22:56:50 25 4
gpt4 key购买 nike

我需要使用 UIApplicationWillTerminateNotificationNSUserDefaults 来存储应用程序的当前状态,当用户从应用程序。我的应用程序是一个基于 TabBar 的应用程序,它又是一个导航 Controller 。我需要存储在导航 Controller 中加载的当前 View Controller 和导航 Controller 所在的当前选项卡。我的应用程序适用于不同的用户,我需要存储特定于用户的应用程序状态。所以,我也需要存储用户数据。我在 StackOverflow 中进行了搜索,并在 Google 上进行了搜索,发现我们需要使用类似 NSUserDefaultsUIApplicationWillTerminateNotification 之类的东西。这是我第一次遇到这些东西。有人可以发布一些教程或任何示例代码来说明如何完成我的要求吗?

最佳答案

您可以自己注册通知(请参阅 NSNotificationCenter)并在那里编写您的保存/加载代码,或者您可以将您的代码放在 AppDelegate 中,它提供了这些 Hook :

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions;
- (void)applicationWillResignActive:(UIApplication *)application;
- (void)applicationDidEnterBackground:(UIApplication *)application;
- (void)applicationWillEnterForeground:(UIApplication *)application;
- (void)applicationDidBecomeActive:(UIApplication *)application;
- (void)applicationWillTerminate:(UIApplication *)application;

查看 AppDelegate.m 文件,您可以看到 stub 实现以及一些关于如何使用它们的文档。

在那里,您将根据需要简单地将数据写入 NSUserDefaults 并从中读取数据。

// Store the data
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:fooObject forKey:@"Foo"];
[defaults setObject:barObject forKey:@"Bar"];
[defaults synchronize];

// Load the data
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *fooObject = [defaults objectForKey:@"Foo"];
NSString *barObject = [defaults objectForKey:@"Bar"];

关于iphone - 如何使用 NSUserDefaults 和 UIApplicationWillTerminateNotification 在用户注销时存储应用程序的当前状态?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10622675/

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