iOS QQ 登录

QQSDK

看官网的文档,确实让人头疼的一件事,说是两个资源文件,就找到了一个(TencentOpenAPI.framework),Demo中也没有找到.

接下来具体实现:

导入库:

 添加SDK依赖的系统库文件。分别是“Security.framework”,“libiconv.dylib”,“SystemConfiguration.framework”,“CoreGraphics.Framework”、“libsqlite3.dylib”、“CoreTelephony.framework”、“libstdc++.dylib”、“libz.dylib”

添加URL Types URL_Schemes = tencent+appid

发现配置完方法后不走回调.主要有一下两点,

1.添加白名单

<key>LSApplicationQueriesSchemes</key>
<array>
<string>mqq</string>
<string>mqqapi</string>
<string>mqqwpa</string>
<string>mqqbrowser</string>
<string>mttbrowser</string>
<string>mqqOpensdkSSoLogin</string>
<string>mqqopensdkapiV2</string>
<string>mqqopensdkapiV3</string>
<string>mqqopensdkapiV4</string>
<string>wtloginmqq2</string>
<string>mqzone</string>
<string>mqzoneopensdk</string>
<string>mqzoneopensdkapi</string>
<string>mqzoneopensdkapi19</string>
<string>mqzoneopensdkapiV2</string>
<string>mqqapiwallet</string>
<string>mqqopensdkfriend</string>
<string>mqqopensdkdataline</string>
<string>mqqgamebindinggroup</string>
<string>mqqopensdkgrouptribeshare</string>
<string>tencentapi.qq.reqContent</string>
<string>tencentapi.qzone.reqContent</string>
</array>
2 回调后的设置

AppDelegate.m 中引入(最好设置成全局的,登录时还要用到)

#import <TencentOpenAPI/QQApiInterface.h>
#import <TencentOpenAPI/TencentOAuth.h>
-(BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
if ([url.host isEqualToString:@"qzapp"]) {
 
[QQApiInterface handleOpenURL:url delegate:(id<QQApiInterfaceDelegate>)[QQApiNewsObject class]];
 
return [TencentOAuth HandleOpenURL:url];
 
}
 
return YES;
}

登录VC 中实现

引入代理方法: TencentSessionDelegate

//声明变量
@property (nonatomic,strong) TencentOAuth * tencentOAuth;
//判断是否装QQ - (void)lookQQAuth { self.tencentOAuth = [[TencentOAuth alloc]initWithAppId:@"AppId" andDelegate:self]; BOOL isInstallQQ = [TencentOAuth iphoneQQInstalled]; if (isInstallQQ == NO) { [self.qqLogin setHidden:YES]; } } //登录函数: - (void)qqLoginBtnAction { NSString *appid = TencentAppid; _tencentOAuth = [[TencentOAuth alloc] initWithAppId:appid andDelegate:self]; NSMutableArray * permission = [NSMutableArray arrayWithObjects:@[kOPEN_PERMISSION_GET_INFO, kOPEN_PERMISSION_GET_USER_INFO, kOPEN_PERMISSION_GET_SIMPLE_USER_INFO],nil]; [_tencentOAuth authorize:permission inSafari:NO]; } #pragma mark --------- qq登录状态回调 TencentSessionDelegate------ //登录成功: - (void)tencentDidLogin { if (_tencentOAuth.accessToken.length > 0) { // 获取用户信息 [_tencentOAuth getUserInfo]; [_qqInfoDict setValue:_tencentOAuth.openId forKey:@"openid"]; } else { NSLog(@"登录不成功 没有获取accesstoken"); } } //非网络错误导致登录失败: - (void)tencentDidNotLogin:(BOOL)cancelled { if (cancelled) { NSLog(@"用户取消登录"); } else { NSLog(@"登录失败"); } } - (void)tencentDidNotNetWork { NSLog(@"检查网络"); } // 获取用户信息 - (void)getUserInfoResponse:(APIResponse *)response { if (response && response.retCode == URLREQUEST_SUCCEED) { // 字符串 转字典 取用户信息 NSDictionary *userInfo = [NSString dictionaryWithJsonString:response.message]; // 开始登录(请求服务器) [self qqLoginRequest]; } else { NSLog(@"QQ auth fail ,getUserInfoResponse:%d", response.detailRetCode); } }
 
原文地址:https://www.cnblogs.com/xingsmile/p/10937716.html