Umeng三方登陆个人总结
Demo地址:https://github.com/haozheMa/UmengThirdLoginDemo
这里仅以微信、QQ和新浪微博作为例子
首先根据官方文档
导入以下依赖库
Security.framework libiconv.dylib SystemConfiguration.framework CoreGraphics.Framework libsqlite3.dylib CoreTelephony.framework libstdc++.dylib libz.dylib imageio.framework(新浪微博的SDK需要这个库)
在AppDelegate中添加头文件
#import "AppDelegate.h" #import "UMSocial.h" #import "UMSocialQQHandler.h" #import "UMSocialSinaSSOHandler.h" #import "UMSocialWechatHandler.h"
然后在didFinishLaunchingWithOptions中添加AppKey
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { [UMSocialData setAppKey:@"507fcab25270157b37000010"]; //打开调试log的开关 [UMSocialData openLog:YES]; //如果你要支持不同的屏幕方向,需要这样设置,否则在iPhone只支持一个竖屏方向 [UMSocialConfig setSupportedInterfaceOrientations:UIInterfaceOrientationMaskAll]; //设置微信AppId,设置分享url,默认使用友盟的网址 [UMSocialWechatHandler setWXAppId:@"wxdc1e388c3822c80b" appSecret:@"a393c1527aaccb95f3a4c88d6d1455f6" url:@"http://www.umeng.com/social"]; // 打开新浪微博的SSO开关 // 将在新浪微博注册的应用appkey、redirectURL替换下面参数,并在info.plist的URL Scheme中相应添加wb+appkey,如"wb3921700954",详情请参考官方文档。 [UMSocialSinaSSOHandler openNewSinaSSOWithAppKey:@"3921700954" secret:@"04b48b094faeb16683c32669824ebdad" RedirectURL:@"http://sns.whalecloud.com/sina2/callback"]; // //设置分享到QQ空间的应用Id,和分享url 链接 [UMSocialQQHandler setQQWithAppId:@"100424468" appKey:@"c7394704798a158208a74ab60104f0ba" url:@"http://www.umeng.com/social"]; // //设置支持没有客户端情况下使用SSO授权 [UMSocialQQHandler setSupportWebView:YES]; return YES; } /** 这里处理SSO授权之后跳转回来,和分享完成之后跳转回来 */ - (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation { BOOL result = [UMSocialSnsService handleOpenURL:url]; if (result == FALSE) { //调用其他SDK,例如支付宝SDK等 } return result; } /** 这里处理三方SSO授权进入三方客户端后进入后台,再返回原来应用 */ - (void)applicationDidBecomeActive:(UIApplication *)application { [UMSocialSnsService applicationDidBecomeActive]; }
然后就可以在登录按钮中实现下面的方法
/** * 微博 * * @param sender <#sender description#> */ - (IBAction)clickWB:(id)sender { UMSocialSnsPlatform *snsPlatform = [UMSocialSnsPlatformManager getSocialPlatformWithName:UMShareToSina]; snsPlatform.loginClickHandler(self,[UMSocialControllerService defaultControllerService],YES,^(UMSocialResponseEntity *response){ // 获取微博用户名、uid、token等 if (response.responseCode == UMSResponseCodeSuccess) { UMSocialAccountEntity *snsAccount = [[UMSocialAccountManager socialAccountDictionary] valueForKey:UMShareToSina]; NSLog(@"username is %@, uid is %@, token is %@ url is %@",snsAccount.userName,snsAccount.usid,snsAccount.accessToken,snsAccount.iconURL); }}); } /** * 微信 * * @param sender <#sender description#> */ - (IBAction)clickWX:(id)sender { 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); } }); } /** * qq登录 * * @param sender <#sender description#> */ - (IBAction)clickQQ:(id)sender { UMSocialSnsPlatform *snsPlatform = [UMSocialSnsPlatformManager getSocialPlatformWithName:UMShareToQQ]; snsPlatform.loginClickHandler(self,[UMSocialControllerService defaultControllerService],YES,^(UMSocialResponseEntity *response){ // 获取微博用户名、uid、token等 if (response.responseCode == UMSResponseCodeSuccess) { UMSocialAccountEntity *snsAccount = [[UMSocialAccountManager socialAccountDictionary] valueForKey:UMShareToQQ]; NSLog(@"username is %@, uid is %@, token is %@ url is %@",snsAccount.userName,snsAccount.usid,snsAccount.accessToken,snsAccount.iconURL); }}); }
原则上代码就这么多
为了实现免登录(SSO)
我们需要选中自己的 target,在 Info->URL Types 中添加 URL Schemes
这里给出官方提供的URL Schemes
微博 wb3921700954
sina.5211818556240bc9ee01db2f(我不知道为何需要两个)
QQ QQ05FC5B14
tencent100424468(这两个都写吧,貌似一个是QQ一个是空间的)
微信 wxdc1e388c3822c80b
然后是在info.plist 中添加-应用跳转(SSO等)可以参考官方文档 “HTTP传输安全” 这个一块加进去吧,貌似也有用
在此过程中我遇到了这些问题
1.新浪微博三方登录需要使工程的BundleID 和在新浪平台注册一样,我用的是友盟的那一套,所以我需要改我的BundleID—>com.Umeng.UMSocial
2.点击新浪微博登录闪退。。。看网上说的:在Build settings->Linking->Other Linker Flags 把值由-ObjC改为-all_load(前提是你这里本身有值)不过我这边不是这个问题。开始闪退是因为我这里什么都没写,填上-ObjC或-all_load之后就直接报红,是因为没有导入imageio.framework
3.即使这样又遇到了*** does not contain bitcode. You must rebuild it with bitcode enabled (Xcode setting ENABLE_BITCODE类似的问题
是因为友盟的二进制库不支持bitcode,而Xcode默认是要支持bitcode的,而且如果支持的话,其中所有的二进制库和framework都必须包含bitcode.至于什么是bitcode,请看这里: http://blog.csdn.net/soindy/article/details/48518717
有什么不对的地方请指点。