1,打开刚才新建的工程,重写appdelegate的两个方法
重写之前导入头文件
#import <TencentOpenAPI/TencentOAuth.h>
openURL:
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation{
return [TencentOAuth HandleOpenURL:url];
}
handleOpenURL:
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url{
return [TencentOAuth HandleOpenURL:url];
}
tencentOAuth=[[TencentOAuthalloc]initWithAppId:@"你的appid"andDelegate:self];
3,设置权限列表
//4,设置需要的权限列表,此处尽量使用什么取什么。
permissions= [NSArrayarrayWithObjects:@"get_user_info",@"get_simple_userinfo",@"add_t",nil];
4,登陆
[tencentOAuth authorize:permissionsinSafari:NO];
5,在代码中实现 TencentSessionDelegate 方法
#pragma mark -- TencentSessionDelegate
//登陆完成调用
- (void)tencentDidLogin
{
resultLable.text =@"登录完成";
if (tencentOAuth.accessToken &&0 != [tencentOAuth.accessTokenlength])
{
// 记录登录用户的OpenID、Token以及过期时间
tokenLable.text =tencentOAuth.accessToken;
}
else
{
tokenLable.text =@"登录不成功没有获取accesstoken";
}
}
//非网络错误导致登录失败:
-(void)tencentDidNotLogin:(BOOL)cancelled
{
NSLog(@"tencentDidNotLogin");
if (cancelled)
{
resultLable.text =@"用户取消登录";
}else{
resultLable.text =@"登录失败";
}
}
// 网络错误导致登录失败:
-(void)tencentDidNotNetWork
{
NSLog(@"tencentDidNotNetWork");
resultLable.text =@"无网络连接,请设置网络";
}
- (void)didReceiveMemoryWarning {
[superdidReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
以上方法基本上就实现了登陆,下来我们得考虑登陆成功之后如何获取用户信息
其实方法很简单我们在登陆成功的方法里面调用
[tencentOAuth getUserInfo];
然后系统会调用一个方法(我们需要提前实现)
-(void)getUserInfoResponse:(APIResponse *)response
{
NSLog(@"respons:%@",response.jsonResponse);
}