User558 User558 - 6 months ago 18
Swift Question

how to save the userid in nsuserdefaults?

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?

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?

Answer

Use this code to save email and password and 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);
}
}