SDK下载
添加依赖库
把下载的SDK拖进工程,在Build Phases下面的Link Binary With Libraries项中添加下面所需的依赖库:
Security.framework,
libiconv.dylib,
SystemConfiguration.framework,
CoreGraphics.Framework,
libsqlite3.dylib,
CoreTelephony.framework,
libstdc++.dylib,
libz.dylib
工程配置属性
在工程配置中的Build Settings一栏中找到Linking配置区,给Other Linker Flags配置项添加属性值:
-fobjc-arc
设置URL Scheme
修改 info.plist 文件 URL types 项中后面的URL Schemes内容为"tencent + appid",例如:tencent1234567890
(我这里设置了支付宝,qq,微信,微博,只用到QQ的话,就添加QQ的就ok)
添加URL Schemes白名单
在“Info.plist”中增加一个LSApplicationQueriesSchemes值,设置为array, 添加QQ和QQ空间需要的几个item(图片中我还添加了别的,只用QQ的话就填加下面这些就可以):
mqqapi
mqq
mqqOpensdkSSoLogin
mqqconnect
mqqopensdkdataline
mqqopensdkgrouptribeshare
mqqopensdkfriend
mqqopensdkapi
mqqopensdkapiV2
mqqopensdkapiV3
mqzoneopensdk
wtloginmqq
wtloginmqq2
mqqwpa
mqzone
mqzonev2
mqzoneshare
wtloginqzone
mqzonewx
mqzoneopensdkapiV2
mqzoneopensdkapi19
mqzoneopensdkapi
mqzoneopensdk
然后就就可以愉快的写代码了
创建了一个继承于NSObject的HHTencentSDK工具类,我自己简单的封装了一下以往需要QQ登录和分享用到的方法,需要别的功能的话,还是要去看官方的开发文档滴.
.h文件
#import <Foundation/Foundation.h>
#import <TencentOpenAPI/TencentOAuth.h>
#import <TencentOpenAPI/QQApiInterface.h>
@interface HHTencentSDK : NSObject
/**
实例化
*/
+(instancetype) shareInstance;
/**
初始化
*/
-(void)initSDK;
/**
请求调用qq登录
*/
- (void)sendQQLoginRequest;
/**
openURL
*/
+(BOOL)handleOpenURL:(NSURL *)url;
/**
分享到QQ好友
@param title 分享的文字标题
@param content 分享的文字内容
@param urlStr 分享的图片URL字符串
*/
+(QQApiSendResultCode)shareToQQWithTitle:(NSString *)title
Content:(NSString *)content
imageURLStr:(NSString *)urlStr;
/**
分享到QQ空间
@param title 分享的文字标题
@param content 分享的文字内容
@param urlStr 分享的图片URL字符串
*/
+(QQApiSendResultCode)shareToQZoneWithTitle:(NSString *)title
Content:(NSString *)content
imageURLStr:(NSString *)urlStr;
@end
.m文件
#import "HHTencentSDK.h"
@interface HHTencentSDK ()<TencentSessionDelegate>
@property (strong, nonatomic)TencentOAuth *tencentOAuth;
@end
@implementation HHTencentSDK
static NSURL * safeURL(NSString * origin) {
return [NSURL URLWithString:
[origin stringByAddingPercentEscapesUsingEncoding:
NSUTF8StringEncoding]];
}
static HHTencentSDK* _instance = nil;
//单例模式供外调用基类对象
+(instancetype) shareInstance{
static dispatch_once_t onceToken ;
dispatch_once(&onceToken, ^{
_instance = [[super allocWithZone:NULL] init] ;
}) ;
return _instance ;
}
+(id) allocWithZone:(struct _NSZone *)zone{
return [HHTencentSDK shareInstance] ;
}
-(id) copyWithZone:(NSZone *)zone{
return [HHTencentSDK shareInstance] ;
}
-(id) mutablecopyWithZone:(NSZone *)zone{
return [HHTencentSDK shareInstance] ;
}
#pragma mark - qq登录
- (void)sendQQLoginRequest{
NSArray* permissions = [NSArray arrayWithObjects:
kOPEN_PERMISSION_GET_USER_INFO,
kOPEN_PERMISSION_GET_SIMPLE_USER_INFO,
nil];
BOOL isSuccess = [_tencentOAuth authorize:permissions inSafari:NO];
if (isSuccess) {
NSLog(@"QQ调用登录成功");
}else{
NSLog(@"QQ调用登录失败");
}
}
//登录成功后的回调
- (void)tencentDidLogin {
if (_tencentOAuth.accessToken && 0 != [_tencentOAuth.accessToken length]){
BOOL isGetUserInfo = [_tencentOAuth getUserInfo];
if (isGetUserInfo) {
NSLog(@"获取用户QQ信息成功");
}
}else{
NSLog(@"登录不成功 没有获取accesstoken");
}
}
//获取用户信息回调
- (void)getUserInfoResponse:(APIResponse*) response{
NSLog(@"getUserInfoResponse-%@",response.jsonResponse);
if (response.jsonResponse==nil) {
return;
}
NSDictionary *dict = response.jsonResponse;
NSLog(@"%@",dict);
//昵称
NSLog(@"%@",dict[@"nickname"]);
//性别
NSLog(@"%@",dict[@"gender"]);
//openID:用户授权登录后对该用户的唯一标识
NSLog(@"%@",_tencentOAuth.openId);
//AccessToken凭证,用于后续访问各开放接口
NSLog(@"%@",_tencentOAuth.accessToken);
//头像
NSLog(@"%@",dict[@"figureurl_qq_2"]);
//获取到用户信息后根据需要保存在本地或上传到服务端,用作的用户信息
}
//登录失败后的回调(cancelled 代表用户是否主动退出登录)
- (void)tencentDidNotLogin:(BOOL)cancelled {
}
//登录时网络有问题的回调
- (void)tencentDidNotNetWork {
}
//初始化
-(void)initSDK{
//appID:从腾讯那里申请的你的应用的AppID
_tencentOAuth = [[TencentOAuth alloc] initWithAppId:QQ_AppID andDelegate:self];
}
//openURL
+(BOOL)handleOpenURL:(NSURL *)url {
return [TencentOAuth HandleOpenURL:url];
}
//分享到QQ好友
+(QQApiSendResultCode)shareToQQWithTitle:(NSString *)title
Content:(NSString *)content
imageURLStr:(NSString *)urlStr{
QQApiNewsObject *newsObj = [QQApiNewsObject objectWithURL:safeURL(urlStr)
title:title
description:content
previewImageURL:safeURL(urlStr)];
SendMessageToQQReq *req = [SendMessageToQQReq reqWithContent:newsObj];
return [QQApiInterface sendReq:req];
}
//分享到QQ空间
+(QQApiSendResultCode)shareToQZoneWithTitle:(NSString *)title
Content:(NSString *)content
imageURLStr:(NSString *)urlStr{
QQApiNewsObject *newsObj = [QQApiNewsObject objectWithURL:safeURL(urlStr)
title:title
description:content
previewImageURL:safeURL(urlStr)];
SendMessageToQQReq *req = [SendMessageToQQReq reqWithContent:newsObj];
return [QQApiInterface SendReqToQZone:req];
}
@end
设置
在AppDelegate.m文件中初始化
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
//qq
[[HHTencentSDK shareInstance] initSDK];
}
openURL方法中设置回调(这里一个是iOS9以下的系统调用的系统方法和一个是iOS9以上的系统调用的系统方法,都要设置)
#pragma mark - OpenURL回调结果
//iOS9-
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation{
[self handleOpenURLWithURLHost:url];
return YES;
}
//iOS9和9+
- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<NSString*, id> *)options{
[self handleOpenURLWithURLHost:url];
return YES;
}
- (void)handleOpenURLWithURLHost:(NSURL *)url{
NSLog(@"url.host:%@", url.host);
//qq(分享,登录)
if ([url.host isEqualToString:@"response_from_qq"]||[url.host isEqualToString:@"qzapp"]){
[HHTencentSDK handleOpenURL:url];
}
}
调用
分享是类方法,直接调用就好
/**
分享到QQ好友
@param title 分享的文字标题
@param content 分享的文字内容
@param urlStr 分享的图片URL字符串
*/
+(QQApiSendResultCode)shareToQQWithTitle:(NSString *)title
Content:(NSString *)content
imageURLStr:(NSString *)urlStr;
/**
分享到QQ空间
@param title 分享的文字标题
@param content 分享的文字内容
@param urlStr 分享的图片URL字符串
*/
+(QQApiSendResultCode)shareToQZoneWithTitle:(NSString *)title
Content:(NSString *)content
imageURLStr:(NSString *)urlStr;
登录需要先实例化再调用以下接口
/**
请求调用qq登录
*/
- (void)sendQQLoginRequest;
[[HHTencentSDK shareInstance] sendQQLoginRequest];