cocoa pods集成
(写一句代码都是罪过)
腾讯开发品台
创建一个项目(做demo不需要提交审核)
info -> URL Types 设置回调 URL Schemes
identifier: tencentopenapi
URL: Schemes tencentAPPID
设置白名单(QQ全面的白名单)
<key>LSApplicationQueriesSchemes</key>
<array>
<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>mqzoneopensdk</string>
<string>mqqopensdkapiV3</string>
<string>mqqopensdkapiV3</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>mqzoneopensdk</string>
</array>
设置ATS
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
#import "AppDelegate.h"
#import <TencentOpenAPI/TencentOAuth.h>
@interface AppDelegate ()<TencentSessionDelegate>
///TencentOAuth *tencentOAuth
@property(nonatomic,strong)TencentOAuth *tencentOAuth;
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
_tencentOAuth = [[TencentOAuth alloc] initWithAppId:@"1105767298" andDelegate:self];
NSArray *permissions = [NSArray arrayWithObjects:kOPEN_PERMISSION_GET_INFO, kOPEN_PERMISSION_GET_USER_INFO, kOPEN_PERMISSION_GET_SIMPLE_USER_INFO, nil];
[_tencentOAuth authorize:permissions];
return YES;
}
//登录成功:
- (void)tencentDidLogin
{
if (_tencentOAuth.accessToken.length > 0) {
// 获取用户信息
[_tencentOAuth getUserInfo];
NSLog(@"_accessToken==%@",_tencentOAuth.accessToken);
} else {
NSLog(@"登录不成功 没有获取accesstoken");
}
}
// 获取用户信息
- (void)getUserInfoResponse:(APIResponse *)response {
if (response && response.retCode == URLREQUEST_SUCCEED) {
NSDictionary *userInfo = [response jsonResponse];
NSString *nickName = userInfo[@"nickname"];
NSLog(@"userInfo==%@",userInfo);
NSLog(@"nicName==%@",nickName);
// 后续操作...
} else {
NSLog(@"QQ auth fail ,getUserInfoResponse:%d", response.detailRetCode);
}
}
//非网络错误导致登录失败:
- (void)tencentDidNotLogin:(BOOL)cancelled {
if (cancelled) {
NSLog(@"用户取消登录");
} else {
NSLog(@"登录失败");
}
}
- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<NSString *,id> *)options {
return [TencentOAuth HandleOpenURL:url];
}
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url{
return [TencentOAuth HandleOpenURL:url];
}
@end
网友评论