我在我的iOS应用程序中使用LinkedIn.我想保存访问令牌以供将来使用.
令牌属于非属性类型,无法直接保存在NSUserDefaults中.
我尝试使用NSKeyedArchiver,但我得到了输出:
Token===oauth_token "(null)" oauth_token_secret "(null)" oauth_verifier "(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,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];
我的编码或访问令牌有什么问题需要一些特殊技术来保存吗?请建议.
解决方法
这就是我拯救的方式.它对我有用.希望它有所帮助
- (void)accesstokenResult:(OAServiceTicket *)ticket didFinish:(NSData *)data { NSDictionary *dict; Nsstring *responseBody = [[Nsstring alloc] initWithData:data encoding:NSUTF8StringEncoding]; BOOL problem = ([responseBody rangeOfString:@"oauth_problem"].location != NSNotFound); if (problem) { DLog(@"Request access token Failed."); DLog(@"%@",responseBody); dict = [NSDictionary dictionaryWithObjectsAndKeys:@"0",@"success",@"Request access token Failed.",@"reason",nil]; } else { self.accesstoken = [[OAToken alloc] initWithHTTPResponseBody:responseBody]; [[NSUserDefaults standardUserDefaults] setobject:responseBody forKey:@"accesstoken"];//save here [[NSUserDefaults standardUserDefaults] setobject:[NSDate date] forKey:@"TokenRefreshDate"];//save here [[NSUserDefaults standardUserDefaults] synchronize]; dict = [NSDictionary dictionaryWithObjectsAndKeys:@"1",@"Login Successful",nil]; } // Notify parent and close this view [[NSNotificationCenter defaultCenter] postNotificationName:@"loginViewDidFinish" object:self userInfo:dict]; [self dismissViewControllerAnimated:YES completion:^{ }]; }
以这种方式使用保存的responseBody
self.accesstoken = [[NSUserDefaults standardUserDefaults] valueForKeyPath:@"accesstoken"]; NSURL *url = [NSURL URLWithString:@"http://api.linkedin.com/v1/people/~/connections:(id,first-name,last-name,headline,maiden-name,picture-url,formatted-name,location,positions,public-profile-url,specialties,num-connections,industry)"]; OAMutableuRLRequest *request = [[OAMutableuRLRequest alloc] initWithURL:url consumer:self.consumer token:[[OAToken alloc] initWithHTTPResponseBody:self.accesstoken] callback:nil signatureProvider:nil]; [request setValue:@"json" forHTTPHeaderField:@"x-li-format"]; OADataFetcher *fetcher = [[OADataFetcher alloc] init]; [fetcher fetchDataWithRequest:request delegate:self didFinishSelector:@selector(connectionsApiCallResult:didFinish:) didFailSelector:@selector(connectionsApiCallResult:didFail:) withId:0];
我希望这次我很清楚