1、在友盟文档中这样说:
7.4 微信登录
添加配置文件参考文档:添加微信及朋友圈,添加相关库文件,配置URL schemes及添加系统回调
注意微信登录必须先在微信开放平台申请微信登录权限
在你的程序APPdelegate入口方法添加下面的代码
#import "UMSocialWechatHandler.h"
//设置微信AppId、appSecret,分享url
[UMSocialWechatHandler setWXAppId:@"wxd930ea5d5a258f4f" appSecret:@"db426a9829e4b49a0dcac7b4162da6b6" url:@"http://www.umeng.com/social"];
在微信登录按钮中实现下面的方法
UMSocialSnsPlatform *snsPlatform = [UMSocialSnsPlatformManager getSocialPlatformWithName:UMShareToWechatSession];
snsPlatform.loginClickHandler(self,[UMSocialControllerService defaultControllerService],YES,^(UMSocialResponseEntity *response){
if (response.responseCode == UMSResponseCodeSuccess) {
UMSocialAccountEntity *snsAccount = [[UMSocialAccountManager socialAccountDictionary]valueForKey:UMShareToWechatSession];
NSLog(@"username is %@, uid is %@, token is %@ url is %@",snsAccount.userName,snsAccount.usid,snsAccount.accessToken,snsAccount.iconURL);
}
});
在授权完成后调用获取用户信息的方法
//得到的数据在回调Block对象形参respone的data属性
[[UMSocialDataService defaultDataService] requestSnsInformation:UMShareToWechatsession completion:^(UMSocialResponseEntity *response){
NSLog(@"SnsInformation is %@",response.data);
}];
2、实践使用时:
#pragma mark - 微信登录
- (void)loginViewClickedWeiXiButton{
[UMSocialData setAppKey:@"55bb141a67e58e9f80001801"];
[UMSocialWechatHandler setWXAppId:@"wx2802d4e49f68996f" appSecret:@"f1bf8a16c5f87570ab07782d3e0ab223" url:@"http://www.umeng.com/social"];
UMSocialSnsPlatform *snsPlatform = [UMSocialSnsPlatformManager getSocialPlatformWithName:UMShareToWechatSession];
snsPlatform.loginClickHandler(self,[UMSocialControllerService defaultControllerService],YES,^(UMSocialResponseEntity *response){
// 获取微博用户名、uid、token等
if (response.responseCode == UMSResponseCodeSuccess) {
UMSocialAccountEntity *snsAccount = [[UMSocialAccountManager socialAccountDictionary]valueForKey:UMShareToWechatSession];
NSLog(@"username is %@, uid is %@, token is %@ iconUrl is %@",snsAccount.userName,snsAccount.usid,snsAccount.accessToken,snsAccount.iconURL);
}
});
}
3、【坑在这里】没有回调时因为要在应用代理方法中增加:
//sdk
#import "UMSocialSnsService.h"
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
return [UMSocialSnsService handleOpenURL:url wxApiDelegate:nil];
}
// 2018.01.05 更新: iOS 9.0 以上替换上面的代理回调
- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options
{
return [UMSocialSnsService handleOpenURL:url wxApiDelegate:nil];
}
- (void)applicationDidBecomeActive:(UIApplication *)application
{
[UMSocialSnsService applicationDidBecomeActive];
}
最后说明
- 微信的
openid
对应友盟里叫usid
下面是授权成功的回调数据:
- UMSocial: makeRequestWithPath is /share/token/53290df956240b6b4a0084b3/, parameters is {
"access_token" = "OezXcEiiBSKSxW0eoylIeJDUKD6z6dmr42JANLPjNN56WRFnd-vVJO37ma0gCpKY1v5q8EVEMwdQqzvXoFFpe1jQXUzjqukub9-228J8sjvugvVVQYvzkhyvAgItvkYmMIGD_cE2hAWUm44_qCB-oQ";
ak = 53290df956240b6b4a0084b3;
de = "iPhone7,1";
dt = 1438857527939;
ek = 4b37315fd0421b0b3a682a4a5e4487e9;
en = WiFi;
"expires_in" = 7199;
mac = "02:00:00:00:00:00";
openid = oyAaTjrJMAb3qfCbyDRp3h0NKyew;
openudid = 147ad485c3b80ffd54bfc8689c2d7db1ed88994b;
os = iOS;
pcv = "2.0";
"refresh_token" = "OezXcEiiBSKSxW0eoylIeJDUKD6z6dmr42JANLPjNN56WRFnd-vVJO37ma0gCpKYHXgyDeQ4Xqm60G-omrGp2u-KbIy7zTP5gV5dnw3RVN-GzkjY8gCWIMC3_cCHcflH6IPexy2c-k46gGG0M07p6g";
sdkv = "4.2.3";
to = wxsession;
tp = 0;
uid = 462ae0edb57d53dfd2affb5e4ae36ad;
usid = oyAaTjrJMAb3qfCbyDRp3h0NKyew;
}
[18:38:48.197( 2055)] - UMSocial: actionType is 15 , received json Dic is {
data = {
};
msg = "no error";
st = 200;
}
-
- 如果有什么疑问,可以在评论区一起讨论;
- 如果有什么不正确的地方,欢迎指导!
2018.01.05 更新
iOS 9.0 以后,系统新增加代理回调方法:
- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options
{
return [UMSocialSnsService handleOpenURL:url wxApiDelegate:nil];
}
试试增加这个后行不行,很久没有使用友盟SDK了,现在都是做国外产品,如果还不行,大家问一下友盟客服啊。
注:本文首发于 iHTCboy's blog,如若转载,请注明来源。