版本记录
版本号 | 时间 |
---|---|
V1.0 | 2018.04.05 |
前言
相信很多人用过友盟,包括友盟的第三方登录、分享以及统计。这里我们就分几个模块进行详细的说明 —— 移动统计
(U - App)
、网站统计(U - Web)
、消息推送(U - Push)
、社会化分享(U - Share)
、游戏统计(U - Game)
、互联网运营数据服务(Dplus)
- 多维度、一站式、精细化数据管理服务。感兴趣的可以看上面几篇。
1. 友盟集成(一) —— UShare模块之快速集成(一)
2. 友盟集成(二) —— UShare模块之快速集成(二)
3. 友盟集成(三) —— UShare模块之第三方登录(一)
4. 友盟集成(四) —— UShare模块之进阶说明之第三方平台SDK说明(一)
分享到第三方平台
1. 第三方平台支持的分享类型总览
2. 分享文本
- (void)shareTextToPlatformType:(UMSocialPlatformType)platformType
{
//创建分享消息对象
UMSocialMessageObject *messageObject = [UMSocialMessageObject messageObject];
//设置文本
messageObject.text = @"社会化组件UShare将各大社交平台接入您的应用,快速武装App。";
//调用分享接口
[[UMSocialManager defaultManager] shareToPlatform:platformType messageObject:messageObject currentViewController:self completion:^(id data, NSError *error) {
if (error) {
NSLog(@"************Share fail with error %@*********",error);
}else{
NSLog(@"response data is %@",data);
}
}];
}
3. 分享图片
- (void)shareImageToPlatformType:(UMSocialPlatformType)platformType
{
//创建分享消息对象
UMSocialMessageObject *messageObject = [UMSocialMessageObject messageObject];
//创建图片内容对象
UMShareImageObject *shareObject = [[UMShareImageObject alloc] init];
//如果有缩略图,则设置缩略图
shareObject.thumbImage = [UIImage imageNamed:@"icon"];
[shareObject setShareImage:@"https://mobile.umeng.com/images/pic/home/social/img-1.png"];
//分享消息对象设置分享内容对象
messageObject.shareObject = shareObject;
//调用分享接口
[[UMSocialManager defaultManager] shareToPlatform:platformType messageObject:messageObject currentViewController:self completion:^(id data, NSError *error) {
if (error) {
NSLog(@"************Share fail with error %@*********",error);
}else{
NSLog(@"response data is %@",data);
}
}];
}
4. 分享图文(新浪支持,微信/QQ仅支持图或文本分享)
- (void)shareImageAndTextToPlatformType:(UMSocialPlatformType)platformType
{
//创建分享消息对象
UMSocialMessageObject *messageObject = [UMSocialMessageObject messageObject];
//设置文本
messageObject.text = @"社会化组件UShare将各大社交平台接入您的应用,快速武装App。";
//创建图片内容对象
UMShareImageObject *shareObject = [[UMShareImageObject alloc] init];
//如果有缩略图,则设置缩略图
shareObject.thumbImage = [UIImage imageNamed:@"icon"];
[shareObject setShareImage:@"https://www.umeng.com/img/index/demo/1104.4b2f7dfe614bea70eea4c6071c72d7f5.jpg"];
//分享消息对象设置分享内容对象
messageObject.shareObject = shareObject;
//调用分享接口
[[UMSocialManager defaultManager] shareToPlatform:platformType messageObject:messageObject currentViewController:self completion:^(id data, NSError *error) {
if (error) {
NSLog(@"************Share fail with error %@*********",error);
}else{
NSLog(@"response data is %@",data);
}
}];
}
5. 分享网页
- (void)shareWebPageToPlatformType:(UMSocialPlatformType)platformType
{
//创建分享消息对象
UMSocialMessageObject *messageObject = [UMSocialMessageObject messageObject];
//创建网页内容对象
UMShareWebpageObject *shareObject = [UMShareWebpageObject shareObjectWithTitle:@"分享标题" descr:@"分享内容描述" thumImage:[UIImage imageNamed:@"icon"]];
//设置网页地址
shareObject.webpageUrl =@"http://mobile.umeng.com/social";
//分享消息对象设置分享内容对象
messageObject.shareObject = shareObject;
//调用分享接口
[[UMSocialManager defaultManager] shareToPlatform:platformType messageObject:messageObject currentViewController:self completion:^(id data, NSError *error) {
if (error) {
NSLog(@"************Share fail with error %@*********",error);
}else{
NSLog(@"response data is %@",data);
}
}];
}
6. 分享音乐
- (void)shareMusicToPlatformType:(UMSocialPlatformType)platformType
{
//创建分享消息对象
UMSocialMessageObject *messageObject = [UMSocialMessageObject messageObject];
//创建音乐内容对象
UMShareMusicObject *shareObject = [UMShareMusicObject shareObjectWithTitle:@"分享标题" descr:@"分享内容描述" thumImage:[UIImage imageNamed:@"icon"]];
//设置音乐网页播放地址
shareObject.musicUrl = @"http://c.y.qq.com/v8/playsong.html?songid=108782194&source=yqq#wechat_redirect";
// shareObject.musicDataUrl = @"这里设置音乐数据流地址(如果有的话,而且也要看所分享的平台支不支持)";
//分享消息对象设置分享内容对象
messageObject.shareObject = shareObject;
//调用分享接口
[[UMSocialManager defaultManager] shareToPlatform:platformType messageObject:messageObject currentViewController:self completion:^(id data, NSError *error) {
if (error) {
NSLog(@"************Share fail with error %@*********",error);
}else{
NSLog(@"response data is %@",data);
}
}];
}
7. 分享视频
- (void)shareVedioToPlatformType:(UMSocialPlatformType)platformType
{
//创建分享消息对象
UMSocialMessageObject *messageObject = [UMSocialMessageObject messageObject];
//创建视频内容对象
UMShareVideoObject *shareObject = [UMShareVideoObject shareObjectWithTitle:@"分享标题" descr:@"分享内容描述" thumImage:[UIImage imageNamed:@"icon"]];
//设置视频网页播放地址
shareObject.videoUrl = @"http://video.sina.com.cn/p/sports/cba/v/2013-10-22/144463050817.html";
// shareObject.videoStreamUrl = @"这里设置视频数据流地址(如果有的话,而且也要看所分享的平台支不支持)";
//分享消息对象设置分享内容对象
messageObject.shareObject = shareObject;
//调用分享接口
[[UMSocialManager defaultManager] shareToPlatform:platformType messageObject:messageObject currentViewController:self completion:^(id data, NSError *error) {
if (error) {
NSLog(@"************Share fail with error %@*********",error);
}else{
NSLog(@"response data is %@",data);
}
}];
}
8. 分享微信表情
- (void)shareEmoticonToPlatformType:(UMSocialPlatformType)platformType
{
UMSocialMessageObject *messageObject = [UMSocialMessageObject messageObject];
UMShareEmotionObject *shareObject = [UMShareEmotionObject shareObjectWithTitle:UMS_Title descr:UMS_Text thumImage:nil];
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"gifFile"
ofType:@"gif"];
NSData *emoticonData = [NSData dataWithContentsOfFile:filePath];
shareObject.emotionData = emoticonData;
messageObject.shareObject = shareObject;
//调用分享接口
[[UMSocialManager defaultManager] shareToPlatform:platformType messageObject:messageObject currentViewController:self completion:^(id data, NSError *error) {
if (error) {
NSLog(@"************Share fail with error %@*********",error);
}else{
if ([data isKindOfClass:[UMSocialShareResponse class]]) {
UMSocialShareResponse *resp = data;
//分享结果消息
NSLog(@"response message is %@",resp.message);
}else{
NSLog(@"response data is %@",data);
}
}
}];
}
9. 分享微信小程序
- (void)shareMiniProgramToPlatformType:(UMSocialPlatformType)platformType
{
//创建分享消息对象
UMSocialMessageObject *messageObject = [UMSocialMessageObject messageObject];
UMShareMiniProgramObject *shareObject = [UMShareMiniProgramObject shareObjectWithTitle:@"小程序标题" descr:@"小程序内容描述" thumImage:[UIImage imageNamed:@"icon"]];
shareObject.webpageUrl = @"兼容微信低版本网页地址";
shareObject.userName = @"小程序username,如 gh_3ac2059ac66f";
shareObject.path = @"小程序页面路径,如 pages/page10007/page10007";
messageObject.shareObject = shareObject;
shareObject.hdImageData = [NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"logo" ofType:@"png"]];
shareObject.miniProgramType = UShareWXMiniProgramTypeRelease; // 可选体验版和开发板
//调用分享接口
[[UMSocialManager defaultManager] shareToPlatform:platformType messageObject:messageObject currentViewController:self completion:^(id data, NSError *error) {
if (error) {
UMSocialLogInfo(@"************Share fail with error %@*********",error);
}else{
if ([data isKindOfClass:[UMSocialShareResponse class]]) {
UMSocialShareResponse *resp = data;
//分享结果消息
UMSocialLogInfo(@"response message is %@",resp.message);
//第三方原始返回的数据
UMSocialLogInfo(@"response originalResponse data is %@",resp.originalResponse);
}else{
UMSocialLogInfo(@"response data is %@",data);
}
}
[self alertWithError:error];
}];
}
10. 示例
分享文本到微信
#import
- (void)shareTextToWechat
{
NSString *text = @"社会化组件U-Share将各大社交平台接入您的应用,快速武装App。";
UMSocialMessageObject *messageObject = [UMSocialMessageObject messageObject];
messageObject.text = text;
[[UMSocialManager defaultManager] shareToPlatform:UMSocialPlatformType_WechatSession messageObject:messageObject currentViewController:self completion:^(id data, NSError *error) {
NSString *message = nil;
if (!error) {
message = [NSString stringWithFormat:@"分享成功"];
} else {
message = [NSString stringWithFormat:@"失败原因Code: %d\n",(int)error.code];
}
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"share"
message:message
delegate:nil
cancelButtonTitle:NSLocalizedString(@"确定", nil)
otherButtonTitles:nil];
[alert show];
}];
}
调用分享面板分享文本到所选平台
#import
#import
- (void)shareWithUI {
//显示分享面板
[UMSocialUIManager showShareMenuViewInWindowWithPlatformSelectionBlock:^(UMSocialPlatformType platformType, NSDictionary *userInfo) {
// 根据获取的platformType确定所选平台进行下一步操作
//创建分享消息对象
UMSocialMessageObject *messageObject = [UMSocialMessageObject messageObject];
//设置文本
messageObject.text = @"社会化组件UShare将各大社交平台接入您的应用,快速武装App。";
//调用分享接口
[[UMSocialManager defaultManager] shareToPlatform:platformType messageObject:messageObject currentViewController:self completion:^(id data, NSError *error) {
if (error) {
NSLog(@"************Share fail with error %@*********",error);
}else{
NSLog(@"response data is %@",data);
}
}];
}];
}
后记
本篇主要介绍了分享的类型以及示例代码,感兴趣的给个赞或者关注~~~