QQ原生第三方登录 分享接入
1,首页需要去腾讯开发者平台注册应用 并且审核通过 获取appid
2,pod 'TencentOpenAPI-Unofficial', '~> 3.3.0'
#import <TencentOpenAPI/QQApiInterface.h>
#import <TencentOpenAPI/TencentOAuth.h>
在xcode URLtype 加入tencent这个是appid identifier :com.tencent
记得加入白名单
<key>LSApplicationQueriesSchemes</key>
<array>
<string>wechat</string>
<string>weixin</string>
<string>sinaweibohd</string>
<string>sinaweibo</string>
<string>sinaweibosso</string>
<string>weibosdk</string>
<string>weibosdk2.5</string>
<string>mqqapi</string>
<string>mqq</string>
<string>mqqOpensdkSSoLogin</string>
<string>mqqconnect</string>
<string>mqqopensdkdataline</string>
<string>mqqopensdkgrouptribeshare</string>
<string>mqqopensdkfriend</string>
<string>mqqopensdkapi</string>
<string>mqqopensdkapiV2</string>
<string>mqqopensdkapiV3</string>
<string>mqqopensdkapiV4</string>
<string>mqzoneopensdk</string>
<string>wtloginmqq</string>
<string>wtloginmqq2</string>
<string>mqqwpa</string>
<string>mqzone</string>
<string>mqzonev2</string>
<string>mqzoneshare</string>
<string>wtloginqzone</string>
<string>mqzonewx</string>
<string>mqzoneopensdkapiV2</string>
<string>mqzoneopensdkapi19</string>
<string>mqzoneopensdkapi</string>
<string>mqqbrowser</string>
<string>mttbrowser</string>
</array>
在appdelegate 回调
@property (nonatomic,strong) TencentOAuth *oauth;
<TencentSessionDelegate>
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
{
//注意: 初始化授权 开发者需要在这里填入自己申请到的 AppID
_oauth = [[TencentOAuth alloc] initWithAppId:QQ_Appid andDelegate:self];
}
- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options{
[TencentOAuth HandleOpenURL:url];
return YES;
}
然后在第三方登录界面
<TencentSessionDelegate>
- (void)getAuthWithUserInfoFromQQ{
self.tencentOAuth = [[TencentOAuth alloc]initWithAppId:@"appid" andDelegate:self];
NSMutableArray *permission = [@[] mutableCopy]; permission = [NSMutableArray arrayWithObjects:@"get_user_info",@"get_simple_userinfo",nil]; [self.tencentOAuth authorize:permission inSafari:NO];
}
#pragma mark --------- qq登录状态回调 TencentSessionDelegate------
- (void)tencentDidNotNetWork{
NSLog(@"没有网络,无法登录");
}
- (void)tencentDidLogin{
if (_tencentOAuth.accessToken){
NSLog(@"%@ == %@",_tencentOAuth.accessToken,_tencentOAuth.openId);
[self.tencentOAuth getUserInfo];
}else{
NSLog(@"登录失败!没有获取到accessToken");
}
}
/** * 登录失败后的回调 */
- (void)tencentDidNotLogin:(BOOL)cancelled{
if (cancelled){
NSLog(@"用户取消登录");
}else{
NSLog(@"登录失败");
}
}
- (void)getUserInfoResponse:(APIResponse *)response
{
NSLog(@"%@",response.jsonResponse);
}
分享
QQApiTextObject *txtObj = [QQApiTextObject objectWithText:@"share text"];
SendMessageToQQReq *req = [SendMessageToQQReq reqWithContent:txtObj];
dispatch_async(dispatch_get_main_queue(), ^{
// [QQApiInterface SendReqToQZone:req];
QQApiSendResultCode sent = [QQApiInterface SendReqToQZone:req];//分享到QQ空间
[self handleSendResult:sent];
qq好友
QQApiTextObject *txtObj = [QQApiTextObject objectWithText:@"share text"];
SendMessageToQQReq *req = [SendMessageToQQReq reqWithContent:txtObj];
dispatch_async(dispatch_get_main_queue(), ^{
// [QQApiInterface sendReq:req];
QQApiSendResultCode sent = [QQApiInterface sendReq:req];//分享到QQ空间
[self handleSendResult:sent];
});
- (void)handleSendResult:(QQApiSendResultCode)sendResult
{
switch (sendResult)
{
case EQQAPIAPPNOTREGISTED:
{
UIAlertView *msgbox = [[UIAlertView alloc] initWithTitle:@"温馨提示" message:@"App未注册" delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:nil];
[msgbox show];
break;
}
case EQQAPIMESSAGECONTENTINVALID:
case EQQAPIMESSAGECONTENTNULL:
case EQQAPIMESSAGETYPEINVALID:
{
UIAlertView *msgbox = [[UIAlertView alloc] initWithTitle:@"温馨提示" message:@"发送参数错误" delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:nil];
[msgbox show];
break;
}
case EQQAPIQQNOTINSTALLED:
{
UIAlertView *msgbox = [[UIAlertView alloc] initWithTitle:@"温馨提示" message:@"您的设备未安装手机QQ" delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:nil];
[msgbox show];
break;
}
case EQQAPIQQNOTSUPPORTAPI:
{
UIAlertView *msgbox = [[UIAlertView alloc] initWithTitle:@"温馨提示" message:@"您的设备未安装手机QQ" delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:nil];
[msgbox show];
break;
}
case EQQAPISENDFAILD:
{
UIAlertView *msgbox = [[UIAlertView alloc] initWithTitle:@"温馨提示" message:@"发送失败" delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:nil];
[msgbox show];
break;
}
case EQQAPIVERSIONNEEDUPDATE:
{
UIAlertView *msgbox = [[UIAlertView alloc] initWithTitle:@"温馨提示" message:@"当前QQ版本太低,需要更新" delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:nil];
[msgbox show];
break;
}
default:
{
break;
}
}
}