如何知道应用程序首先在Mac上运行

如何知道应用程序首先在Mac上运行

本文介绍了如何知道应用程序首先在Mac上运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Windows上,我们可以将值写入注册表以了解这一点

On Windows, we can write values into registry to know that

但是如何知道我的应用程序是否是首次在Mac上运行?我需要执行一些初始化任务.

but how can I know if my application is the first time it runs on a mac? I need to perform some initialization task.

谢谢

推荐答案

您正在寻找类NSUserDefaults(请参见 Apple文档)

You are looking for the class NSUserDefaults (see Apple Documentation)

例如:

#define kAlreadyBeenLaunched @"AlreadyBeenLaunched"

    if (! [[NSUserDefaults standardUserDefaults] boolForKey:kAlreadyBeenLaunched]) {
        // This is our very first launch

        // Setting userDefaults for next time
        [[NSUserDefaults standardUserDefaults] setValue:[NSNumber numberWithBool:YES] forKey:kAlreadyBeenLaunched];

        // Do your first time stuff
        //<##>
    }

您将使用相同的类来保存和检索用户首选项.

You will use the same class to save and retrieve user preferences.

此值将保存在~/Library/Preferences/<your_bundle_id>.plist中.通过查看文件来进行调试很有用,但是您不应依赖于代码中的实现细节.

This values will be saved in ~/Library/Preferences/<your_bundle_id>.plist. This is useful to know for debugging, by looking at the file, but you should not rely on this implementation detail in your code.

这篇关于如何知道应用程序首先在Mac上运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 14:44