如何在nsuserdefaults中保存用户ID?

时间:2016-01-19 09:52:36

标签: ios objective-c swift nsuserdefaults

当我使用我的电子邮件和密码登录我的应用程序时,我当时成功收到回复,要将我的用户ID保存到nsuserdefaults,我该怎么做?

这是我的代码:

-(void)signInMethod
{
    NSString *accessKey = @"akjsda9s8dadandl4";
    NSData *returnData;
    returnData=[[WebServices alloc]loginTest:emailTxtFld.text andPassword:pswdTxtFld.text andSESSION_ACCESS_KEY:accessKey];
    if (returnData.length>0)
    {
        NSError *error;
        NSDictionary *res=[NSJSONSerialization JSONObjectWithData:returnData options:kNilOptions error:&error];
        NSLog(@"SignIn response is %@",res);
        NSString *msg = [res valueForKey:@"userid"];
        NSLog(@"%@",msg);
    }
}

从上面的代码WebServices是我的服务类。如何保存该用户ID?

3 个答案:

答案 0 :(得分:6)

商品

用于存储String NSUserDefaults,您可以直接使用setObject

[[NSUserDefaults standardUserDefaults] setObject:[res valueForKey:@"userid"] forKey:@"userid"];
[[NSUserDefaults standardUserDefaults] synchronize];

<强>提取

用于检索String中的NSUserDefaults,您可以直接使用objectForKey

NSString *Value = [[NSUserDefaults standardUserDefaults]objectForKey:@"userid"];

删除UserdefaultValue

如果您的工作结束,您想要清除userdefault的内存,请使用以下代码

[[NSUserDefaults standardUserDefaults] removeObjectForKey:@"userid"];

<强>夫特

商品

NSUserDefaults.standardUserDefaults().setObject(res["userid"], forKey: "userid")

<强>提取

 var Value: String = NSUserDefaults.standardUserDefaults().objectForKey("userid")

删除UserdefaultValue

NSUserDefaults.standardUserDefaults().removeObjectForKey("userid")

答案 1 :(得分:2)

 -(void)signInMethod
{
NSString *accessKey = @"akjsda9s8dadandl4";
NSData *returnData;
returnData=[[WebServices alloc]loginTest:emailTxtFld.text andPassword:pswdTxtFld.text andSESSION_ACCESS_KEY:accessKey];
if(returnData.length>0)
{
    NSError *error;
    NSDictionary *res=[NSJSONSerialization JSONObjectWithData:returnData options:kNilOptions error:&error];
    NSLog(@"SignIn response is %@",res);
    NSString *msg = [res valueForKey:@"userid"];

   [[NSUserDefaults standardUserDefaults] setObject:msg forKey:@"userid"];
   [[NSUserDefaults standardUserDefaults] synchronize];


    NSLog(@"%@",msg);
}
}

要重新使用

NSString *UDID = [[NSUserDefaults standardUserDefaults] objectForKey:@"userid"];

答案 2 :(得分:1)

使用此代码保存电子邮件和密码,并使用userIdto NSUserDefault。

 -(void)signInMethod
{
NSString *accessKey = @"akjsda9s8dadandl4";
NSData *returnData;
returnData=[[WebServices alloc]loginTest:emailTxtFld.text andPassword:pswdTxtFld.text andSESSION_ACCESS_KEY:accessKey];
if(returnData.length>0)
{
    NSError *error;
    NSDictionary *res=[NSJSONSerialization JSONObjectWithData:returnData options:kNilOptions error:&error];
    NSLog(@"SignIn response is %@",res);
    NSString *msg = [res valueForKey:@"userid"];

    [[NSUserDefaults standardUserDefaults] setObject:emailTxtFld.text forKey:@"email"];
    [[NSUserDefaults standardUserDefaults] setObject:pswdTxtFld.text forKey:@"password"];
    [[NSUserDefaults standardUserDefaults] setObject:msg forKey:@"userid"];
    [[NSUserDefaults standardUserDefaults] synchronize];

    NSLog(@"%@",msg);
}
}