/**
* 授权地址
*
* @return UIWebView所要加载的url
*/
+ (NSURL *)authorizeURL
{
NSString *authStr = [NSString stringWithFormat:@"%@?client_id=%@&redirect_uri=%@&response_type=%@&display=%@",SINA_AUTHORIZE_URL,SINA_APP_KEY,SINA_REDIRECT_URI,@"code",@"mobile"];
return [NSURL URLWithString:authStr];
}
/**
* 判断有没有登录过,并且获得到的token有没有过期
*
* @return YES 有可用的token ,并且没有过期; NO 没有可用的token
*/
+ (BOOL)isAuthorized
{
NSString *accessToken = [[NSUserDefaults standardUserDefaults] objectForKey:SINA_ACCESS_TOKEN_KEY];
//读取沙盒中的Library/Preference/xx.plist
NSDate *expiresDate = [[NSUserDefaults standardUserDefaults] objectForKey:SINA_ACCESS_EXPIRES_IN_KEY];
if (expiresDate)
{
return ( NSOrderedDescending == [expiresDate compare:[NSDate date]] && accessToken);
}
return NO;
}
/**
* 获取AccessToken
*
* @param code 在webView的重定向地址中取得code
* @param isSuccess block 请求成功的回调
* BOOL isSuccess 请求是否成功 YES or NO
*/
+ (void)getAccessToken:(NSString *)code
success:(void (^) (BOOL isSuccess))isSuccess
{
NSMutableDictionary *dic = [[NSMutableDictionary alloc] init];
[dic setObject:SINA_APP_KEY forKey:@"client_id"];
[dic setObject:SINA_APP_SECRET forKey:@"client_secret"];
[dic setObject:@"authorization_code" forKey:@"grant_type"];
[dic setObject:code forKey:@"code"];
[dic setObject:SINA_REDIRECT_URI forKey:@"redirect_uri"];
[HttpBaseModel getDataResponseHostName:HOSTURL Path:@"oauth2/access_token" params:dic httpMethod:@"POST" onCompletion:^(NSData *responseData){
NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
NSLog(@"responseString = %@", responseString);
if ([responseString rangeOfString:@"access_token"].location != NSNotFound) {
isSuccess(YES);
SBJSON *json = [[SBJSON alloc] init];
NSError *error = nil;
NSDictionary *jsonDic = [json objectWithString:responseString error:&error];
[LPSinaEngine saveLoginInfo:jsonDic];
}
[responseString release];
} onError:^(NSError *error){
isSuccess(NO);
}];
}
/**
* 保存登录信息
*
* @param aDic 字典中有access_token,exoires_in,uid等信息
*/
+ (void)saveLoginInfo:(NSDictionary *)aDic
{
[[NSUserDefaults standardUserDefaults] setObject:[aDic objectForKey:@"access_token"] forKey:SINA_ACCESS_TOKEN_KEY];
NSDate *expiresDate = [NSDate dateWithTimeIntervalSinceNow:[[aDic objectForKey:@"expires_in"] intValue]];
[[NSUserDefaults standardUserDefaults] setObject:expiresDate forKey:SINA_ACCESS_EXPIRES_IN_KEY];
[[NSUserDefaults standardUserDefaults] setObject:[aDic objectForKey:@"uid"] forKey:SINA_USER_ID_KEY];
[[NSUserDefaults standardUserDefaults] synchronize];
}
/**
* 登出,清除当前账号的信息
*/
+ (void)logout
{
NSHTTPCookieStorage *storage = [NSHTTPCookieStorage sharedHTTPCookieStorage];
NSArray *cookies = [storage cookiesForURL:[NSURL URLWithString:@"https://api.weibo.com"]];
for (NSHTTPCookie *each in cookies)
[storage deleteCookie:each];
[[NSUserDefaults standardUserDefaults] removeObjectForKey:SINA_ACCESS_TOKEN_KEY];
[[NSUserDefaults standardUserDefaults] removeObjectForKey:SINA_ACCESS_EXPIRES_IN_KEY];
[[NSUserDefaults standardUserDefaults] removeObjectForKey:SINA_USER_ID_KEY];
[[NSUserDefaults standardUserDefaults] synchronize];
}