XWShareView.m 8.05 KB
//
//  XWShareView.m
//  CNLiveScioLive
//
//  Created by CNLive-zxw on 2018/10/20.
//  Copyright © 2018年 CNLive. All rights reserved.
//

#import "XWShareView.h"
#import "TemplateShareManager.h"


@interface XWShareView (){
    UIToolbar              *_toolBar;
    
    UIView                     *_contentView;
    TemplateShareButton        *_weiboButton;
    TemplateShareButton        *_weixinButton;
    TemplateShareButton        *_pengyouquanButton;
    TemplateShareButton        *_qqButton;
    TemplateShareButton        *_qqZoneButton;
    TemplateShareButton        *_txweiboButton;
    TemplateShareButton        *_xlweiboButton;
    TemplateShareButton        *_renrenButton;
    TemplateShareButton        *_reportButton;
    
    NSString                   *_title;
    NSString                   *_shareText;
    NSString                   *_shareImg;
    NSString                   *_shareUrl;
    BOOL                        _isShowing;
    
}

@end

@implementation XWShareView

- (instancetype)initWithFrame:(CGRect)frame ShareImg:(NSString *)shareImg ShareTitle:(NSString *)shareTitle ShareText:(NSString *)shareText ShareUrl:(NSString *)shareUrl{

    self = [super initWithFrame:frame];
    if (self) {
        _isShowing = YES;
        _title = shareTitle;
        _shareText = [self getZZwithString:shareText];
        _shareImg = shareImg;
        _shareUrl = shareUrl;

    }
    return self;
    
}

- (void)dealloc{
    NSLog(@"%s DESTROYED-----------",__func__);
    
}

- (void)layoutSubviews{
    [super layoutSubviews];
    
}

