一、申请应用信息
1、申请开发者账号
2、申请应用的appid等信息
腾讯开放平台、WeiXin开放平台、新浪微博开放平台
*申请分享应用的前提是需要应用已经上线,申请时需要提供商城地址。
二、下载友盟开发包
1、下载地址
2、下载完成后直接拖拽到项目目录里即可,Xcode 会自动区分 Header Search Paths、Framework Search Paths 、Library Search Paths
3、添加依赖库
具体操作请参考 友盟开发文档
三、配置工程
1、配置URL Schemes
四、添加代码
1、AppDelegate中添加:
- - (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
- {
- BOOL result = [UMSocialSnsService handleOpenURL:url];
- if (result == FALSE) {
-
- }
- return result;
- }
-
- -(BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url
- {
- BOOL result = [UMSocialSnsService handleOpenURL:url];
- if (result == FALSE) {
- }
- return result;
- }
1、创建工具类:UMControl.h 负责友盟的信息初始化、分享、第三方登陆。
2、代码
.h文件
文件添加枚举类型便于区分登陆,以及保存当前登陆状态,便于重新登陆
- typedef enum
- {
- LoginTypeNone = 0,
- LoginTypeQQ = 1,
- LoginTypeWeiXin = 2,
- LoginTypeWeiBo = 3,
- }LoginType;
声明block 用于返回登陆成功/失败信息
- typedef void(^UMBlock)(BOOL success);
添加登陆方法
- -(void)SocialSnsLoginWithType:(LoginType)type block:(UMBlock)block;
.m文件
添加头文件:
- #import "UMSocial.h"
- #import "UMSocialWechatHandler.h"
- #import "UMSocialQQHandler.h"
- #import "UMSocialSinaSSOHandler.h"
登陆方法:
- -(void)SocialSnsLoginWithType:(LoginType)type block:(UMBlock)block
- {
- NSString *platformName = [self getPlatformNameWithType:type];
- UMSocialSnsPlatform *snsPlatform = [UMSocialSnsPlatformManager getSocialPlatformWithName:platformName];
- snsPlatform.loginClickHandler((UIViewController*)self,[UMSocialControllerService defaultControllerService],YES,^(UMSocialResponseEntity *response){
- if (response.responseCode == UMSResponseCodeSuccess) {
- UMSocialAccountEntity *snsAccount = [[UMSocialAccountManager socialAccountDictionary] valueForKey:snsPlatform.platformName];
-
-
-
- NSLog(@"\nusername = %@,\n usid = %@,\n token = %@ iconUrl = %@,\n unionId = %@,\n thirdPlatformUserProfile = %@,\n thirdPlatformResponse = %@ \n, message = %@",snsAccount.userName,snsAccount.usid,snsAccount.accessToken,snsAccount.iconURL, snsAccount.unionId, response.thirdPlatformUserProfile, response.thirdPlatformResponse, response.message);
- block(true);
- }else{
- block(false);
- }
- });
- }
辅助方法:
- -(NSString*)getPlatformNameWithType:(LoginType)type
- {
- NSString *name = @"";
- switch (type) {
- case LoginTypeWeiBo:
- name = UMShareToSina;
- break;
- case LoginTypeWeiXin:
- name = UMShareToWechatSession;
- break;
- case LoginTypeQQ:
- name = UMShareToQQ;
- break;
- default:
- break;
- }
- return name;
- }