自定义UI集成微信、QQ、微博分享功能

  目前社会化分享是一个非常常见的功能,通过阅读官方文档可以进行对应平台的分享。在项目中原本有微信的分享,后来需要集成QQ和微博的分享,于是想着用ShareSDK,在使用的过程中发现ShareSDK中的weChatSDK与原来的不同,导致原来的微信注册与登录功能无法使用,具体原因不详。没办法只能手动集成(看到安卓的小伙伴没有问题,我就不开心了,让我哭三声??????)。首先要解决UI问题,因为苹果不允许推荐第三方应用,只能通过判断去隐藏微信、QQ图标(如果用户没有安装)。本人一直都觉得没有设计天赋,自定义的UI比较难看,望各位大神勿喷。下面是UI的代码。

  背景view,.h文件就不粘贴了,很简单。

 1 #import "ALShareBackView.h"
 2
 3 @implementation ALShareBackView
 4
 5 - (instancetype)initWithFrame:(CGRect)frame
 6 {
 7     if (self = [super initWithFrame:frame])
 8     {
 9
10         self.backgroundColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.5];
11     }
12     return self;
13 }
14
15 - (void)setCustomView:(ALShareCustomView *)customView
16 {
17     if (_customView != customView)
18     {
19         _customView = customView;
20     }
21     [self addSubview:_customView];
22 }
23
24 @end

  UI主要代码如下。

 1 #import <UIKit/UIKit.h>
 2
 3 typedef enum shareType{
 4     ShareToQQ = 0,
 5     ShareToQQZone,
 6     ShareToWeiXin,
 7     ShareToFriend,
 8     ShareToSine
 9 }shareType;
