使用NSUserDefaults检测应用程序的初始启动 [英] Detecting the initial launch of an app using NSUserDefaults

查看:101
本文介绍了使用NSUserDefaults检测应用程序的初始启动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

参考以下问题: iphone:最快的检测首次启动的最简单方法

当我查找NSUserDefaults apple引用时,它说 registerDefaults 不会将数据存储到磁盘上。在上面的问题中,应用程序在每次启动时将值 firstLaunch 注册到 YES 。因此,每次应用程序启动时, firstLaunch 被覆盖到 YES ,所以应用程序将永远认为这是应用程式的初次启动。

When I looked up NSUserDefaults apple reference, it said that registerDefaults does not store the data onto the disk. In the above question, the app registers the value firstLaunch to YES upon each launch. So each time the app is launched, firstLaunch is overwritten to YES and so the app will always think that this is the app's initial launch.

我可以在这里吗?

编辑:

完成上面的教程后,它并不总是工作。我继续从Xcode重新启动,并且它在第一次启动时保持打印 1 bool值。

After doing what the tutorial above says, it doesn't always work anyway. I keep relaunching from Xcode and it keep printing out 1 bool value as in its the first launch.

推荐答案

registerDefaults:不会覆盖默认值中已有的内容,但会设置默认值为默认值。因此,如果缺少缺省值,它使用default-default。混淆,不是吗?

registerDefaults: doesn't overwrite what is already in the defaults, but it sets defaults-for-the-defaults. So if a default is missing it uses a default-default. Confusing, isn't it?

如果你从未在键 FirstLaunch 下写入值, code> boolForKey:@FirstLaunch将使用 registerDefaults:的值。但是,如果你之前做了 setBool:NO forKey:@FirstLaunch,然后再次使用 boolForKey:然后不使用 registerDefaults:的值。

If you've never written a value under the key FirstLaunch, then boolForKey:@"FirstLaunch" will use the value from registerDefaults:. However, if you did previously do setBool:NO forKey:@"FirstLaunch" and later ask for it again with boolForKey: then the value from registerDefaults: is not used.

这篇关于使用NSUserDefaults检测应用程序的初始启动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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