基于shareSDK自定义分享界面

本文介绍了一个iOS平台上的自定义分享组件实现过程。该组件能够根据不同条件显示可用的分享渠道,如QQ、微信、微博等,并提供了分享内容至各平台的功能。文章详细展示了如何通过检查第三方应用的安装状态来动态调整分享选项。
摘要由CSDN通过智能技术生成

#import "CLShareView.h"
#import "CLCustomButton.h"
#import <ShareSDK/ShareSDK.h>
#import "MBProgressHUD+MJ.h"
#import "WeiboSDK.h"
#import "WXApi.h"
#import <TencentOpenAPI/QQApiInterface.h>
#import <MessageUI/MessageUI.h>
#import<MessageUI/MFMailComposeViewController.h>
@interface CLShareView()

/***/
@property (nonatomic,strong) NSArray *imgArr;
@property (nonatomic,strong) NSArray *titleArr;
@end
@implementation CLShareView

- (instancetype)init
{
    self = [super init];
    if (self) {
        // 创建一个阴影
        UIWindow *win = [UIApplication sharedApplication].keyWindow;
        UIView *cover = [[UIView alloc] initWithFrame:[UIScreen mainScreen].bounds];
        cover.backgroundColor = [UIColor blackColor];
        cover.tag = 100;
        cover.alpha = 0.8;
        [win addSubview:cover];
        // 创建一个提示框
        CGFloat tipX = 20;
        CGFloat tipW = cover.frame.size.width - 2*tipX;
        CGFloat tipH = tipW;
        
        UIView *tipViews = [[UIView alloc] initWithFrame:CGRectMake(tipX, CLScreenH, tipW, tipW)];
        tipViews.backgroundColor = [UIColor colorWithWhite:1 alpha:0.9];
        [win addSubview:tipViews];
        [UIView animateWithDuration:0.25 animations:^{
            CGFloat tipY = (CLScreenH-tipW)*0.5;
            tipViews.frame = CGRectMake(tipX, tipY, tipW, tipH);
        } completion:^(BOOL finished) {
            
        }];
        
        UILabel *lable = [[UILabel alloc] initWithFrame:CGRectMake(0, 20, tipViews.frame.size.width, 30)];
        lable.text  =@"分享到";
        lable.textAlignment = NSTextAlignmentCenter;
        lable.font = [UIFont systemFontOfSize:15];
        [tipViews addSubview:lable];
       Class messageClass = (NSClassFromString(@"MFMessageComposeViewController"));
        
        if ((![WXApi isWXAppInstalled]&&[QQApiInterface isQQInstalled])&&[messageClass canSendText]) {
            self.imgArr = @[@"QQ",@"QQzoom",@"新浪",@"短信",@"邮件"];
            self.titleArr = @[@"QQ好友",@"QQ空间",@"新浪微博",@"短信",@"收藏"];
        }else if ((![WXApi isWXAppInstalled])&&(![WeiboSDK isWeiboAppInstalled]&&[QQApiInterface isQQInstalled])&&[messageClass canSendText]){
            self.imgArr = @[@"QQ",@"QQzoom",@"短信",@"邮件"];
            self.titleArr = @[@"QQ好友",@"QQ空间",@"短信",@"收藏"];
        } else if (![QQApiInterface isQQInstalled]&&[WXApi isWXAppInstalled]&&[messageClass canSendText]){
            self.imgArr = @[@"weixin",@"朋友圈",@"收藏",@"新浪",@"短信",@"邮件"];
            self.titleArr = @[@"微信好友",@"微信朋友圈",@"微信收藏",@"新浪微博",@"短信",@"收藏"];
        } else if ((![QQApiInterface isQQInstalled])&&(![WXApi isWXAppInstalled])&&[messageClass canSendText]){
            self.imgArr = @[@"新浪",@"短信",@"邮件"];
            self.titleArr = @[@"新浪微博",@"短信",@"收藏"];
        } else if ((![QQApiInterface isQQInstalled])&&([WXApi isWXAppInstalled])&&![messageClass canSendText]){
            self.imgArr = @[@"weixin",@"朋友圈",@"收藏",@"新浪",@"邮件"];
            self.titleArr = @[@"微信好友",@"微信朋友圈",@"微信收藏",@"新浪微博",@"收藏"];
        } else if ((![messageClass canSendText])&&(![QQApiInterface isQQInstalled])&&(![WXApi isWXAppInstalled])){
            self.imgArr = @[@"新浪",@"邮件"];
            self.titleArr = @[@"新浪微博",@"收藏"];
        } else if (([QQApiInterface isQQInstalled])&&([WXApi isWXAppInstalled])&&[WeiboSDK isWeiboAppInstalled]&&[messageClass canSendText]){
            self.imgArr = @[@"QQ",@"QQzoom",@"weixin",@"朋友圈",@"收藏",@"新浪",@"短信",@"邮件"];
            self.titleArr = @[@"QQ好友",@"QQ空间",@"微信好友",@"微信朋友圈",@"微信收藏",@"新浪微博",@"短信",@"收藏"];
        }else if ([QQApiInterface isQQInstalled]&&[WXApi isWXAppInstalled]&&![messageClass canSendText]){
            self.imgArr = @[@"weixin",@"朋友圈",@"收藏",@"新浪",@"邮件"];
            self.titleArr = @[@"微信好友",@"微信朋友圈",@"微信收藏",@"新浪微博",@"收藏"];
        }
       
        for (int i = 0; i < self.titleArr.count; i++) {
            int line = 4;
            CGFloat btnW = tipViews.frame.size.width/line;
            CGFloat btnX = i%line*btnW;
            CGFloat btnY = i/line*btnW;
            UIButton *btnShare = [[CLCustomButton alloc] initWithFrame:CGRectMake(btnX,70 + btnY, btnW, btnW)];
            btnShare.tag = i+100;
            btnShare.imageView.contentMode = UIViewContentModeScaleAspectFit;
            [btnShare setTitle:self.titleArr[i] forState:UIControlStateNormal];
            [btnShare setImage:[UIImage imageNamed:self.imgArr[i]] forState:UIControlStateNormal];
            [btnShare addTarget:self action:@selector(clickBtn:) forControlEvents:UIControlEventTouchUpInside];
            [tipViews addSubview:btnShare];
        }
        
        UIButton *cancelBtn = [[UIButton alloc] initWithFrame:CGRectMake((tipViews.frame.size.width-50)*0.5, tipViews.frame.size.height - 60, 50, 50)];
        [cancelBtn setBackgroundImage:[UIImage imageNamed:@"取消"] forState:UIControlStateNormal];
        [cancelBtn addTarget:self action:@selector(clickCancel) forControlEvents:UIControlEventTouchUpInside];
        [tipViews addSubview:cancelBtn];

    }
    return self;
}

- (void)clickCancel
{
    UIWindow *win = [UIApplication sharedApplication].keyWindow;
    [win.subviews.lastObject removeFromSuperview];
    UIWindow *win2 = [UIApplication sharedApplication].keyWindow;
    [win2.subviews.lastObject removeFromSuperview];
    UIWindow *win3 = [UIApplication sharedApplication].keyWindow;
    [win3.subviews.lastObject removeFromSuperview];
}

- (void)setContent:(NSString *)content
{
    _content = content;
    NSUserDefaults *userD = [NSUserDefaults standardUserDefaults];
    [userD setValue:_content forKey:@"1"];
    [userD synchronize];
}

- (void)clickBtn:(UIButton *)btn
{
    [self initWithBtn:btn];
}
- (void)initWithBtn:(UIButton *)btn
{
    NSUserDefaults *userD = [NSUserDefaults standardUserDefaults];
    NSString *str = [userD objectForKey:@"1"];
    //创建分享参数
    NSMutableDictionary *shareParams = [NSMutableDictionary dictionary];
    [shareParams SSDKEnableUseClientShare];
    [shareParams SSDKSetupShareParamsByText:str
                                     images:nil
                                        url:nil
                                      title:nil
                                       type:SSDKContentTypeAuto];
    if ([btn.titleLabel.text isEqualToString:@"QQ好友"]) { //QQ
        [ShareSDK share:SSDKPlatformTypeQQ parameters:shareParams onStateChanged:^(SSDKResponseState state, NSDictionary *userData, SSDKContentEntity *contentEntity, NSError *error) {
            if (state == SSDKResponseStateSuccess) {
                [MBProgressHUD showSuccess:@"分享成功"];
            }

        }];
    }
    if ([btn.titleLabel.text isEqualToString:@"QQ空间"]) { // QQ 空间
        NSMutableDictionary *shareParams2 = [NSMutableDictionary dictionary];
        [shareParams2 SSDKEnableUseClientShare];
        [shareParams2 SSDKSetupShareParamsByText:str
                                         images:@"logo.png"
                                            url:[NSURL URLWithString:GoToAppS]
                                          title:@"累了吗这款应用还不错哦"
                                           type:SSDKContentTypeAuto];
        [ShareSDK share:SSDKPlatformSubTypeQZone parameters:shareParams2 onStateChanged:^(SSDKResponseState state, NSDictionary *userData, SSDKContentEntity *contentEntity, NSError *error) {
            if (state == SSDKResponseStateSuccess) {
                [MBProgressHUD showSuccess:@"分享成功"];
            }
            
        }];
    }

    if ([btn.titleLabel.text isEqualToString:@"微信好友"]) {
        [ShareSDK share:SSDKPlatformTypeWechat parameters:shareParams onStateChanged:^(SSDKResponseState state, NSDictionary *userData, SSDKContentEntity *contentEntity, NSError *error) {
            if (state == SSDKResponseStateSuccess) {
                [MBProgressHUD showSuccess:@"分享成功"];
            }
            
        }];
    }

    if ([btn.titleLabel.text isEqualToString:@"微信朋友圈"]) {
        [ShareSDK share:SSDKPlatformSubTypeWechatTimeline parameters:shareParams onStateChanged:^(SSDKResponseState state, NSDictionary *userData, SSDKContentEntity *contentEntity, NSError *error) {
            if (state == SSDKResponseStateSuccess) {
                [MBProgressHUD showSuccess:@"分享成功"];
            }
            
        }];
    }

    if ([btn.titleLabel.text isEqualToString:@"微信收藏"]) {
        [ShareSDK share:SSDKPlatformSubTypeWechatFav parameters:shareParams onStateChanged:^(SSDKResponseState state, NSDictionary *userData, SSDKContentEntity *contentEntity, NSError *error) {
            if (state == SSDKResponseStateSuccess) {
                [MBProgressHUD showSuccess:@"收藏成功"];
            }
            
        }];
    }

    if ([btn.titleLabel.text containsString:@"新浪微博"]) {

        [ShareSDK share:SSDKPlatformTypeSinaWeibo
                      parameters:shareParams onStateChanged:^(SSDKResponseState state, NSDictionary *userData, SSDKContentEntity *contentEntity, NSError *error) {
                          if (state == SSDKResponseStateSuccess) {
                              [MBProgressHUD showSuccess:@"分享成功"];
                          } else if(state == SSDKResponseStateFail){
                              NSLog(@"%@",error);
                          }
        }];
    }

    if ([btn.titleLabel.text isEqualToString:@"短信"]) {
        [ShareSDK share:SSDKPlatformTypeSMS parameters:shareParams onStateChanged:^(SSDKResponseState state, NSDictionary *userData, SSDKContentEntity *contentEntity, NSError *error) {
            if (state == SSDKResponseStateSuccess) {
                [MBProgressHUD showSuccess:@"分享成功"];
            }
            
        }];
    }
    
    if ([btn.titleLabel.text isEqualToString:@"收藏"]) {
        [self clickCancel];
         self.btnSelected();
    }
}
@end

 

 

 

移动端技术热门技术交流群170229489

转载于:https://www.cnblogs.com/darren-chen/p/5511876.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值