当我用我的用户名 + 密码向后端询问令牌时,它返回:
{"token":"5KB2bTHSSLgBeZjMqvfa"}
这就是我迄今为止的工作。我得到令牌,它已成功保存。
- (void)requestFinished:(ASIHTTPRequest *)request
{
if ([request responseStatusCode] != 200) {
[self requestFailed:request];
}
else {
NSLog(@"%@", [request responseString]);
NSData * data = [request responseData];
NSDictionary *token = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error: nil];
// authentication successful, store token
NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
[defaults setValue:[token objectForKey:@"token"] forKey:@"auth_token"];
}
}
现在我想为每个下一个请求发送令牌,但我不知道如何。你能告诉我缺少什么吗?
NSURL *url = [NSURL URLWithString:@"http://localhost:3000/userDetails"];
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request setDelegate:self];
// AUTH TOKEN?
[request startAsynchronous];