提要:(后面粘上代码!!!如果就是按照我的方法实现,直接复制代码,稍作调整即可)
本文着重介绍:就本人使用代码实现<友盟分享弹出的选择界面的自定义方法、不同的按钮实现相应的第三方分享>
1、项目需求
1.1.1:
下面是我们项目的需求图:(如果用原生的是一行四个按钮)
1.1.2:
友盟注册,以及SDK下载和导入项目不在说,直接进入正题。(注意,6.0.1的SDK对类名做了替换,大家AppDelegate里面也要替换)
2、界面
2.1.1:
本人使用了九宫格布局,为了方便大家,我给大家提供一个链接地址:GitHub - GeekZP/ZPActionSheet: 简单的两种弹窗方式 一种适用于9宫格类型的分享,另一种适用于替换系统的actionsheet,这个就是直接上面的UI效果,很方便的。
3、项目代码
3.1.1:
在你点击分享按钮里写入一下代码:
NSArray *titlearr = @[@"微信好友",@"微信朋友圈",@"QQ好友",@"QQ空间",@"新浪微博",@"复制链接"];
NSArray *imageArr = @[@"UMS_wechat_session_icon",@"UMS_wechat_timeline_icon",@"UMS_qq_icon",@"UMS_qzone_icon",@"UMS_sina_icon",@"fuzhilainjie"];
ActionSheetView *actionsheet = [[ActionSheetView alloc] initWithShareHeadOprationWith:titlearr andImageArry:imageArr andProTitle:@"测试" and:ShowTypeIsShareStyle];
说明:imageArr:图片字典 titlearr:文字字典
上面的代码就可以实现了上面的UI效果。
3.1.2
在刚才的代码下面加入以下代码:
[actionsheet setBtnClick:^(NSInteger btnTag) {
// 这里面可以加入分享到某个第三方的点击
}
在上面的大括号里实现下面图片里的判断方法:
说明:“btnTag == 0” 就是点击了九宫格第一个按钮触发的方法,以此类推
——————————————————————————————————3.1.3(6.0.1之前的版本)——————
——————————————————————————
在if判断里面写入下面的代码实现分享:【功能实现后,可以自行提取一下】
注意:图片的红框框住的是对应第三方平台的,记得更改,微信,朋友圈,QQ,QQ空间,他们四个是一样的,但是,但是,但是——————————————新浪微博————————————————是独特的,它的实现方法是下面这样的:
注意到了吗,微博的标题和链接必须是写在一起的,就是我红框框住的地方。
到此就可以实现分享功能了。
——————————————————————————————————3.1.3(6.0.1最新的版本)——————
————————————————————————
在if判断里面写入下面的代码实现分享:【功能实现后,可以自行提取一下】
在这里我就以分享带图片、文字和链接为例-----------------------------
先创建分享信息对象,title:标题; descr:详情; image:图片链接;
webpageUrl:网址链接;
接下来是点击按钮后,用到的方法:
注意:图片的红框框住的是对应第三方平台的,记得更改,微信,朋友圈,QQ,QQ空间,他们四个是一样的,但是,但是,但是——————————————新浪微博————————————————是独特的,它的实现方法是下面这样的:
注意到了吗,微博的标题和链接必须是写在一起的,就是我红框框住的地方。
到此就可以实现分享功能了。
不过,我把复制链接的方法也分享给大家吧:
UIPasteboard *pastboad = [UIPasteboard generalPasteboard];
pastboad.string = @"链接地址";
就已经放在粘贴板上面了。
这是代码部分(我没有提取,微信,朋友圈,QQ,QQ空间内容一样的,大家可自行提取):
旧版:
/**友盟分享*/
- (void)rightBarButtonAction
{
NSArray *titlearr = @[@"微信好友",@"微信朋友圈",@"QQ好友",@"QQ空间",@"新浪微博",@"复制链接"];
NSArray *imageArr = @[@"UMS_wechat_session_icon",@"UMS_wechat_timeline_icon",@"UMS_qq_icon",@"UMS_qzone_icon",@"UMS_sina_icon",@"fuzhilainjie"];
UIImage *cachedImage = [[SDImageCache sharedImageCache] imageFromDiskCacheForKey:self.imgUrl];
ActionSheetView *actionsheet = [[ActionSheetView alloc] initWithShareHeadOprationWith:titlearr andImageArry:imageArr andProTitle:@"测试" and:ShowTypeIsShareStyle];
[actionsheet setBtnClick:^(NSInteger btnTag) {
[UMSocialData defaultData].extConfig.title = self.shareTitle;
[WXApi isWXAppInstalled];
// 显示在前面的图片
if (btnTag ==0) {
[UMSocialData defaultData].extConfig.wechatSessionData.url = self.ashareurl;
[UMSocialData defaultData].extConfig.wechatSessionData.title = self.titleName;
[[UMSocialDataService defaultDataService] postSNSWithTypes:@[UMShareToWechatSession]
content:self.describes
image:cachedImage
location:nil
urlResource:nil
presentedController:self completion:^(UMSocialResponseEntity *response){
if (response.responseCode == UMSResponseCodeSuccess) {
NSLog(@"分享成功");
}
}];
}
else if (btnTag ==1)
{
[UMSocialData defaultData].extConfig.wechatTimelineData.url = self.ashareurl;
[UMSocialData defaultData].extConfig.wechatTimelineData.title = self.titleName;
[[UMSocialDataService defaultDataService] postSNSWithTypes:@[UMShareToWechatTimeline]
content:self.describes
image:cachedImage
location:nil
urlResource:nil
presentedController:self completion:^(UMSocialResponseEntity *response){
if (response.responseCode == UMSResponseCodeSuccess) {
NSLog(@"分享成功");
}
}];
}
else if (btnTag ==2)
{
[UMSocialData defaultData].extConfig.qqData.url = self.ashareurl;
[UMSocialData defaultData].extConfig.qqData.title = self.titleName;
[[UMSocialDataService defaultDataService] postSNSWithTypes:@[UMShareToQQ]
content:self.describes
image:cachedImage
location:nil
urlResource:nil
presentedController:self completion:^(UMSocialResponseEntity *response){
if (response.responseCode == UMSResponseCodeSuccess) {
NSLog(@"分享成功");
}
}];
}
else if (btnTag ==3)
{
[UMSocialData defaultData].extConfig.qzoneData.url = self.ashareurl;
[UMSocialData defaultData].extConfig.qzoneData.title = self.titleName;
[[UMSocialDataService defaultDataService] postSNSWithTypes:@[UMShareToQzone]
content:self.describes
image:cachedImage
location:nil
urlResource:nil
presentedController:self completion:^(UMSocialResponseEntity *response){
if (response.responseCode == UMSResponseCodeSuccess) {
NSLog(@"分享成功");
}
}];
}
else if (btnTag ==4)
{
[[UMSocialDataService defaultDataService] postSNSWithTypes:@[UMShareToSina]
content:[NSString stringWithFormat:@"%@%@",self.describes,self.ashareurl]
image:cachedImage
location:nil
urlResource:nil
presentedController:self completion:^(UMSocialResponseEntity *response){
if (response.responseCode == UMSResponseCodeSuccess) {
NSLog(@"分享成功");
}
}];
}
else if (btnTag ==5)
{
UIPasteboard *pastboad = [UIPasteboard generalPasteboard];
pastboad.string = self.ashareurl;
ZTShowAlertMessage(@"复制链接成功");
}
}];
[[UIApplication sharedApplication].keyWindow addSubview:actionsheet];
}
新版:
/**友盟分享*/
- (void)secondBarButtonAction
{
NSArray *titlearr = @[@"微信好友",@"微信朋友圈",@"QQ好友",@"QQ空间",@"新浪微博",@"复制链接"];
NSArray *imageArr = @[@"UMS_wechat_session_icon",@"UMS_wechat_timeline_icon",@"UMS_qq_icon",@"UMS_qzone_icon",@"UMS_sina_icon",@"fuzhilainjie"];
ActionSheetView *actionsheet = [[ActionSheetView alloc] initWithShareHeadOprationWith:titlearr andImageArry:imageArr andProTitle:@"测试" and:ShowTypeIsShareStyle];
[actionsheet setBtnClick:^(NSInteger btnTag) {
//创建分享消息对象
UMSocialMessageObject *messageObject = [UMSocialMessageObject messageObject];
//创建网页内容对象
UMShareWebpageObject *shareObject = [UMShareWebpageObject shareObjectWithTitle:self.titleName descr:self.describes thumImage:self.shareImageurl];
//设置网页地址
shareObject.webpageUrl = self.ashareurl;
//分享消息对象设置分享内容对象
messageObject.shareObject = shareObject;
// 显示在前面的图片
if (btnTag ==0) {
//调用分享接口
[[UMSocialManager defaultManager] shareToPlatform:UMSocialPlatformType_WechatSession 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);
//第三方原始返回的数据
NSLog(@"response originalResponse data is %@",resp.originalResponse);
}else{
NSLog(@"response data is %@",data);
}
}
}];
}
else if (btnTag ==1)
{
[[UMSocialManager defaultManager] shareToPlatform:UMSocialPlatformType_WechatTimeLine 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);
//第三方原始返回的数据
NSLog(@"response originalResponse data is %@",resp.originalResponse);
}else{
NSLog(@"response data is %@",data);
}
}
}];
}
else if (btnTag ==2)
{
[[UMSocialManager defaultManager] shareToPlatform:UMSocialPlatformType_QQ 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);
//第三方原始返回的数据
NSLog(@"response originalResponse data is %@",resp.originalResponse);
}else{
NSLog(@"response data is %@",data);
}
}
}];
}
else if (btnTag ==3)
{
[[UMSocialManager defaultManager] shareToPlatform:UMSocialPlatformType_Qzone 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);
//第三方原始返回的数据
NSLog(@"response originalResponse data is %@",resp.originalResponse);
}else{
NSLog(@"response data is %@",data);
}
}
}];
}
else if (btnTag ==4)
{
UMSocialMessageObject *messageObject = [UMSocialMessageObject messageObject];
messageObject.text = [NSString stringWithFormat:@"%@%@",self.titleName, self.ashareurl];
//创建图片内容对象
UMShareImageObject *shareObject = [[UMShareImageObject alloc] init];
//如果有缩略图,则设置缩略图
[shareObject setShareImage:self.shareImageurl];
//分享消息对象设置分享内容对象
messageObject.shareObject = shareObject;
[[UMSocialManager defaultManager] shareToPlatform:UMSocialPlatformType_Sina 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);
//第三方原始返回的数据
NSLog(@"response originalResponse data is %@",resp.originalResponse);
}else{
NSLog(@"response data is %@",data);
}
}
}];
}
else if (btnTag ==5)
{
UIPasteboard *pastboad = [UIPasteboard generalPasteboard];
pastboad.string = self.ashareurl;
ZTShowAlertMessage(@"复制链接成功");
}
}];
[[UIApplication sharedApplication].keyWindow addSubview:actionsheet];
}