#pragma mark -- Show Method
- (void)show{
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    [window addSubview:self];
    [self mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(window);
    }];
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(btnCancel)];
    [self addGestureRecognizer:tap];
    [self configibutton];
    
}
#pragma mark -- UI
- (void)configibutton{
    
    self.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.3];
    
    _contentView = [[UIView alloc]initWithFrame:CGRectMake(0, MainScreenHeight - 200, MainScreenWidth, 200)];
    _contentView.backgroundColor = [UIColor whiteColor];
    [self addSubview:_contentView];
    
    UILabel *title = [[UILabel alloc]initWithFrame:CGRectMake(100, 15, MainScreenWidth - 200, 22)];
    title.textAlignment = NSTextAlignmentCenter;
    title.text = @"分享到"; //@"分享到";
    title.font = Font(16);
    [_contentView addSubview:title];
    
    UIButton *close = [[UIButton alloc]initWithFrame:CGRectMake(MainScreenWidth - 44, 0, 44, 44)];
    [close setImage:[UIImage imageNamed:@"cnlive_icon"] forState:UIControlStateNormal];
    [close addTarget:self action:@selector(dismiss) forControlEvents:UIControlEventTouchUpInside];
    [_contentView addSubview:close];
    
    CGFloat shareWH = 80, shareMarginS = (MainScreenWidth - 3 * shareWH)/4.0;
    
    //朋友圈
    _pengyouquanButton = [TemplateShareButton buttonWithType:UIButtonTypeCustom];
    _pengyouquanButton.frame = CGRectMake(shareMarginS, 80, 80, 80);
    _pengyouquanButton.shareButtonType = TemplateShareButtonTypePengYouQuan;
    _pengyouquanButton.image = @"朋友圈";//@"朋友圈";
    [_pengyouquanButton addTarget:self action:@selector(didClickShareButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [_contentView addSubview:_pengyouquanButton];
    
    //微信
    _weixinButton = [TemplateShareButton buttonWithType:UIButtonTypeCustom];
    _weixinButton.frame = CGRectMake(2 * shareMarginS + shareWH, 80, 80, 80);
    _weixinButton.shareButtonType = TemplateShareButtonTypeWeiXin;
    _weixinButton.image = @"微信";//@"微信";
    [_weixinButton addTarget:self action:@selector(didClickShareButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [_contentView addSubview:_weixinButton];
    
    //新浪微博
    _xlweiboButton = [TemplateShareButton buttonWithType:UIButtonTypeCustom];
    _xlweiboButton.frame = CGRectMake(3 * shareMarginS + 2 * shareWH, 80, 80, 80);
    _xlweiboButton.shareButtonType = TemplateShareButtonTypeXLWeiBo;
//    _xlweiboButton.image = CustomStr(@"Weibo");//@"新浪微博";
    _xlweiboButton.image = @"新浪微博";//@"新浪微博";
    [_xlweiboButton addTarget:self action:@selector(didClickShareButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [_contentView addSubview:_xlweiboButton];
    
}

//取消分享
- (void)dismiss{
    [self removeFromSuperview];
}

#pragma mark -- Event Response
//取消分享
- (void)btnCancel{
    [self removeFromSuperview];
    
}
- (void)didClickShareButtonAction:(TemplateShareButton *)button{
    [self dismiss];
    switch (button.shareButtonType) {
            
        case TemplateShareButtonTypePengYouQuan:case TemplateShareButtonTypeWeiXin:
            if ([WXApi isWXAppInstalled] && [WXApi isWXAppSupportApi]){
                TemplateShareManager *manger = [TemplateShareManager shareSingleton];
                [manger shareType:button.shareButtonType shareImage:_shareImg shareTitle:_title shareText:_shareText shareUrl:_shareUrl];
                
            }
            else{
                //未安装微信

            }
            
            break;
        case TemplateShareButtonTypeXLWeiBo:
            
            if ([WeiboSDK isCanShareInWeiboAPP] || [WeiboSDK isCanSSOInWeiboApp]){
                TemplateShareManager *manger = [TemplateShareManager shareSingleton];
                [manger shareType:button.shareButtonType shareImage:_shareImg shareTitle:_title shareText:_shareText shareUrl:_shareUrl];
                
            }
            else{
                //未安装新浪微博

            }
            break;
            
        case TemplateShareButtonTypeQQ:case TemplateShareButtonTypeQQZone:
            
            if ([QQApiInterface isQQInstalled]){
                TemplateShareManager *manger = [TemplateShareManager shareSingleton];
                [manger shareType:button.shareButtonType shareImage:_shareImg shareTitle:_title shareText:_shareText shareUrl:_shareUrl];
                
            }
            else{
                //未安装qq
            }
            break;
            
        default:
            break;
    }
    
}
#pragma mark - 去标签Tool
- (NSString *)getZZwithString:(NSString *)string{
    
    if (!string || [string isEqualToString:@""] || [(NSNull *)string isEqual:[NSNull null]]) {
        return @"";
    }
    
    
    NSRegularExpression *regularScriptExpretion=[NSRegularExpression regularExpressionWithPattern:@"<script[^>]*?>[\\s\\S]*?<\\/script>"
                                                                                          options:0
                                                                                            error:nil];
    
    string=[regularScriptExpretion stringByReplacingMatchesInString:string options:NSMatchingReportProgress range:NSMakeRange(0, string.length) withTemplate:@""];
    
    
    
    NSRegularExpression *regularExpretion=[NSRegularExpression regularExpressionWithPattern:@"<style[^>]*?>[\\s\\S]*?<\\/style>"
                                                                                    options:0
                                                                                      error:nil];
    string=[regularExpretion stringByReplacingMatchesInString:string options:NSMatchingReportProgress range:NSMakeRange(0, string.length) withTemplate:@""];
    
    
    
    
    NSRegularExpression *regularHtmlExpretion=[NSRegularExpression regularExpressionWithPattern:@"<[^>]+>"
                                                                                        options:0
                                                                                          error:nil];
    string=[regularHtmlExpretion stringByReplacingMatchesInString:string options:NSMatchingReportProgress range:NSMakeRange(0, string.length) withTemplate:@""];
    
    
    
    string = [string stringByReplacingOccurrencesOfString:@"&nbsp;" withString:@""];  // 过滤&nbsp等标签
    
    return string;
}

@end