iOS第三方之友盟(分享、登陆、统计)
2017-09-04 本文已影响7610人
平安喜乐698
目录
1. 友盟分享
2. 友盟登录
3. 友盟统计
1. 友盟分享
集成步骤
第一步
注册友盟开发账号(获取KEY)、注册QQ开发账号(获取ID、key)、注册微博开发账号(获取key、sect)、注册微信开发账号(获取ID、Serct),替换以下key值
第二步
cocoaPods加 (选择性集成)
pod 'UMengUShare/UI'
pod 'UMengUShare/Social/WeChat'
pod 'UMengUShare/Social/ReducedQQ'
pod 'UMengUShare/Social/QQ'
pod 'UMengUShare/Social/ReducedSina'
pod 'UMengUShare/Social/Sina'
/*
选择性集成 其他应用
# U-Share SDK UI模块(分享面板,建议添加)
pod ‘UMengUShare/UI’
# 集成微信(精简版0.2M)
pod ‘UMengUShare/Social/ReducedWeChat'
# 集成微信(完整版14.4M)
pod ‘UMengUShare/Social/WeChat'
# 集成QQ/QZone/TIM(精简版0.5M)
pod ‘UMengUShare/Social/ReducedQQ'
# 集成QQ/QZone/TIM(完整版7.6M)
pod ‘UMengUShare/Social/QQ'
# 集成新浪微博(精简版1M)
pod ‘UMengUShare/Social/ReducedSina'
# 集成新浪微博(完整版25.3M)
pod ‘UMengUShare/Social/Sina'
# 集成Facebook/Messenger
pod ‘UMengUShare/Social/Facebook'
# 集成Twitter
pod ‘UMengUShare/Social/Twitter'
# 集成支付宝
pod ‘UMengUShare/Social/AlipayShare'
# 集成钉钉
pod ‘UMengUShare/Social/DingDing'
# 集成豆瓣
pod ‘UMengUShare/Social/Douban'
# 集成人人
pod ‘UMengUShare/Social/Renren'
# 集成腾讯微博
pod ‘UMengUShare/Social/TencentWeibo'
# 集成来往(点点虫)
pod ‘UMengUShare/Social/LaiWang'
# 集成易信
pod ‘UMengUShare/Social/YiXin'
# 集成领英
pod ‘UMengUShare/Social/Linkedin'
# 集成Flickr
pod ‘UMengUShare/Social/Flickr'
# 集成Kakao
pod ‘UMengUShare/Social/Kakao'
# 集成Tumblr
pod ‘UMengUShare/Social/Tumblr'
# 集成Pinterest
pod ‘UMengUShare/Social/Pinterest'
# 集成Instagram
pod ‘UMengUShare/Social/Instagram'
# 集成Line
pod ‘UMengUShare/Social/Line'
# 集成WhatsApp
pod ‘UMengUShare/Social/WhatsApp'
# 集成有道云笔记
pod ‘UMengUShare/Social/YouDao'
# 集成印象笔记
pod ‘UMengUShare/Social/EverNote'
# 集成Google+
pod ‘UMengUShare/Social/GooglePlus'
# 集成Pocket
pod ‘UMengUShare/Social/Pocket'
# 集成DropBox
pod ‘UMengUShare/Social/DropBox'
# 集成VKontakte
pod ‘UMengUShare/Social/VKontakte'
# 集成邮件
pod ‘UMengUShare/Social/Email'
# 集成短信
pod ‘UMengUShare/Social/SMS'
# 加入IDFA获取
pod ‘UMengUShare/Plugin/IDFA'
*/
info.plist 中添加白名单
<key>LSApplicationQueriesSchemes</key>
<array>
<!-- 微信 URL Scheme 白名单-->
<string>wechat</string>
<string>weixin</string>
<!-- 新浪微博 URL Scheme 白名单-->
<string>sinaweibohd</string>
<string>sinaweibo</string>
<string>sinaweibosso</string>
<string>weibosdk</string>
<string>weibosdk2.5</string>
<!-- QQ、Qzone URL Scheme 白名单-->
<string>mqqapi</string>
<string>mqq</string>
<string>mqqOpensdkSSoLogin</string>
<string>mqqconnect</string>
<string>mqqopensdkdataline</string>
<string>mqqopensdkgrouptribeshare</string>
<string>mqqopensdkfriend</string>
<string>mqqopensdkapi</string>
<string>mqqopensdkapiV2</string>
<string>mqqopensdkapiV3</string>
<string>mqzoneopensdk</string>
<string>wtloginmqq</string>
<string>wtloginmqq2</string>
<string>mqqwpa</string>
<string>mqzone</string>
<string>mqzonev2</string>
<string>mqzoneshare</string>
<string>wtloginqzone</string>
<string>mqzonewx</string>
<string>mqzoneopensdkapiV2</string>
<string>mqzoneopensdkapi19</string>
<string>mqzoneopensdkapi</string>
<string>mqqbrowser</string>
<string>mttbrowser</string>
<!-- 支付宝 URL Scheme 白名单-->
<string>alipay</string>
<string>alipayshare</string>
<!-- 人人 URL Scheme 白名单-->
<string>renrenios</string>
<string>renrenapi</string>
<string>renren</string>
<string>renreniphone</string>
<!-- 来往 URL Scheme 白名单-->
<string>laiwangsso</string>
<!-- 易信 URL Scheme 白名单-->
<string>yixin</string>
<string>yixinopenapi</string>
<!-- instagram URL Scheme 白名单-->
<string>instagram</string>
<!-- whatsapp URL Scheme 白名单-->
<string>whatsapp</string>
<!-- line URL Scheme 白名单-->
<string>line</string>
<!-- Facebook URL Scheme 白名单-->
<string>fbapi</string>
<string>fb-messenger-api</string>
<string>fbauth2</string>
<string>fbshareextension</string>
</array>
项目 | Info | URLTypes添加URL Schemes
微信
微信AppKey
QQ/QQ空间
tencent+QQAppID (例:tencent100424468)
QQ+8位16进制 (AppID转16进制后,不足8位前面补0)
新浪微博
wb+新浪appKey
支付宝
ap+appID (identifier填alipayShare)
钉钉
钉钉appkey (identifier填dingtalk)
易信
易信appkey
Facebook
fb+FacebookID
第三步
注册友盟分享、注册各个平台,在openURL方法中做相应处理
AppDelegate 配置APIKEY
// 友盟appKey
static NSString *umengKey = @"596*12d475ca353ed7000bb7";
// QQ
static NSString *qqAppID = @"1105*85954";
static NSString *qqAppKey = @"5Z*CTJeQneILxHvJ";
// 微博
static NSString *wbAppKey = @"33*1627971";
static NSString *wbAppSecret = @"60*d6b29e7e7ad697a2e8f5855cd7fd1";
// 微信
static NSString *wxAppID = @"wxda5*c5195ebb9ffe";
static NSString *wxAppSecret = @"e3*055efd6db3d1497424a4c0e36dbd2";
// 友盟
[[UMSocialManager defaultManager] setUmSocialAppkey:USHAREAPPKEY];
// 微信
[[UMSocialManager defaultManager] setPlaform:UMSocialPlatformType_WechatSession appKey:WXAppID appSecret:WXSecret redirectURL:@"http://mobile.umeng.com/social"];
// QQ
[[UMSocialManager defaultManager] setPlaform:UMSocialPlatformType_QQ appKey:QQAppID appSecret:nil redirectURL:@"http://mobile.umeng.com/social"];
// 新浪
[[UMSocialManager defaultManager] setPlaform:UMSocialPlatformType_Sina appKey:SinaAPPKEY appSecret:SinaSecret redirectURL:@"https://sns.whalecloud.com/sina2/callback"];
// 移除相应平台的分享,如微信收藏
//[[UMSocialManager defaultManager] removePlatformProviderWithPlatformTypes:@[@(UMSocialPlatformType_WechatFavorite)]];
// iOS 9以前
- (BOOL)application:(UIApplication *)application
openURL:(NSURL *)url
sourceApplication:(NSString *)sourceApplication
annotation:(id)annotation {
BOOL result = [[UMSocialManager defaultManager] handleOpenURL:url sourceApplication:sourceApplication annotation:annotation];
if (!result) {
// 其他如支付等SDK的回调
BOOL canHandleURL = [Pingpp handleOpenURL:url withCompletion:nil];
return canHandleURL;
}
return result;
}
// iOS 9 及以后
- (BOOL)application:(UIApplication *)app
openURL:(NSURL *)url
options:(NSDictionary *)options {
BOOL result = [[UMSocialManager defaultManager] handleOpenURL:url];
if (!result) {
BOOL canHandleURL = [Pingpp handleOpenURL:url withCompletion:nil];
return canHandleURL;
}
return result;
}
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url{
//
BOOL result = [[UMSocialManager defaultManager] handleOpenURL:url];
if (!result) {
// 其他如支付等SDK的回调
BOOL canHandleURL = [Pingpp handleOpenURL:url withCompletion:nil];
return canHandleURL;
}
return result;
}
其他应用的平台初始化
// 钉钉
[[UMSocialManager defaultManager] setPlaform: UMSocialPlatformType_DingDing appKey:@"dingoalmlnohc0wggfedpk" appSecret:nil redirectURL:nil];
// 支付宝
[[UMSocialManager defaultManager] setPlaform: UMSocialPlatformType_AlipaySession appKey:@"2015111700822536" appSecret:nil redirectURL:nil];
// 易信
[[UMSocialManager defaultManager] setPlaform: UMSocialPlatformType_YixinSession appKey:@"yx35664bdff4db42c2b7be1e29390c1a06" appSecret:nil redirectURL:@"http://mobile.umeng.com/social"];
// 点点虫(原来往)
[[UMSocialManager defaultManager] setPlaform: UMSocialPlatformType_LaiWangSession appKey:@"8112117817424282305" appSecret:@"9996ed5039e641658de7b83345fee6c9" redirectURL:@"http://mobile.umeng.com/social"];
// 领英
[[UMSocialManager defaultManager] setPlaform:UMSocialPlatformType_Linkedin appKey:@"81t5eiem37d2sc" appSecret:@"7dgUXPLH8kA8WHMV" redirectURL:@"https://api.linkedin.com/v1/people"];
// Twitter
[[UMSocialManager defaultManager] setPlaform:UMSocialPlatformType_Twitter appKey:@"fB5tvRpna1CKK97xZUslbxiet" appSecret:@"YcbSvseLIwZ4hZg9YmgJPP5uWzd4zr6BpBKGZhf07zzh3oj62K" redirectURL:nil];
// Facebook
[[UMSocialManager defaultManager] setPlaform:UMSocialPlatformType_Facebook appKey:@"506027402887373" appSecret:nil redirectURL:nil];
// Pinterest
[[UMSocialManager defaultManager] setPlaform:UMSocialPlatformType_Pinterest appKey:@"4864546872699668063" appSecret:nil redirectURL:nil];
// dropbox
[[UMSocialManager defaultManager] setPlaform: UMSocialPlatformType_DropBox appKey:@"k4pn9gdwygpy4av" appSecret:@"td28zkbyb9p49xu" redirectURL:@"https://mobile.umeng.com/social"];
// vk
[[UMSocialManager defaultManager] setPlaform:UMSocialPlatformType_VKontakte appKey:@"5786123" appSecret:nil redirectURL:nil];
第四步:点击分享按钮
-(void)inviteFriend{
// 弹出自定义分享按钮视图,点击事件:
switch (tag) {
case 0://微信朋友圈
[weakSelf shareWebPageToPlatformType:UMSocialPlatformType_WechatTimeLine];
break;
case 1://微信好友
[weakSelf shareWebPageToPlatformType:UMSocialPlatformType_WechatSession];
break;
case 2: //QQ
[weakSelf shareWebPageToPlatformType:UMSocialPlatformType_QQ];
break;
case 3: //微博
[weakSelf shareWebPageToPlatformType:UMSocialPlatformType_Sina];
break;
default:
break;
}
- (void)shareWebPageToPlatformType:(UMSocialPlatformType)platformType{
// 创建分享对象
UMSocialMessageObject *messageObject = [UMSocialMessageObject messageObject];
// 设置内容
// 标题 详情 图片
UMShareWebpageObject *shareObject = [UMShareWebpageObject shareObjectWithTitle:@"Y-您贴身的出行管家" descr:@"#Y#出游担心行程不满意,提前结束又损失钱财?上Y,行程费用每天结,减少你的经济损失,提升管家服务质量。" thumImage:[UIImage imageNamed:@"icon"]]; // img类型(直接写图片名,分享失败)
// 点击跳转的URL
shareObject.webpageUrl = @"分享跳转的url"; // 没有地址则微信提示小红点
messageObject.shareObject = shareObject;
// 分享---调用分享接口
[[UMSocialManager defaultManager] shareToPlatform:platformType messageObject:messageObject currentViewController:self completion:^(id data, NSError *error) {
if (error) {
UMSocialLogInfo(@"************Share fail with error %@*********",error);
[YTHUD showError:@"分享失败"];
}else{
if ([data isKindOfClass:[UMSocialShareResponse class]]) {
}else{
}
[YTHUD showSuccess:@"分享成功"];
}
}];
}
// 弹出系统分享UI
[UMSocialUIManager showShareMenuViewInWindowWithPlatformSelectionBlock:^(UMSocialPlatformType platformType, NSDictionary *userInfo) {
}];
如果没有出现某个平台,去添加白名单
苹果原生分享(一般不用)
// 须在手机设置中添加账号
#import <Social/Social.h>
if([SLComposeViewController isAvailableForServiceType:SLServiceTypeTencentWeibo]){
return;
}
//
SLComposeViewController *comC=[SLComposeViewController composeViewControllerForServiceType:SLServiceTypeTencentWeibo];
[comC setInitialText:@"分享内容"];
[comC addImage:[UIImage imageNamed:@"payyinlian"]];
[comC addURL:[NSURL URLWithString:@"http://www.baidu.com"]];
__block SLComposeViewController *comBlock=comC;
comC.completionHandler = ^(SLComposeViewControllerResult result) {
//
if(result==SLComposeViewControllerResultDone){
NSLog(@"开始发送");
}
[comBlock dismissViewControllerAnimated:true completion:nil];
};
[self presentViewController:comC animated:true completion:nil];
2. 友盟登录
支持的平台
国内平台
微信、QQ、新浪、腾讯微博、人人网、豆瓣
国外平台
Facebook、Twitter、linkedIn、Kakao
配置 同分享
import <UMSocialCore/UMSocialCore.h>
可以获取到 昵称/性别/头像/应用特殊唯一ID/
NSInteger LoginType = 0;
switch (type) {
case 0:
LoginType=UMSocialPlatformType_QQ;
break;
case 1:
LoginType=UMSocialPlatformType_WechatSession;
break;
case 2:
LoginType=UMSocialPlatformType_Sina;
break;
default:
break;
}
[[UMSocialManager defaultManager]getUserInfoWithPlatform:LoginType currentViewController:nil completion:^(id result, NSError *error) {
if(error){
NSLog(@"%@",error);
}else{
//
UMSocialUserInfoResponse *resp = result;
NSDictionary *dic;
switch (LoginType) {
case UMSocialPlatformType_QQ:{
dic=@{@"name":resp.name,@"avatar":resp.iconurl,@"type":@"1",@"id":resp.accessToken,@"sex":resp.unionGender};
}
break;
case UMSocialPlatformType_WechatSession:{
dic=@{@"name":resp.name,@"avatar":resp.iconurl,@"type":@"2",@"id":resp.uid,@"sex":resp.unionGender};
}
break;
case UMSocialPlatformType_Sina:{
dic=@{@"name":resp.name,@"avatar":resp.iconurl,@"type":@"3",@"id":resp.accessToken,@"sex":resp.unionGender };
}
break;
default:
break;
}
NSLog(@"%@",dic);
}
}];
3. 友盟统计
pod 'UMengAnalytics'
#import <UMMobClick/MobClick.h>
AppDelegate中(仅+以下代码即可)
// 友盟统计
[MobClick setLogEnabled:true];
UMConfigInstance.appKey=USHAREAPPKEY; // 友盟应用AppKey
UMConfigInstance.channelId=@"App Store";
[MobClick startWithConfigure:UMConfigInstance];
// 是否加密统计数据(默认:不加密)
[MobClick setEncryptEnabled:true];
// 是否捕捉bug(默认:是)
[MobClick setCrashReportEnabled:false];
/*. 打印测试设备id时
// b75294c460d76ef8c248aa73b2049cc367949ce7
Class cls = NSClassFromString(@"UMANUtil");
SEL deviceIDSelector = @selector(openUDIDString);
NSString *deviceID = nil;
if(cls && [cls respondsToSelector:deviceIDSelector]){
deviceID = [cls performSelector:deviceIDSelector];
}
NSData* jsonData = [NSJSONSerialization dataWithJSONObject:@{@"oid" : deviceID}
options:NSJSONWritingPrettyPrinted
error:nil];
NSLog(@"%@", [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding]);
*/
配置数据发送策略(有官网(优先)和代码 2方式)
官网---开发者官网|统计分析|设置|发送策略(有:启动时(默认)和按间隔2种)
统计账号(友盟统计默认以设备为标准,可通过以下方法统计账号)
代码
// 统计用户id为uid的账号
[MobClick profileSignInWithPUID:@"uid"];
// 统计用户id为uid,登陆方式为wx的账号
[MobClick profileSignInWithPUID:@"uid" provider:@"wx"];
// 停止统计用户
[MobClick profileSignOff];
官网
官网|设置|使用账号统计报表(启动)
统计页面
// willAppear中+
[MobClick beginLogPageView:@"page"];
// willDisAppear中+
[MobClick endLogPageView:@"page"];
统计事件
代码
// 统计事件
[MobClick event:@"eventId"];
// 统计事件(+属性)
[MobClick event:@"purchase" attributes:@{@"type" : @"book", @"quantity" : @"3"}];
官网
官网|设置|事件 添加事件