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

时间:2022-01-26 20:11:34

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

我在这里构建代码

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.Can anyone please help me to solve the issue.

log cat中没有错误但是不保存首选项。我需要在用户打开应用程序时保存首选项并加载。我在代码中做错了什么。任何人都可以帮我解决问题。

1 个解决方案

#1


5  

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 ..看看这是类函数,所以你必须使用你的类名称来调用它。将它写在你的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
    }
}

#1


5  

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 ..看看这是类函数,所以你必须使用你的类名称来调用它。将它写在你的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
    }
}