我LinkedIn
在我的 iOS 应用程序中使用。我想保存访问令牌以备将来使用。
令牌是非属性类型,不能NSUserDefaults
直接保存。我尝试使用NSKeyedArchiver
它,但我得到了输出:
Token===oauth_token "(null)" oauth_token_secret "(null)" oauth_verifier "(null)"
令牌中的文本即将到来,但值将变为 null。
代码片段 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;
}
我也尝试过这样保存,但结果是一样的:
Token===oauth_token "(null)" oauth_token_secret "(null)" oauth_verifier "(null)"
代码片段 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];
我的编码有什么问题或访问令牌需要一些特殊技术来保存吗?请建议。