cdts/xdts-ios 3/TreeHole/Code/Utility/Share/MTShareServiceImp.m
2023-07-27 09:20:00 +08:00

221 lines
7.1 KiB
Objective-C

//
// MTShareServiceImp.m
// Meet
//
// Created by ko1o on 2018/10/3.
// Copyright © 2018年 ko1o. All rights reserved.
//
#import "MTShareServiceImp.h"
//#import "WXApi.h"
//#import "WeiboSDK.h"
#import "SVProgressHUD.h"
#import "MTShareService.h"
#import "zhPopupController.h"
#import "TPServiceManager.h"
//#import <UMShare/UMShare.h>
@implementation MTShareMedia
@end
@interface MTShareServiceImp ()
//<WXApiDelegate, WeiboSDKDelegate>
@property (nonatomic, strong) zhPopupController *popVc;
@property (nonatomic, strong) MTShareMedia *media;
@property (nonatomic, strong) UIView *shareToolBar;
@property (nonatomic, strong) NSArray *types;
@end
@implementation MTShareServiceImp
TPSERVICE_AUTO_REGISTER(MTShareService)
+ (instancetype)sharedInstance
{
static MTShareServiceImp *serviceImp = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
serviceImp = [[MTShareServiceImp alloc] init];
serviceImp.popVc = [[zhPopupController alloc] init];
serviceImp.popVc.slideStyle = zhPopupSlideStyleFromBottom;
serviceImp.popVc.layoutType = zhPopupLayoutTypeBottom;
[serviceImp setShareTypes:@[@(MTShareTypeWeChat), @(MTShareTypeWXZone), @(MTShareTypeQQ), @(MTShareTypeQZone)]];
// // 初始化WeiBoSDK
// [WeiboSDK registerApp:MT_SINA_SHARE_APP_ID];
// [WXApi registerApp:WEIXIN_APP_ID];
});
return serviceImp;
}
- (void)setupUI
{
UIView *shareToolBar = [[UIView alloc] init];
shareToolBar.backgroundColor = [UIColor clearColor];
shareToolBar.height = FIX_SIZE(191);
shareToolBar.width = SCREEN_WIDTH;
self.shareToolBar = shareToolBar;
// UILabel *titleLbael = [[UILabel alloc] init];
// titleLbael.y = FIX_SIZE(47.0);
// titleLbael.width = shareToolBar.width;
// titleLbael.height = FIX_SIZE(40*0.5);
// titleLbael.font = MT_FONT_MEDIUM_SIZE(14);
// titleLbael.textColor = TITLE_COLOR;
// titleLbael.textAlignment = NSTextAlignmentCenter;
// titleLbael.text = @"分享至";
// [shareToolBar addSubview:titleLbael];
UIView *topActionView = [[UIView alloc] init];
topActionView.width = SCREEN_WIDTH;
topActionView.height = FIX_SIZE(110);
topActionView.backgroundColor = BG_COLOR;
[topActionView setCorners:UIRectCornerTopLeft | UIRectCornerTopRight radius:FIX_SIZE(20)];
topActionView.clipsToBounds = YES;
[shareToolBar addSubview:topActionView];
NSArray *icons = @[@"TH_wechat_icon", @"TH_wechatspace_icon", @"TH_qq_icon", @"TH_qqspace_icon"];
NSArray *titles = @[@"邀请微信好友", @"朋友圈邀请", @"邀请QQ好友", @"QQ空间邀请"];
NSInteger typesCount = self.types.count;
if (typesCount <= 0) return;
CGFloat imgW = FIX_SIZE(60);
CGFloat btnY = FIX_SIZE(15);
CGFloat marginScale = 0.5;
CGFloat btnMargin = (shareToolBar.width - imgW * typesCount) / typesCount;
CGFloat leftMargin = btnMargin * marginScale;
for (int i = 0; i < typesCount; i++) {
UIView *shareButton = [[UIView alloc] init];
shareButton.clipsToBounds = NO;
UIImageView *imageView = [[UIImageView alloc] initWithImage:ImageNamed(icons[i])];
imageView.size = CGSizeMake(imgW, imgW);
[topActionView addSubview:shareButton];
shareButton.y = btnY;
shareButton.x = leftMargin + (imgW + btnMargin) * i;
shareButton.width = imgW;
[shareButton addSubview:imageView];
UILabel *titleLabel = [[UILabel alloc] init];
titleLabel.text = titles[i];
titleLabel.font = MT_FONT_REGULAR_SIZE(10);
titleLabel.textColor = COLOR_WITH_RGB(0x474747);
[titleLabel sizeToFit];
titleLabel.y = imageView.bottom + FIX_SIZE(6);
titleLabel.centerX = imageView.centerX;
[shareButton addSubview:titleLabel];
shareButton.tag = [self.types[i] intValue];
shareButton.height = titleLabel.bottom;
WeakSelf(self);
WeakSelf(shareButton);
[shareButton addTapWithAction:^{
[weakself share:weakshareButton];
}];
}
UIView *bottomView = [[UIView alloc] init];
bottomView.height = shareToolBar.height;
bottomView.y = topActionView.bottom - 1;
bottomView.width = SCREEN_WIDTH;
bottomView.backgroundColor = UIColor.whiteColor;
[shareToolBar addSubview:bottomView];
UILabel *cancelLabel = [[UILabel alloc] init];
cancelLabel.height = FIX_SIZE(50);
cancelLabel.width = SCREEN_WIDTH;
cancelLabel.y = topActionView.bottom;
cancelLabel.textAlignment = NSTextAlignmentCenter;
cancelLabel.textColor = TITLE_COLOR;
cancelLabel.font = NORMAL_FONT;
cancelLabel.text = @"取消";
WeakSelf(self);
[cancelLabel addTapWithAction:^{
[weakself.popVc dismiss];
}];
[shareToolBar addSubview:cancelLabel];
}
- (void)setShareTypes:(NSArray *)shareTypes
{
if (shareTypes.count == 0) {
shareTypes = @[@(MTShareTypeWeChat), @(MTShareTypeWXZone), @(MTShareTypeQQ), @(MTShareTypeQZone)];
}
_types = shareTypes;
}
- (void)share:(UIView *)sender
{
[self shareWithMedia:_media type:sender.tag];
[self hiddenShareToolBar];
}
- (void)showShareToolBarWithMedia:(MTShareMedia *)media
{
// 清空
[self.shareToolBar removeSubviews];
self.shareToolBar = nil;
[self setupUI];
_media = media;
[self.popVc presentContentView:self.shareToolBar];
}
- (void)hiddenShareToolBar
{
[self.popVc dismiss];
}
- (void)shareWithMedia:(MTShareMedia *)media type:(MTShareType)type
{
// //创建分享消息对象
// UMSocialMessageObject*messageObject = [UMSocialMessageObject messageObject];
//
// //创建网页内容对象
// UMShareWebpageObject*shareObject =[UMShareWebpageObject shareObjectWithTitle:media.title descr:media.content thumImage:media.imageData ?: (media.previewImage ?: media.previewImageURL)];
// //设置网页地址
// shareObject.webpageUrl = media.url.absoluteString;
//
// //分享消息对象设置分享内容对象
// messageObject.shareObject = shareObject;
// UMSocialPlatformType platformType;
// switch (type) {
// case MTShareTypeWeChat:
// platformType = UMSocialPlatformType_WechatSession;
// break;
// case MTShareTypeWXZone:
// platformType = UMSocialPlatformType_WechatTimeLine;
// break;
// case MTShareTypeQQ:
// platformType = UMSocialPlatformType_QQ;
// break;
// case MTShareTypeQZone:
// platformType = UMSocialPlatformType_Qzone;
// break;
//
// default:
// break;
// }
// //调用分享接口
// [[UMSocialManager defaultManager] shareToPlatform:platformType messageObject:messageObject currentViewController:[PYAppService currentViewController] completion:^(id data,NSError*error){
// if (error) {
// [SVProgressHUD showErrorWithStatus:@"分享失败"];
// NSLog(@"************Share fail with error %@*********",error);
// } else {
// [SVProgressHUD showSuccessWithStatus:@"分享成功"];
// NSLog(@"response data is %@",data);
// }
// }];
}
@end