10
11 typedef void(^shareBlock)(shareType);
12 typedef void(^cancelBlock)();
13
14 @interface ALShareCustomView : UIView
15
16 @property (nonatomic, copy) shareBlock shareBlock;
17 @property (nonatomic, copy) cancelBlock cancelBlock;
18
19 + (CGFloat)operateHeightWith:(CGFloat)screenW;
20
21 @end
  1 #define numberOfLine 4
  2 #import "ALShareCustomView.h"
  3 #import "WXApi.h"
  4 #import <TencentOpenAPI/QQApiInterface.h>
  5 #import <TencentOpenAPI/QQApiInterfaceObject.h>
  6
  7 @interface ALShareCustomView ()
  8 @property (nonatomic, strong) NSArray *pictureArray;
  9 @property (nonatomic, strong) NSArray *titleArray;
 10 @end
 11
 12 @implementation ALShareCustomView
 13
 14 + (CGFloat)operateHeightWith:(CGFloat)screenW
 15 {
 16     NSMutableArray *shareArray = [NSMutableArray array];
 17
 18     //先确定有哪些可以进行分享的平台
 19     if ([QQApiInterface isQQInstalled])
 20     {
 21         [shareArray addObject:@"share_qq"];
 22         [shareArray addObject:@"share_qq_zone"];
 23     }
 24     if ([WXApi isWXAppInstalled])
 25     {
 26         [shareArray addObject:@"share_weixin"];
 27         [shareArray addObject:@"share_friend"];
 28     }
 29     [shareArray addObject:@"share_sine"];
 30     float leftGap = 10;
 31     float midGap = 20;
 32     float imageWidth = (screenW-20-(leftGap+midGap)*2)/numberOfLine;
 33     //计算高度 30+10+ x * (10+)
 34     float viewHeight = 30+10+([shareArray count]/numberOfLine+1)*(imageWidth+15+10+10)+10*2+44;
 35     return viewHeight;
 36 }
 37
 38 - (instancetype)initWithFrame:(CGRect)frame
 39 {
 40     if (self = [super initWithFrame:frame])
 41     {
 42         self.pictureArray = @[@"share_qq", @"share_qq_zone", @"share_weixin", @"share_friend", @"share_sine"];
 43         self.titleArray = @[@"QQ", @"QQ空间", @"微信", @"朋友圈", @"新浪微博"];
 44         [self createUI];
 45     }
 46     return self;
 47 }
 48
 49 - (void)createUI
 50 {
 51     NSMutableArray *shareArray = [NSMutableArray array];
 52     NSMutableArray *shareTitle = [NSMutableArray array];
 53     //先确定有哪些可以进行分享的平台
 54     if ([QQApiInterface isQQInstalled])
 55     {
 56         [shareArray addObject:@"share_qq"];
 57         [shareArray addObject:@"share_qq_zone"];
 58         [shareTitle addObject:@"QQ"];
 59         [shareTitle addObject:@"QQ空间"];
 60     }
 61     if ([WXApi isWXAppInstalled])
 62     {
 63         [shareArray addObject:@"share_weixin"];
 64         [shareArray addObject:@"share_friend"];
 65         [shareTitle addObject:@"微信"];
 66         [shareTitle addObject:@"朋友圈"];
 67     }
 68     [shareTitle addObject:@"新浪微博"];
 69     [shareArray addObject:@"share_sine"];
 70
 71     float leftGap = 10;
 72     float midGap = 20;
 73     float imageWidth = (self.frame.size.width-40-midGap*(numberOfLine-1))/numberOfLine;
 74
 75     UIView *backView = [[UIView alloc] initWithFrame:CGRectMake(10, 0, self.frame.size.width-20, self.frame.size.height-20-44)];
 76     backView.backgroundColor = [UIColor whiteColor];
 77     backView.layer.cornerRadius = 8;
 78     backView.layer.masksToBounds = YES;
 79     [self addSubview:backView];
 80
 81     UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 10, self.frame.size.width-20, 20)];
 82     titleLabel.backgroundColor = nil;
 83     titleLabel.text = @"分享到";
 84     titleLabel.font = [UIFont systemFontOfSize:17];
 85     titleLabel.textColor = [UIColor blackColor];
 86     titleLabel.textAlignment = NSTextAlignmentCenter;
 87     [backView addSubview:titleLabel];
 88
 89     for (int i=0; i<[shareArray count]; i++)
 90     {
 91         UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(10+(i%numberOfLine*(imageWidth+midGap)), CGRectGetMaxY(titleLabel.frame)+10+i/numberOfLine*(imageWidth+15+10+10), imageWidth, imageWidth)];
 92         imageView.image = [UIImage imageNamed:shareArray[i]];
 93         imageView.tag = 10010+[self.pictureArray indexOfObject:shareArray[i]];
 94         imageView.userInteractionEnabled = YES;
 95         [backView addSubview:imageView];
 96         UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, imageWidth+10, 15)];
 97         label.top = imageView.bottom+5;
 98         label.centerX = imageView.centerX;
 99         label.backgroundColor = [UIColor whiteColor];
