1,首頁須要去騰訊開發者平臺註冊應用 而且審覈經過 獲取appidjson
2,pod 'TencentOpenAPI-Unofficial', '~> 3.3.0'api
#import <TencentOpenAPI/QQApiInterface.h>xcode
#import <TencentOpenAPI/TencentOAuth.h>網絡
在xcode URLtype 加入tencent這個是appid identifier :com.tencentapp
記得加入白名單async
<key>LSApplicationQueriesSchemes</key>ide
<array>atom
<string>wechat</string>url
<string>weixin</string>spa
<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;
}
}
}