本篇文档介绍在 Wilddog Auth 中如何使用 QQ 对用户进行身份认证。
前期准备
- 在控制面板中创建应用。请参考 控制面板-创建应用。
- 在 QQ 互联-我的应用 中,获取应用的 APP ID 和 APP KEY。
- 在控制面板 身份认证—登录方式 中打开 QQ 登录方式,配置 QQ 帐号 APP ID 和 APP KEY。
实现 QQ 登录
1.安装 Wilddog Auth SDK:
将以下 pod 包含在你的 Podfile 中:
安装 SDK:
2.创建 Wilddog Auth 实例:
Objective-C
Swift
WDGOptions *option = [[WDGOptions alloc] initWithSyncURL:@"https://<your-wilddog-appid>.wilddogio.com"]; [WDGApp configureWithOptions:option];
|
let options = WDGOptions.init(syncURL: "https://<your-wilddog-appid>.wilddogio.com") WDGApp.configure(with: options)
|
3.参考 QQ API 调用说明 将 QQ 登录集成到应用中。当初始化 TencentOAuth 对象时,设置 delegate 来接收登录和登出事件:
Objective-C
Swift
_qqOAuth = [[TencentOAuth alloc] initWithAppId:@"your QQ App ID" andDelegate:self]; NSArray *permissions = [NSArray arrayWithObjects:@"get_user_info", @"get_simple_userinfo", @"add_t", nil]; [_qqOAuth authorize:permissions inSafari:NO];
|
let qqOAuth = TencentOAuth(appId:"your QQ App ID", andDelegate: self) let permissions = ["get_user_info", "get_simple_userinfo", "add_t"] qqOAuth.authorize(permissions, inSafari: false)
|
在你的 delegate 中,实现 tencentDidLogin 方法。
Objective-C
Swift
- (void)tencentDidLogin { }
|
func tencentDidLogin() { }
|
4.QQ 登录成功后,在 tencentDidLogin 方法中通过 _qqOAuth 对象获取 accessToken 来生成 credential:
Objective-C
Swift
WDGAuthCredential *credential = [WDGQQAuthProvider credentialWithAccessToken:_qqOAuth.accessToken];
|
let credential = WDGQQAuthProvider.credential(withAccessToken: qqOAuth?.accessToken)
|
5.最后,使用 credential 来进行 Auth 用户认证:
Objective-C
Swift
[[WDGAuth auth] signInWithCredential:credential completion:^(WDGUser *user, NSError *error) { }];
|
WDGAuth.auth()?.signIn(with: credential){(user, error) in }
|
退出登录
signOut: 方法用于用户退出登录:
Objective-C
Swift
NSError *error; [[WDGAuth auth] signOut:&error]; if (!error) { }
|
try! WDGAuth.auth()!.signOut()
|
更多使用
- 通过
[WDGAuth auth].currentUser 获取当前用户并管理用户。详情请参考 用户管理。