100         label.font = [UIFont systemFontOfSize:14];
101         label.text = shareTitle[i];
102         label.textColor = [UIColor blackColor];
103         label.textAlignment = NSTextAlignmentCenter;
104         [backView addSubview:label];
105
106         UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(selectImage:)];
107         [imageView addGestureRecognizer:tap];
108
109     }
110
111     UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
112     button.frame = CGRectMake(10, CGRectGetMaxY(backView.frame)+10, self.frame.size.width-20, 44);
113     button.backgroundColor = [UIColor whiteColor];
114     button.layer.cornerRadius = 8.0;
115     [button setTitle:@"取消" forState:UIControlStateNormal];
116     [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
117     [button addTarget:self action:@selector(cancel) forControlEvents:UIControlEventTouchUpInside];
118     [self addSubview:button];
119
120 }
121
122 - (void)selectImage:(UITapGestureRecognizer *)gesture
123 {
124     shareType type;
125     /*
126      ShareToQQ = 0
127      ShareToQQZone
128      ShareToWeiXin
129      ShareToFriend
130      ShareToSine
131      */
132     switch (gesture.view.tag-10010)
133     {
134         case 0:
135             type = ShareToQQ;
136             break;
137         case 1:
138             type = ShareToQQZone;
139             break;
140         case 2:
141             type = ShareToWeiXin;
142             break;
143         case 3:
144             type = ShareToFriend;
145             break;
146         case 4:
147             type = ShareToSine;
148             break;
149         default:
150             break;
151     }
152     self.shareBlock(type);
153 }
154
155 - (void)cancel
156 {
157     self.cancelBlock();
158 }
159
160 @end

  分享管理类:

  1 #import "ALShareManager.h"
  2 #import "YYShareManager.h"
  3 #import <TencentOpenAPI/TencentOAuth.h>
  4 #import <TencentOpenAPI/QQApiInterface.h>
  5 #import <TencentOpenAPI/QQApiInterfaceObject.h>
  6 #import "WeiboSDK.h"
  7
  8 @interface ALShareManager ()
  9
 10 @end
 11
 12 @implementation ALShareManager
 13
 14 + (void)shareWith:(NSDictionary *)infoDic andShareType:(shareType)shareType
 15 {
 16     ALShareManager *shareManager = [[ALShareManager alloc] init];
 17     switch (shareType)
 18     {
 19         case ShareToQQ:
 20             [shareManager shareToQQ:infoDic];
 21             break;
 22         case ShareToQQZone:
 23             [shareManager shareToQQZone:infoDic];
 24             break;
 25         case ShareToWeiXin:
 26             [shareManager shareToWeiXin:infoDic];
 27             break;
 28         case ShareToFriend:
 29             [shareManager shareToFriend:infoDic];
 30             break;
 31         case ShareToSine:
 32             [shareManager shareToWeiBo:infoDic];
 33             break;
 34         default:
 35             break;
 36     }
 37 }
 38
 39 #pragma mark --分享到qq
 40 - (void)shareToQQ:(NSDictionary *)infoDic
 41 {
 42
 43     TencentOAuth *tencentOAuth = [[TencentOAuth alloc] initWithAppId:@"1105494105" andDelegate:nil];
 44
 45     QQApiNewsObject *newsObj = [QQApiNewsObject
 46                                 objectWithURL:[NSURL URLWithString:SHARE_LINK]
 47                                 title:@" "
 48                                 description:[infoDic formateObjectForKey:@"content"]
 49                                 previewImageURL:[infoDic formateObjectForKey:@"image"] ];
 50     SendMessageToQQReq *req = [SendMessageToQQReq reqWithContent:newsObj];
 51     [QQApiInterface sendReq:req];
 52     NSLog(@"qq");
 53 }
 54
 55 #pragma mark --分享到qq空间
 56 - (void)shareToQQZone:(NSDictionary *)infoDic
 57 {
 58     TencentOAuth *tencentOAuth = [[TencentOAuth alloc] initWithAppId:@"1105494105" andDelegate:nil];
 59     QQApiNewsObject *newsObj = [QQApiNewsObject
 60                                 objectWithURL:[NSURL URLWithString:SHARE_LINK]
 61                                 title:@" "
 62                                 description:[infoDic formateObjectForKey:@"content"]
 63                                 previewImageURL:[infoDic formateObjectForKey:@"image"] ];
 64     SendMessageToQQReq *req = [SendMessageToQQReq reqWithContent:newsObj];
 65     [QQApiInterface SendReqToQZone:req];
 66     NSLog(@"qqzone");
 67 }
 68 #pragma mark --分享到微信
 69 - (void)shareToWeiXin:(NSDictionary *)infoDic
 70 {
 71     NSLog(@"weixin");
 72     [YYShareManager shareInfoToWechat:infoDic];
 73 }
 74 #pragma mark --分享到朋友圈
 75 - (void)shareToFriend:(NSDictionary *)infoDic
 76 {
 77     NSLog(@"friend");
 78     [YYShareManager shareInfoToMoment:infoDic];
 79 }
 80 #pragma mark --分享到微博
 81 - (void)shareToWeiBo:(NSDictionary *)infoDic
 82 {
 83     WBAuthorizeRequest *authRequest = [WBAuthorizeRequest request];
 84     authRequest.redirectURI = SINA_URL;
 85     authRequest.scope = @"all";
 86
 87     WBSendMessageToWeiboRequest *request = [WBSendMessageToWeiboRequest requestWithMessage:[self messageToShare:[infoDic formateObjectForKey:@"content"]] authInfo:authRequest access_token:nil];
 88 //    request.userInfo = @{@"ShareMessageFrom": @"SendMessageToWeiboViewController",
 89 //                         @"Other_Info_1": [NSNumber numberWithInt:123],
 90 //                         @"Other_Info_2": @[@"obj1", @"obj2"],
 91 //                         @"Other_Info_3": @{@"key1": @"obj1", @"key2": @"obj2"}};
 92 ////    request.shouldOpenWeiboAppInstallPageIfNotInstalled = NO;
 93     [WeiboSDK sendRequest:request];
 94
 95     NSLog(@"weibo");
 96 }
 97
 98 - (WBMessageObject *)messageToShare:(NSString *)content
 99 {
100     WBMessageObject *message = [WBMessageObject message];
101
102     if (0)
103     {
104         message.text = NSLocalizedString(content, nil);
105     }
106
107     if (0)
108     {
109         WBImageObject *image = [WBImageObject object];
110         image.imageData = [NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"image_1" ofType:@"jpg"]];
111         message.imageObject = image;
112     }
113
114     if (1)
115     {
116         WBWebpageObject *webpage = [WBWebpageObject object];
117         webpage.objectID = @"dule";
118         webpage.title = NSLocalizedString(@"头条", nil);
119         webpage.description = [NSString stringWithFormat:NSLocalizedString(content, nil), [[NSDate date] timeIntervalSince1970]];
120
121 //        webpage.thumbnailData = [NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"erweima" ofType:@"png"]];
122
123         webpage.webpageUrl = @"http://sina.cn?a=1";
124         message.mediaObject = webpage;
125     }
126
127     return message;
128 }
129
130 @end

  上述文件中分享至微信和朋友圈的方式实现是另一个类中的方法,在这里也就不写了,小伙伴可以自己写下。

  最后给下实力代码:

 1 ALShareBackView *shareBackView = [[ALShareBackView alloc] initWithFrame:[UIScreen mainScreen].bounds];
 2     __weak typeof(shareBackView)weakShareBackView = shareBackView;
 3     ALShareCustomView *shareCustomView = [[ALShareCustomView alloc] initWithFrame:CGRectMake(0, SCREEN_HEIGHT-[ALShareCustomView operateHeightWith:SCREEN_WIDTH], SCREEN_WIDTH, [ALShareCustomView operateHeightWith:SCREEN_WIDTH])];
 4     shareBackView.customView = shareCustomView;
 5     shareCustomView.shareBlock = ^(shareType shareType){
 6         NSMutableDictionary *infoDic = [NSMutableDictionary dictionary];
 7         NSString *detail = @"负贵指数72,超过85%的人...别笑,你不一定比我好!久坐一族一定要测!";
 8         [infoDic setObject:SHARE_LINK forKey:@"url"];
 9         [infoDic setObject:[NSURL URLWithString:LOGO_SHARE_URL] forKey:@"image"];
10         [infoDic setObject:detail forKey:@"content"];
11
12         [ALShareManager shareWith:infoDic andShareType:shareType];
13         [weakShareBackView removeFromSuperview];
14     };
15     shareCustomView.cancelBlock = ^{
16         [weakShareBackView removeFromSuperview];
17     };
18     [[UIApplication sharedApplication].keyWindow addSubview:shareBackView];
时间: 2024-08-10 16:04:13

