讲到第三方登录分享,第一反应大概就是友盟、ShareSDK之类。集成微信、QQ、微博三个平台的话,友盟的SDK有62.9M,ShareSDK的包有74M。如果直接集成最原始的三个平台的SDK,大小总共为51M。(关于集成前期的配置,可参考原生接入微博、微信和QQ的登录及分享)
WechatIMG286.jpeg
我们一般会在各个平台的授权登录回调里去获取需要的值。微信会返回code,QQ会返回accessToken和openId,微博会返回access_token,拿到这些值后传给自己的后台,后台再用这些值去对应的平台请求用户信息,并与自己的用户体系绑定。
// 微信授权登录回调
- (void)onResp:(id )resp {
if ([resp isKindOfClass:[SendAuthResp class]]) {
SendAuthResp *response = (SendAuthResp *)resp;
if (response.errCode == 0) {
SendAuthResp *aresp = (SendAuthResp *)resp;
NSString *code = aresp.code;
DLog(@"--- %@", code);
[MBProgressHUD showInfoMsg:@"登录成功"];
}else{
[MBProgressHUD showErrorMsg:@"登录失败"];
}
}
}
// 微博授权登录回调
- (void)didReceiveWeiboResponse:(WBBaseResponse *)response {
if ([response isKindOfClass:WBAuthorizeResponse.class]) {
if (response.statusCode == WeiboSDKResponseStatusCodeSuccess) {
NSDictionary *userInfo = response.userInfo;
NSString * access_token =[userInfo objectForKey:@"access_token"];
DLog(@"--- %@", access_token);
}else {
[MBProgressHUD showErrorMsg:@"登录失败"];
}
}
}
// QQ授权登录成功的回调
- (void)tencentDidLogin {
/* Access Token凭证,用于后续访问各开放接口*/
DLog(@"%@ -- %@",_tencentOAuth.accessToken, _tencentOAuth.openId);
/* 获取用户信息。调用这个方法,qq的sdk会自动调用
- (void)getUserInfoResponse:(APIResponse *)response*/
}
然而,用MonkeyKing就不需要那么复杂,也不用集成SDK。所有的请求都会在内部完成。
使用方法
1. 先注册各个平台。
MonkeyKing.registerAccount(.weChat(appID: KEY.kWX_APP_ID, appKey: KEY.kWX_APP_KEY, miniAppID: nil))
MonkeyKing.registerAccount(.qq(appID: KEY.kQQ_APP_ID))
/*微博的appID是AppKey,AppKey是AppSecret*/
MonkeyKing.registerAccount(.weibo(appID: KEY.kWB_APP_KEY, appKey: KEY.kWB_APP_SECRET, redirectURL: KEY.kWB_REDIRECT_URL))
2. 调用
MonkeyKing.weChatOAuthForCode(scope: nil, requestToken: nil) { (wxCode, error) in
// 微信授权登录
if let code = wxCode {
print(code)
}
}
MonkeyKing.oauth(for: .qq, scope: "get_simple_userinfo") { (info, response, error) in
// QQ授权登录
if let d = info {
if let openid = d["openid"], let token = d["access_token"] {
print("openid is \(openid), token is \(token)")
}
}
}
MonkeyKing.oauth(for: .weibo) { (info, response, error) in
// 微博授权登录
if let d = info {
if let token = d["accessToken"] {
print(token)
}
}
}
实现原理
微信登录
-
通过weixin://app/YourAppId/auth/?scope=snsapi_userinfo&state=Weixinauth
授权获取微信code
-
拿到code值通过https://api.weixin.qq.com/sns/oauth2/access_token
授权获取微信openId和access_token
-
拿到openId和access_token通过https://api.weixin.qq.com/sns/userinfo
获取用户信息。(ps:这一步一般都是后台操作。)
QQ登录(xxx为你的AppId)
-
通过mqqOpensdkSSoLogin://SSoLogin/tencentXXX/com.tencent.tencentXXX?generalpastboard=1
获取openId和access_token
-
拿到openId和access_token后通过https://graph.qq.com/user/get_user_info
获取用户信息。(ps:这一步一般都是后台操作。)
微博登录
-
设置剪切版参数,再通过weibosdk://request?id=uuidString&sdkversion=003203000
获取accessToken
let uuidString = UUID().uuidString
let transferObjectData = NSKeyedArchiver.archivedData(withRootObject: [
"__class": "WBAuthorizeRequest",
"redirectURI": redirectURL,
"requestID": uuidString,
"scope": scope ])
let userInfoData = NSKeyedArchiver.archivedData(withRootObject: [
"mykey": "as you like",
"SSO_From": "SendMessageToWeiboViewController"])
let appData = NSKeyedArchiver.archivedData(withRootObject: [
"appKey": appID,
"bundleID": Bundle.main.monkeyking_bundleID ?? "",
"name": Bundle.main.monkeyking_displayName ?? ""])
let authItems: [[String: Any]] = [
["transferObject": transferObjectData],
["userInfo": userInfoData],
["app": appData]]
UIPasteboard.general.items = authItems
-
拿到accessToken,通过https://api.weibo.com/2/users/show.json
获取用户信息。(ps:这一步一般都是后台操作。)
MonkeyKing除了登录功能外,还有分享及支付功能等。而这些功能的实现不需要集成任何SDK,对App体积大小有要求的人来说,这个库相当棒。具体使用方法,详见github
网友评论