一、QQ三方登录的准备工作
1.下载最新的SDK
http://wiki.open.qq.com/wiki/mobile/SDK下载
doc: 为说明文档, 方法参数说明。
sample 这个是简单的demo
TencentOpenApi_IOS_Bundle.bundle 资源文件包
TencentOpenApi.framework 核心开发框架
腾讯开发平台上没有QQ三方登录的相关文档,可以搜下QQ互联,那上面比较详细。
2.添加依赖
最简单的方法:在sample里,打开项目,照着这个项目里的依赖抄上去。

20160617162349630.jpeg
3.在工程配置中的“Build Settings”一栏中找到“Linking”配置区,给“Other Linker Flags”配置项添加属性值“-fobjc-arc”

20160617162625160.jpeg
4.选择项目,targets,点击info,点开下面的URL Types,点击加号
在URL Schemes里填写tencent + aped。
5.在info.plist文件中加入 LSApplicationQueriesSchemes
这个也是从sample里复制粘贴的。

20160617175053663.jpeg
二、QQ三方登录在代码中的实现
1.引入头文件,并遵循代理
#import <TencentOpenAPI/QQApiInterface.h>
#import <TencentOpenAPI/TencentOAuth.h>
@interface AppDelegate ()<QQApiInterfaceDelegate>
@end
2.在添加跳转的请求方法
// 从微信端打开第三方APP会调用此方法,此方法再调用代理的onResp方法
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url
{
[TencentOAuth HandleOpenURL:url];
[QQApiInterface handleOpenURL:url delegate:self];
return YES;
}
- (BOOL)application:(UIApplication*)application openURL:(NSURL *)url sourceApplication:(NSString*)sourceApplication annotation:(id)annotation{
[QQApiInterface handleOpenURL:url delegate:self];
[TencentOAuth HandleOpenURL:url];
return YES;
}
3.QQ登录按钮响应事件对应的控制器引入头文件,准守协议
#import <TencentOpenAPI/QQApiInterface.h>
#import <TencentOpenAPI/TencentOAuth.h>
#import <TencentOpenAPI/QQApiInterfaceObject.h>
@interface ViewController ()<TencentSessionDelegate>
{
TencentOAuth *_tencentOAuth;
NSMutableArray *_permissionArray; //权限列表
}
@end
4.点击QQ登录按钮之后的代码
- (IBAction)qqLogin:(id)sender {
_tencentOAuth = [[TencentOAuth alloc] initWithAppId:@"1105593761" andDelegate:self];
_permissionArray = [NSMutableArray arrayWithObjects:
kOPEN_PERMISSION_GET_SIMPLE_USER_INFO,
nil];
[_tencentOAuth authorize:_permissionArray];
}
#pragma TencentSessionDelegate
/**
* [该逻辑未实现]因token失效而需要执行重新登录授权。在用户调用某个api接口时,如果服务器返回token失效,则触发该回调协议接口,由第三方决定是否跳转到登录授权页面,让用户重新授权。
* \param tencentOAuth 登录授权对象。
* \return 是否仍然回调返回原始的api请求结果。
* \note 不实现该协议接口则默认为不开启重新登录授权流程。若需要重新登录授权请调用\ref TencentOAuth#reauthorizeWithPermissions: \n注意:重新登录授权时用户可能会修改登录的帐号
*/
- (BOOL)tencentNeedPerformReAuth:(TencentOAuth *)tencentOAuth{
return YES;
}
- (BOOL)tencentNeedPerformIncrAuth:(TencentOAuth *)tencentOAuth withPermissions:(NSArray *)permissions{
// incrAuthWithPermissions是增量授权时需要调用的登录接口
// permissions是需要增量授权的权限列表
[tencentOAuth incrAuthWithPermissions:permissions];
return NO; // 返回NO表明不需要再回传未授权API接口的原始请求结果;
// 否则可以返回YES
}
-(void)tencentDidLogin{
NSLog(@"----ok-----");
/** Access Token凭证,用于后续访问各开放接口 */
if (_tencentOAuth.accessToken) {
//获取用户信息。 调用这个方法后,qq的sdk会自动调用
//- (void)getUserInfoResponse:(APIResponse*) response
//这个方法就是 用户信息的回调方法。
[_tencentOAuth getUserInfo];
}else{
NSLog(@"accessToken 没有获取成功");
}
}
//-(NSArray *)getAuthorizedPermissions:(NSArray *)permissions withExtraParams:(NSDictionary *)extraParams{
// NSLog(@"----%@********%@-----",permissions,extraParams);
// return permissions;
//}
- (void)getUserInfoResponse:(APIResponse*) response{
NSLog(@"*********");
NSLog(@" response %@",response);
NSLog(@"*********");
}
-(void)tencentDidNotLogin:(BOOL)cancelled{
if (cancelled) {
NSLog(@" 用户点击取消按键,主动退出登录");
}else{
NSLog(@"其他原因, 导致登录失败");
}
}
-(void)tencentDidNotNetWork{
NSLog(@"没有网络了, 怎么登录成功呢");
}