自定义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];

 

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

上一篇:微信新媒体等...笔记


下一篇:Erdaicms旅游网站系统微信和手机端分销系统正式上线发布啦