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];
コーディングに何か問題がありますか、それともアクセス トークンを保存するために特別なテクニックが必要ですか?提案してください。