ios配置QQSdk,本文只讲解配置的核心部分,包括登录和分享,详细方法请看官网
1.配置Xcode项目
1.1配置Associated Domains,不要前面的https://,后面到com就行不用斜杠/
1.2配置文件apple-app-site-association
1.2.1details中配置数组是为了配置多个app,从而多个app可以使用一个文件即可;
1.2.2其中paths为可以打开app的路径,配置格式如下,因为qq回调app的时候路径就是这样,这样配置只有链接为:https://Associated Domains/qq_conn/你的qq appid/...的时候才能打开app;
1.2.3 该文件放到服务器(Associated Domains)的根目录下或者根目录的.well-known目录下,配置成功在浏览器输入https://Associated Domains/apple-app-site-association可以下载该文件
(可参考微信配置:https://help.wechat.com/apple-app-site-association)
{
"applinks": {
"apps": [],
"details": [{
"appID": "team id.bundle id",
"paths": ["/qq_conn/你的qq appid/*"]
}]
}
}
1.3qq互联应用配置,universal link 只需为你xcode里面配置的Associated Domains,因为qq回调到qq的时候会自动带上路径:/qq_conn/你的qq appid/
1.4xcode项目导入qqSDK,具体步骤看官方即可,注意:
1.4.1导入头文件的时候只能导入,否则会报错:
#import <TencentOpenAPI/TencentOpenApiUmbrellaHeader.h>
1.4.2注册app
self.oauth = [[TencentOAuth alloc] initWithAppId:kQQAppid andUniversalLink:@"https://Associated Domains/qq_conn/你的qq appid" andDelegate:self];
1.4.3发起授权登录和分享
// 登录
if ([TencentOAuth iphoneQQInstalled]) {
NSArray *permissions = [NSArray arrayWithObjects:
kOPEN_PERMISSION_GET_USER_INFO,
kOPEN_PERMISSION_GET_SIMPLE_USER_INFO,
kOPEN_PERMISSION_ADD_ALBUM,
nil];
[self.oauth authorize:permissions];
}
// 分享
QQApiTextObject *txtObj = [QQApiTextObject objectWithText:text];
SendMessageToQQReq *req = [SendMessageToQQReq reqWithContent:txtObj];
//将内容分享到
if (type == 0) {
QQApiSendResultCode sent = [QQApiInterface sendReq:req];
}
else
{
[txtObj setCflag:kQQAPICtrlFlagQZoneShareOnStart];
QQApiSendResultCode sent = [QQApiInterface SendReqToQZone:req];
}
1.4.4处理回调
//需要在appDelegate.m文件中实现如下:
// Universal Links打开app
-(BOOL)application:(UIApplication *)application continueUserActivity:(NSUserActivity *)userActivity restorationHandler:(void(^)(NSArray<id<UIUserActivityRestoring>> * __nullable restorableObjects))restorationHandler API_AVAILABLE(ios(8.0))
{
// qqsdk配置回调
if ([userActivity.activityType isEqualToString:NSUserActivityTypeBrowsingWeb]) {
if ([TencentOAuth CanHandleUniversalLink:userActivity.webpageURL]) {
// qq分享
BOOL flag = [QQApiInterface handleOpenUniversallink:url delegate:self];
if (!flag) {
// qq登录
return [TencentOAuth HandleUniversalLink:url];
}
return flag;
}
}
// 微信sdk回调 如果还有其他的需要加入判断
/**
if ([userActivity.activityType isEqualToString:NSUserActivityTypeBrowsingWeb]) {
if ([userActivity.webpageURL.description containsString:WeChatAPPKEY]) {
return [WXApi handleOpenUniversalLink:userActivity delegate:self];
}
}
*/
return [WXApi handleOpenUniversalLink:userActivity delegate:self];
}
// ios9以后调用
- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<UIApplicationOpenURLOptionsKey, id> *)options NS_AVAILABLE_IOS(9_0){
if ([TencentOAuth CanHandleOpenURL:url]) {
// qq登录
return [TencentOAuth HandleOpenURL:url] ;
}
else if ([url.absoluteString rangeOfString:@"response_from_qq"].location != NSNotFound) {
// qq分享
return [QQApiInterface handleOpenURL:url delegate:[DJQQManager manager]];
}
// 微信登录
// else if ([url.host isEqualToString:@"oauth"]) {
return [WXApi handleOpenURL:url delegate:self];
// }
return true;
}
// 分享回调
#pragma mark - QQApiInterfaceDelegate
-(void)onResp:(QQBaseResp *)resp
{
if (resp.type == ESENDMESSAGETOQQRESPTYPE) { // 分享
if ([resp isKindOfClass:[SendMessageToQQResp class]]) {
SendMessageToQQResp *response = (SendMessageToQQResp *)resp;;
if ([response.result isEqualToString:@"0"]) {
NSLog(@"分享成功");
[self showToast:@"分享成功"];
} else {
NSLog(@"分享失败");
[self showToast:@"分享失败"];
}
}
}
}
// 授权登录回调
#pragma mark - TencentSessionDelegate
/**
* 登录成功后的回调
*/
- (void)tencentDidLogin
{
if (self.oauth.accessToken.length > 0) {
// 获取用户信息
[self.oauth getUserInfo];
} else {
NSLog(@"登录不成功 没有获取accesstoken");
}
}