简体   繁体   English

如何保存iPhone应用程序的用户首选项?

[英]How can I save user preferences for my iPhone application?

I am building one Application and need to enter the access credentials (Org code, accesscode) before opening the application. 我正在构建一个应用程序,需要在打开应用程序之前输入访问凭据(组织代码,访问代码)。 when the user enter the access permissions for the first time then we need to save the preferences. 当用户首次输入访问权限时,我们需要保存首选项。 When the user open the app again, automatically he need to continue with application but not with access credentials. 当用户再次打开该应用程序时,他需要自动继续该应用程序,但不需要访问凭证。

And I build the code here 我在这里构建代码

There are no errors in log cat but preferences are not saving. 日志目录中没有错误,但首选项未保存。 I need to save the preferences and load when the application opens by the user. 我需要保存首选项并在用户打开应用程序时加载。 Am I doing anything wrong in the code? 我在代码中做错了吗?

This common function is to add your data to NSUserDefaults.. Look this is Class functions so You have to call this using your class name.. Write this in your AppDelegate implementation file.. 这个常见的功能是将数据添加到NSUserDefaults。.看起来这是Class函数,因此您必须使用您的类名来调用它。.在AppDelegate实现文件中编写它。

+(void)addToNSUserDefaults:(id)pObject forKey:(NSString*)pForKey
{
    NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
    [defaults setObject:pObject forKey:pForKey];
    [defaults synchronize]; 
}

This is to get data from NSUserDefaults.. 这是从NSUserDefaults获取数据。

+(id)getFromNSUserDefaults:(NSString*)pForKey
{
    id pReturnObject;
    NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
    pReturnObject = [defaults valueForKey:pForKey];
    return pReturnObject;
}

When you start application, Just Check this in this method of AppDelegate, 启动应用程序时,只需在AppDelegate的此方法中进行检查,

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{
    NSString *strtmp = [AppDelegate getFromNSUserDefaults:YOUR_KEY];

    if(strtmp == nil)
    {
        //User have to enter data to save in User Defaults
    }
    else
    {
        //Application Starts without entering Code, as you have it in your UserDefaults
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何保存iPhone应用程序的用户首选项? - How do I save user preferences for my iPhone app? 如何在不更改iPhone上的语言首选项的情况下,使用Xcode以另一种语言测试我的iOS应用程序? - How can I test my iOS application, using Xcode, in another language without changing my language preferences on my iPhone? 如何备份/恢复应用程序的用户首选项 - How do I backup/restore the user preferences for my application 如何存储应用程序首选项(iPhone) - How to store application preferences (iphone) 如何在没有服务器的情况下保存iPhone应用程序数据? - How do I save my iPhone application data without a server? 如何将iPhone库与iPhone应用程序连接? - How can I connect iphone library with my iPhone application? 如何请求我的iPhone应用程序用户提交有关该应用程序的评论? - How can I request the user of my iPhone app to submit a review about the application? iPhone - 如何从应用程序中保存用户设置? - iPhone - how to save user settings from application? 我可以让用户在“应用程序偏好设置”中选择多个标题吗? - Can I let user to multi select some titles in Application Preferences? 如何在iphone应用程序中保存一个简单的计数器,以便可以递增is并且更改将在应用程序启动之间保存? - How can I save a simple counter in an iphone application such that I can increment is and the change will be saved between application launches?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM