ShareView.m 12.1 KB
//
//  ShareView.m
//  IVTMeLiveTwo
//
//  Created by XRG on 16/8/11.
//  Copyright © 2016年 QLQ. All rights reserved.
//

#import "ShareView.h"
#import "WXApiObject.h"
#import "WXApi.h"
#import <TencentOpenAPI/TencentOAuth.h>
#import <TencentOpenAPI/QQApiInterface.h>
#import "WeiboSDK.h"
#import "AppDelegate.h"

#define kShareImageName @"icon-share.png"

static ShareView *instance = nil;

@interface ShareView ()
{
//    UIView *bgView;
    UIButton *wechatSessionButton;
    UIButton *wechatTimelineButton;
    UIButton *qqButton;
    UIButton *qzoneButton;
    UIButton *sinaButton;
    
    ShareContentModel *shareEntity;
}

@end

@implementation ShareView

+ (ShareView *)sharedInstance{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        instance = [[self alloc] init];
        [instance setupView];
    });
    return instance;
}

- (void)setupView{
    self.frame = [[UIScreen mainScreen] bounds];
    self.backgroundColor = [UIColor clearColor];
    
    /**
     *  创建显示各个分享按钮的view
     */
    CGFloat space = (kScreenWidth - 5*50)/6;
    CGFloat heightSpace = 15;
    _shareButtonView = [[UIView alloc]init];
    _shareButtonView.backgroundColor = [UIColor whiteColor];
    _shareButtonView.frame = CGRectMake(self.bounds.origin.x, kScreenHeight, kScreenWidth, 80);
    _shareButtonView.alpha = 0;
    [self addSubview:_shareButtonView];
    /**
     *  微信
     */
    wechatSessionButton = [[UIButton alloc]init];
    wechatSessionButton.frame = CGRectMake(space, heightSpace, 50, 50);
    [wechatSessionButton setImage:[UIImage imageNamed:@"shareList1"] forState:UIControlStateNormal];
    [wechatSessionButton addTarget:self action:@selector(doWechatSessionButton) forControlEvents:UIControlEventTouchUpInside];
    [_shareButtonView addSubview:wechatSessionButton];
    /**
     *  微信朋友圈
     */
    wechatTimelineButton = [[UIButton alloc]init];
    wechatTimelineButton.frame = CGRectMake(wechatSessionButton.frame.origin.x + wechatSessionButton.frame.size.width + space, heightSpace, 50, 50);
    [wechatTimelineButton setImage:[UIImage imageNamed:@"shareList2"] forState:UIControlStateNormal];
    [wechatTimelineButton addTarget:self action:@selector(doWechatTimelineButton) forControlEvents:UIControlEventTouchUpInside];
    [_shareButtonView addSubview:wechatTimelineButton];
    /**
     *  QQ
     */
    qqButton = [[UIButton alloc]init];
    qqButton.frame = CGRectMake(wechatTimelineButton.frame.origin.x + wechatTimelineButton.frame.size.width + space, heightSpace, 50, 50);
    [qqButton setImage:[UIImage imageNamed:@"shareList3"] forState:UIControlStateNormal];
    [qqButton addTarget:self action:@selector(doQQButton) forControlEvents:UIControlEventTouchUpInside];
    [_shareButtonView addSubview:qqButton];
    /**
     *  QQ空间
     */
    qzoneButton = [[UIButton alloc]init];
    qzoneButton.frame = CGRectMake(qqButton.frame.origin.x + qqButton.frame.size.width + space, heightSpace, 50, 50);
    [qzoneButton setImage:[UIImage imageNamed:@"shareList4"] forState:UIControlStateNormal];
    [qzoneButton addTarget:self action:@selector(doQzoneButton) forControlEvents:UIControlEventTouchUpInside];
    [_shareButtonView addSubview:qzoneButton];
    /**
     *  新浪微博
     */
    sinaButton = [[UIButton alloc]init];
    sinaButton.frame = CGRectMake(qzoneButton.frame.origin.x + qzoneButton.frame.size.width + space, heightSpace, 50, 50);
    [sinaButton setImage:[UIImage imageNamed:@"shareList5"] forState:UIControlStateNormal];
    [sinaButton addTarget:self action:@selector(doSinaButton) forControlEvents:UIControlEventTouchUpInside];
    [_shareButtonView addSubview:sinaButton];
}
/**
 *  微信好友
 */
- (void)doWechatSessionButton{
    WXMediaMessage *message = [WXMediaMessage message];
    message.title = shareEntity.shareTitle;
    message.description = shareEntity.shareContent;
//    [message setThumbImage:[UIImage imageNamed:@"icon-share.png"]];
    NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:shareEntity.sharePictureURL]];
    if ([data length] > 1024*32) {
        UIImage *img = [UIImage imageWithData:data];
        img = [img scaledImageWithWidth:120 andHeight:120];
        data = UIImageJPEGRepresentation(img, 1);
    }
    message.thumbData = data;
    
    WXWebpageObject *ext = [WXWebpageObject object];
    ext.webpageUrl = shareEntity.shareURL;
    
    message.mediaObject = ext;
    message.mediaTagName = @"WECHAT_TAG_JUMP_APP";
    message.messageExt = @"这是第三方带的测试字段";
    message.messageAction = @"<action>dotalist</action>";
    
    SendMessageToWXReq* req = [[SendMessageToWXReq alloc] init];
    req.bText = NO;
    req.message = message;
    req.scene = WXSceneSession;
    
    [WXApi sendReq:req];
}
/**
 *  微信朋友圈
 */
- (void)doWechatTimelineButton{
    WXMediaMessage *message = [WXMediaMessage message];
    message.title = shareEntity.shareTitle;
    message.description = shareEntity.shareContent;
//    [message setThumbImage:[UIImage imageNamed:@"icon-share.png"]];
    
    NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:shareEntity.sharePictureURL]];
    if ([data length] > 1024*32) {
        UIImage *img = [UIImage imageWithData:data];
        img = [img scaledImageWithWidth:120 andHeight:120];
        data = UIImageJPEGRepresentation(img, 1);
    }
    message.thumbData = data;
    
    WXWebpageObject *ext = [WXWebpageObject object];
    ext.webpageUrl = shareEntity.shareURL;
    
    message.mediaObject = ext;
    message.mediaTagName = @"WECHAT_TAG_JUMP_APP";
    message.messageExt = @"这是第三方带的测试字段";
    message.messageAction = @"<action>dotalist</action>";
    
    SendMessageToWXReq* req = [[SendMessageToWXReq alloc] init];
    req.bText = NO;
    req.message = message;
    req.scene = WXSceneTimeline;
    
    [WXApi sendReq:req];
}
/**
 *  qq好友
 */
- (void)doQQButton{
//    NSString *path = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"icon-share.png"];
//    NSData *data = [NSData dataWithContentsOfFile:path];
    NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:shareEntity.sharePictureURL]];
    if ([data length] > 1024*32) {
        UIImage *img = [UIImage imageWithData:data];
        img = [img scaledImageWithWidth:120 andHeight:120];
        data = UIImageJPEGRepresentation(img, 1);
    }

    NSURL *url = [NSURL URLWithString:shareEntity.shareURL];
    NSString *title = shareEntity.shareTitle;
    NSString *desc = shareEntity.shareContent;
    
    QQApiNewsObject *img = [QQApiNewsObject objectWithURL:url title:title description:desc previewImageData:data];
    SendMessageToQQReq *req = [SendMessageToQQReq reqWithContent:img];
    QQApiSendResultCode sent = [QQApiInterface sendReq:req];
    [self handleSendResult:sent];
}
/**
 *  qq空间
 */
- (void)doQzoneButton{
//    NSString *path = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"icon-share.png"];
//    NSData *data = [NSData dataWithContentsOfFile:path];
    
    NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:shareEntity.sharePictureURL]];
    if ([data length] > 1024*32) {
        UIImage *img = [UIImage imageWithData:data];
        img = [img scaledImageWithWidth:120 andHeight:120];
        data = UIImageJPEGRepresentation(img, 1);
    }

    NSURL *url = [NSURL URLWithString:shareEntity.shareURL];
    NSString *title = shareEntity.shareTitle;
    NSString *desc = shareEntity.shareContent;
    QQApiNewsObject *img = [QQApiNewsObject objectWithURL:url title:title description:desc previewImageData:data];
    
    [img setCflag:kQQAPICtrlFlagQZoneShareOnStart];
    
    SendMessageToQQReq *req = [SendMessageToQQReq reqWithContent:img];
    
    QQApiSendResultCode sent = [QQApiInterface sendReq:req];
    
    [self handleSendResult:sent];
}

- (void)handleSendResult:(QQApiSendResultCode)sendResult
{
    switch (sendResult)
    {
        case EQQAPIAPPNOTREGISTED:
        {
            UIAlertView *msgbox = [[UIAlertView alloc] initWithTitle:@"Error" message:@"App未注册" delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:nil];
            [msgbox show];
            
            break;
        }
        case EQQAPIMESSAGECONTENTINVALID:
        case EQQAPIMESSAGECONTENTNULL:
        case EQQAPIMESSAGETYPEINVALID:
        {
            UIAlertView *msgbox = [[UIAlertView alloc] initWithTitle:@"Error" message:@"发送参数错误" delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:nil];
            [msgbox show];
            
            break;
        }
        case EQQAPIQQNOTINSTALLED:
        {
            UIAlertView *msgbox = [[UIAlertView alloc] initWithTitle:@"Error" message:@"未安装手Q" delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:nil];
            [msgbox show];
            
            break;
        }
        case EQQAPIQQNOTSUPPORTAPI:
        {
            UIAlertView *msgbox = [[UIAlertView alloc] initWithTitle:@"Error" message:@"API接口不支持" delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:nil];
            [msgbox show];
            
            break;
        }
        case EQQAPISENDFAILD:
        {
            UIAlertView *msgbox = [[UIAlertView alloc] initWithTitle:@"Error" message:@"发送失败" delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:nil];
            [msgbox show];
            
            break;
        }
        default:
        {
            break;
        }
    }
}

/**
 *  新浪分享
 */
- (void)doSinaButton{
    /**
     *  封装request
     */
    WBAuthorizeRequest *authRequest = [WBAuthorizeRequest request];
    authRequest.redirectURI = @"http://sns.whalecloud.com";
    authRequest.scope = @"all";
    /**
     *  封装message
     */
    WBMessageObject *message = [WBMessageObject message];
    message.text = shareEntity.shareContent;
    WBWebpageObject *webpage = [WBWebpageObject object];
    webpage.objectID = @"identifier1";
    webpage.title = shareEntity.shareTitle;
    webpage.description = shareEntity.shareContent;
//    webpage.thumbnailData = [NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"icon-share" ofType:@"png"]];
    NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:shareEntity.sharePictureURL]];
    if ([data length] > 1024*32) {
        UIImage *img = [UIImage imageWithData:data];
        img = [img scaledImageWithWidth:120 andHeight:120];
        data = UIImageJPEGRepresentation(img, 1);
    }
    webpage.thumbnailData = data;
    
    webpage.webpageUrl = shareEntity.shareURL;
    message.mediaObject = webpage;
    /**
     *  发送请求
     */
    WBSendMessageToWeiboRequest *request = [WBSendMessageToWeiboRequest requestWithMessage:message authInfo:authRequest access_token:[AppDelegate appDelegate].wbAccessToken];
//    request.userInfo = @{@"ShareMessageFrom": @"ShareView",
//                         @"Other_Info_1": [NSNumber numberWithInt:123],
//                         @"Other_Info_2": @[@"obj1", @"obj2"],
//                         @"Other_Info_3": @{@"key1": @"obj1", @"key2": @"obj2"}};
    [WeiboSDK sendRequest:request];
}

- (void)showWithEntity:(ShareContentModel *)entity{
    
    
    shareEntity = entity;
    /**
     *  创建点击事件
     */
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(hidden)];
    [self addGestureRecognizer:tap];

    [[AppDelegate appDelegate].window addSubview:self];//添加分享视图
    
    CGRect rect = _shareButtonView.frame;
    rect.origin.y -= rect.size.height;
    [UIView animateWithDuration:.3 animations:^{
        _shareButtonView.alpha = 1;
        _shareButtonView.frame = rect;
    }];
}

- (void)hidden
{
    CGRect rect = _shareButtonView.frame;
    rect.origin.y = kScreenHeight;
    [UIView animateWithDuration:.3 animations:^{
        _shareButtonView.alpha = 0;
        _shareButtonView.frame = rect;
    } completion:^(BOOL finished) {
//        [bgView removeFromSuperview];
        [self removeFromSuperview];
    }];

}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end