title: 微信登录
本篇文档介绍在 Wilddog Auth 中如何使用微信对用户进行身份认证。
前期准备
- 在控制面板中创建应用。请参考 控制面板-创建应用。
- 在 微信开放平台管理中心,获取应用的 AppID 和 AppSecret。
- 在 控制面板 身份认证—登录方式 中打开微信登录方式,配置微信帐号 AppID 和 AppSecret。
实现微信登录
1.安装 Wilddog Auth SDK:
将以下 pod 包含在你的 Podfile 中:
pod 'Wilddog/Auth'
安装 SDK:
$ pod install
2.创建 Wilddog Auth 实例:
Objective-C
Swift
objectivec
WDGOptions *option = [[WDGOptions alloc] initWithSyncURL:@"https://<your-wilddog-appid>.wilddogio.com"];
[WDGApp configureWithOptions:option];
swift
let options = WDGOptions.init(syncURL: "https://<your-wilddog-appid>.wilddogio.com")
WDGApp.configure(with: options)
3.参考 微信 iOS 接入指南 将微信登录集成到你的应用中。在 AppDelegate 的 application: openURL: options:
方法中设置 delegate 来接收网络事件:
Objective-C
Swift
objectivec
- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<NSString *,id> *)options
{
if ([url.absoluteString hasPrefix:@"wx"]) {
return [WXApi handleOpenURL:url delegate:self];
}
return NO;
}
swift
func application(app: UIApplication, openURL url: NSURL, options: [String : AnyObject]) -> Bool {
if url.absoluteString.hasPrefix("wx") {
return WXApi.handleOpenURL(url, delegate: self)
}
}
调用微信的授权代码:
Objective-C
Swift
objectivec
SendAuthReq *req = [SendAuthReq new];
req.scope = @"snsapi_userinfo" ;
req.state = @"osc_wechat_login" ;
// 第三方向微信终端发送一个 SendAuthReq 消息结构
[WXApi sendReq:req];
swift
let req = SendAuthReq()
req.scope = "snsapi_userinfo"
req.state = "osc_wechat_login"
// 第三方向微信终端发送一个 SendAuthReq 消息结构
WXApi.sendReq(req)
在你的 delegate 中,实现 onResp:
方法,并从中获取用户登录的 code:
Objective-C
Swift
objectivec
-(void)onResp:(BaseResp*)resp
{
SendAuthResp *response = (SendAuthResp*)resp;
NSString *code = response.code;
}
swift
func onResp(resp: BaseResp!) {
let response = resp as? SendAuthResp
let code = response?.code
}
4.微信登录成功后,在 onResp:
方法中得到的 code 来生成 credential:
Objective-C
Swift
objectivec
WDGAuthCredential *credential =
[WDGWeiXinAuthProvider credentialWithCode:code];
swift
let credential = WDGWeiXinAuthProvider.credential(withCode: weixinOAuth.code)
5.使用 credential 来进行 Auth 用户认证:
Objective-C
Swift
objectivec
[[WDGAuth auth] signInWithCredential:credential
completion:^(WDGUser *user, NSError *error) {
// ...
}];
swift
WDGAuth.auth()?.signIn(with: credential){(user, error) in
// ...
}
退出登录
signOut:
方法用于用户退出登录:
Objective-C
Swift
objectivec
NSError *error;
[[WDGAuth auth] signOut:&error];
if (!error) {
// 退出登录成功
}
swift
try! WDGAuth.auth()!.signOut()
更多使用
- 通过
[WDGAuth auth].currentUser
获取当前用户并管理用户。详情请参考 用户管理。