I am using LinkedIn
in my iOS app. I want to save the access token for the future use.
The token is of non property type which cannot be saved in NSUserDefaults
directly.
I tried using NSKeyedArchiver
for this but I got the output:
Token===oauth_token "(null)" oauth_token_secret "(null)" oauth_verifier "(null)"
The text in the token is coming but the values are coming null.
Code snippet 1 :
-(void)saveData :(LOAToken *)token
{
NSFileManager *filemgr;
NSString *docsDir;
NSArray *dirPaths;
filemgr = [NSFileManager defaultManager];
// Get the documents directory
dirPaths = NSSearchPathForDirectoriesInDomains(
NSDocumentDirectory, NSUserDomainMask, YES);
docsDir = [dirPaths objectAtIndex:0];
// Build the path to the data file
NSString *dataFilePath = [[NSString alloc] initWithString: [docsDir
stringByAppendingPathComponent: @"data.archive"]];
[NSKeyedArchiver archiveRootObject:
token toFile:dataFilePath];
}
-(LOAToken *)GetToken
{
NSFileManager *filemgr;
NSString *docsDir;
NSArray *dirPaths;
filemgr = [NSFileManager defaultManager];
// Get the documents directory
dirPaths = NSSearchPathForDirectoriesInDomains(
NSDocumentDirectory, NSUserDomainMask, YES);
docsDir = [dirPaths objectAtIndex:0];
// Build the path to the data file
NSString *dataFilePath = [[NSString alloc] initWithString: [docsDir
stringByAppendingPathComponent: @"data.archive"]];
// Check if the file already exists
if ([filemgr fileExistsAtPath: dataFilePath])
{
LOAToken *token;
token = [NSKeyedUnarchiver
unarchiveObjectWithFile: dataFilePath];
return token;
}
return NULL;
}
I also tried to save like this but the result is same:
Token===oauth_token "(null)" oauth_token_secret "(null)" oauth_verifier "(null)"
Code snippet 2 :
NSData *myEncodedObject = [NSKeyedArchiver archivedDataWithRootObject:self.accessToken];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:myEncodedObject forKey:@"myEncodedObjectKey"];
[defaults synchronize];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSData *myEncodedObject = [defaults objectForKey:@"myEncodedObjectKey"];
LOAToken *obj = (LOAToken *)[NSKeyedUnarchiver unarchiveObjectWithData: myEncodedObject];
Is there anything wrong with my coding or Access Token need some special technique to save?Please Suggest.