新浪SDK中没有说明如何保存access_token到本地,每次打开应用都需要进行授权登陆。用文件把信息保存到本地,当再次打开应用的时候,先判断文件是否存在,如果存在,将access_token取出再进行数据的下载等操作。
1.当进行授权后系统会自动调用下面的方法,在此方法保存数据到本地。
- (void)didReceiveWeiboResponse:(WBBaseResponse *)response
{
//保存信息到本地
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *path = [paths objectAtIndex:0];
NSString *filePath = [path stringByAppendingPathComponent:@"AuthData"];
NSDictionary *authData = [NSDictionary dictionaryWithDictionary:response.userInfo];
[authData writeToFile:filePath atomically:YES];
}
2.用户注销,即把文件移除掉,我用下面方法实现
-(void)logout:(UIBarButtonItem *)sender
{
NSString *filePath = [self filePath];
NSFileManager *manager = [NSFileManager defaultManager];
[manager removeItemAtPath:filePath error:nil];
}