如何在nsuserdefaults中保存userid ?(复制)

时间:2022-12-14 07:35:25

This question already has an answer here:

这个问题已经有了答案:

When I login to my app with my email and password I am getting a response successfully at that time, to save my userid into nsuserdefaults, how can I do that?

当我用我的电子邮件和密码登录到我的应用程序时,我得到了一个成功的响应,为了将我的userid保存到nsuserdefaults,我怎么做呢?

Here is my code:

这是我的代码:

-(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);
    }
}

From the above code WebServices is my Service Class. How can I save that userid?

从上面的代码中,WebServices是我的服务类。如何保存该userid?

3 个解决方案

#1


1  

Use this code to save email and password and userIdto NSUserDefault.

使用此代码保存电子邮件和密码,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);
}
}

#2


6  

Store

商店

for store the String in NSUserDefaults you can directly use setObject

对于在NSUserDefaults中存储字符串,您可以直接使用setObject

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

Retrieve

检索

for Retrieve the String in NSUserDefaults you can directly use objectForKey

对于在NSUserDefaults中检索字符串,您可以直接使用objectForKey

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

Remove UserdefaultValue

删除UserdefaultValue

if your work is over you want to clear the memory of userdefault use the following code

如果您的工作已经结束,您希望清除userdefault的内存,请使用以下代码

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

Swift

斯威夫特

Store

商店

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

Retrieve

检索

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

Remove UserdefaultValue

删除UserdefaultValue

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

#3


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);
}
}

To retrive use

能够利用使用

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

#1


1  

Use this code to save email and password and userIdto NSUserDefault.

使用此代码保存电子邮件和密码,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);
}
}

#2


6  

Store

商店

for store the String in NSUserDefaults you can directly use setObject

对于在NSUserDefaults中存储字符串,您可以直接使用setObject

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

Retrieve

检索

for Retrieve the String in NSUserDefaults you can directly use objectForKey

对于在NSUserDefaults中检索字符串,您可以直接使用objectForKey

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

Remove UserdefaultValue

删除UserdefaultValue

if your work is over you want to clear the memory of userdefault use the following code

如果您的工作已经结束,您希望清除userdefault的内存,请使用以下代码

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

Swift

斯威夫特

Store

商店

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

Retrieve

检索

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

Remove UserdefaultValue

删除UserdefaultValue

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

#3


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);
}
}

To retrive use

能够利用使用

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