自定义UI集成微信、QQ、微博分享功能的相关文章

Android集成友盟社会化分享功能

1.  产品概述 友盟社会化组件,可以让移动应用快速具备社会化分享.登录.评论.喜欢等功能,并提供实时.全面的社会化数据统计分析服务. 指南将会手把手教你使用社会化组件SDK,用5分钟为APP增加新浪微博.腾讯微博.人人网分享功能. 注意:本文示例代码均针对最新版SDK,如果你所用SDK的类名或方法名与此文不符合,请使用你所用SDK的随包文档.或者下载使用最新版SDK. 2.  获取友盟Appkey 如果你之前已经在友盟注册了应用,并获取到了Appkey,可以继续使用它. 如果你尚未在友盟注册开

微信js_sdk的分享功能的问题

微信js_sdk的分享功能的问题 调用失败的原因1 没有在wx.ready(function () {}函数中调用分享功能的函数 修改为: wx.ready(function () { wx.onMenuShareTimeline({ title: '', // 分享标题 link: '', // 分享链接 imgUrl: '', // 分享图标 success: function () { // 用户确认分享后执行的回调函数 }, cancel: function () { // 用户取消分享

史上最详细Android集成QQ,微信,微博分享(不用第三方)持续更新中

QQ 1 下载sdk并运行demo 2 各种配置 IUiListener 4 分享图片 41 分享到QQ 41 分享到QZONE Sina微博 配置 选择集成sdk方式 分享图片 现在项目中用个第三方登录,或分享已经不是什么稀奇事了,但是要想把这个功能做好,那可就不容易了.估计到这里就有人会说了,扯犊子,这玩意,我用第三方sdk,什么umeng,sharesdk分分钟给你做出来,可是这些第三方sdk只有对项目的分享要求不高的时候才适合,如果要定制分享,那我就只能呵呵.虽然第三方登录或分享没什么技

h5怎么做分享到QQ 、朋友圈、微信 、微博等功能

微信已经成为我们日常聊天联系基本的必备工具,所以小菜我首先介绍一下如何调用微信的分享功能.其实除了可以在微信上分享到朋友圈和发送给好友,微信的分享接口还提供了分享到QQ和分享到腾讯微博等,就是在页面的config接口注入权限验证配置好就ok! 类似于"分享到朋友圈"按钮点击状态及自定义分享内容接口,我们调用"分享到QQ"和"分享到腾讯微博"按钮点击状态及自定义分享内容接口 . 获取"分享到QQ"按钮点击状态及自定义分享内容接口

如何在自己的代码中实现分享视频文件或者是图片文件到微信 QQ微博 新浪微博等!!!

首先在文档第一句我先自嘲下 , 我是大傻逼, 弄了两天微信是视频分享,一直被说为啥跟系统的相册分享的不一样,尼玛!!! 这里来说正文,我这里不像多少太多,大家都是程序猿,具体的阔以看代码. 搞代码之前先啰嗦几句啊,你们从我的这个code中你们会学到what, first:分享到微信,新浪.etc无需你注册appkey,哇,这么吊,是的就是这么屌,等你们看完代码就知道咯,略屌... 好吧!!!来看怎么不许申请appkey轻松做到分享!!! 我这里只拿微信的坐下demo了啊,别的一个样!!! pri

微信app的分享功能

最近在做微信app,需要用到分享功能,横观文档,压根没有提过分享功能自定义的事情……后来在搜索中找到一些前辈的文章,使用WeixinJSBridge这个接口实现,但是,我非常非常好奇,这是什么渠道透露出来的 ||_|| var options = { "appid": '', //可以不设置 但必须存在这属性 "img_url": '', //分享图片的路径 "img_width": "200", "img_heig

微信JS-SDK实际分享功能

为了净化网络,整顿诱导分享及诱导关注行为,微信于2014年12月30日发布了<微信公众平台关于整顿诱导分享及诱导关注行为的公告>,微信平台开发者发现,原有的微信分享功能不能用了,在iphone手机上,显示为当前页的链接,之前设置的图标和标题等都没有了.虽然在部分android手机上还可以用,但最近微信升级后,分享功能也不正常了.正在苦于微信分享该怎么解决时,微信于2015年1月10日即时发布了开放JS-SDK,为微信网站的开发提供了强大的js功能.这里仅就分享功能的使用进行一些描述. 下面的代

微信二次分享功能开发笔记

最近的一个项目在进行二次分享的时候出现了问题,定制的文案描述及图片都不能在分享后出现,如图所示: 经过十八般折腾,终于变成我想要的样式了,如图:    下面讲讲具体如何实现的. 首先,明白我们的需求是使由app分享到微信后的页面可以使用微信的二次分享功能做定制化的分享(如图所示) 然后开始着手做相关功能开发. 根据微信开发文档,知晓我们需要调用微信的api,获取对应的分享功能(朋友.朋友圈.qq)等,还需要一个对应的公众号或者服务号.理清思路后,接下来介绍关于各部分的具体处理,见下: 本地代码处

QQ空间分享功能代码

1 <!DOCTYPE HTML> 2 <html> 3 <head> 4 <title>QQ空间分享</title> 5 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 6 </head> 7 <body> 8 <input type="button"