基于ShareSDK平台的分享实现

版本记录

版本号 时间
V1.0 2017.08.03

前言

前面我写过ShareSDK登录,以及苹果原生的几种分享方式,但是还没有对ShareSDK在分享上的应用进行配置,这一篇主要就是讲解ShareSDK在分享上的应用。

功能要求

结合工程实践,演示ShareSDK在分享上的应用。


功能实现

下面我就结合具体的工程实践,详细的讲解下ShareSDK的分享实现。

1. 工程配置

首先我们需要配置下工程。

工程配置请参考我写的ShareSDK登录篇。

2. 代码实现

下面写一个分享的工具类实现第三方平台的分享,直接看代码吧。

1. JJShareTool.h
#import 

@interface JJShareTool : NSObject

//分享
+ (void)shareWithButtonTag:(NSInteger)btnTag Text:(NSString *)text
                    images:(id)images
                       url:(NSURL *)url
                     title:(NSString *)title
                   success:(void(^)(BOOL isSuccess,NSString* channelType))success
                      fail:(void(^)(BOOL isFail))fail
                    cancel:(void(^)(BOOL isCancel))cancel
              notInstalled:(void(^)(BOOL isInstalled, NSString *msg))notInstalled;

@end
2. JJShareTool.m
#import "JJShareTool.h"

#import 
#import 
#import 
#import 
#import 
#import 

#import 
#import "WXApi.h"
#import "WeiboSDK.h"
//腾讯开放平台(对应QQ和QQ空间)SDK头文件
#import 
#import 

@implementation JJShareTool


//分享
+ (void)shareWithButtonTag:(NSInteger)btnTag Text:(NSString *)text
                    images:(id)images
                       url:(NSURL *)url
                     title:(NSString *)title
                    success:(void(^)(BOOL isSuccess,NSString* channelType))success
                    fail:(void(^)(BOOL isFail))fail
                    cancel:(void(^)(BOOL isCancel))cancel
                    notInstalled:(void(^)(BOOL isInstalled, NSString *msg))notInstalled
{
    if (btnTag == 5) {
        if (notInstalled) {
            if (![WeiboSDK isWeiboAppInstalled]) {
                notInstalled([WeiboSDK isWeiboAppInstalled], @"未安装微博客户端,请安装后重新分享");
                return;
            }
        }
    } else if (btnTag%2==1) {
        if (notInstalled) {
            if (![WXApi isWXAppInstalled]) {
                notInstalled([WXApi isWXAppInstalled], @"未安装微信客户端,请安装后重新分享");
                return;
            }
        }
    } else {
        if (notInstalled) {
            if (![QQApiInterface isQQInstalled]) {
                notInstalled([QQApiInterface isQQInstalled], @"未安装QQ客户端,请安装后重新分享");
                return;
            }
        }
    }
    SSDKPlatformType platformType =[[self class] getPlatformTypeWithButtonTag:btnTag];
    int SSDKContentType =SSDKContentTypeAuto;
    if (platformType ==SSDKPlatformTypeSinaWeibo) {
        SSDKContentType = SSDKContentTypeWebPage;
    }
    if (platformType == SSDKPlatformSubTypeWechatTimeline) {
        title = text;
    }
    if (!title || title.length == 0) {
        title = @"XXXXX";
    }
    NSInteger channelType =[[self class] getChannelTypeWithButtonTag:btnTag];
    NSMutableDictionary *shareParams = [NSMutableDictionary dictionary];
    [shareParams SSDKSetupShareParamsByText:text images:images url:url title:title type:SSDKContentType];
    [[self class] shareWithShareParams:shareParams platformType:platformType channelType:channelType success:success fail:fail cancel:cancel];
}

+ (SSDKPlatformType)getPlatformTypeWithButtonTag:(NSInteger)btnTag
{
    SSDKPlatformType platformType;
    
    switch (btnTag) {
        case 5:
            //新浪
            platformType = SSDKPlatformTypeSinaWeibo;
            
            break;
        case 1:
            //微信
            platformType = SSDKPlatformSubTypeWechatSession;
            break;
        case 3:
            //朋友圈
            platformType = SSDKPlatformSubTypeWechatTimeline;
            
            break;
        case 2:
            //qq
            platformType = SSDKPlatformSubTypeQQFriend;
            
            break;
        case 4:
            
            //qq空间
            // [[self class] chekoutQQAppInstalled];
            platformType = SSDKPlatformSubTypeQZone;
            
            break;
        default:
            platformType = SSDKPlatformSubTypeWechatTimeline;
            break;
    }
    
    return platformType;
}

+ (NSInteger)getChannelTypeWithButtonTag:(NSInteger)btnTag
{
    NSInteger channelType = 0;
    switch (btnTag) {
        case 5:
            //新浪
            channelType = 2;
            break;
        case 1:
            //微信
            channelType = 3;
            break;
        case 3:
            //朋友圈
            channelType = 4;
            break;
        case 2:
            //qq
            channelType = 1;
            break;
        case 4:
            //qq空间
            channelType = 5;
            break;
        default:
            
            break;
    }
    
    return channelType;
    
}
    
+ (void)shareWithShareParams:(NSMutableDictionary *)shareParams platformType:(SSDKPlatformType)platformType channelType:(NSInteger)channelType success:(void(^)(BOOL isSuccess,NSString * channelType))success
    fail:(void(^)(BOOL isFail))fail
    cancel:(void(^)(BOOL isCancel))cancel
{
    //使用客户端分享
    [shareParams SSDKEnableUseClientShare];
    //进行分享
    [ShareSDK share:platformType
         parameters:shareParams
     onStateChanged:^(SSDKResponseState state, NSDictionary *userData, SSDKContentEntity *contentEntity, NSError *error) {
         
         switch (state) {
             case SSDKResponseStateBegin: {
                 break;
             }
             case SSDKResponseStateSuccess: {
                 if (success) {
                     success(YES,[NSString stringWithFormat:@"%ld",channelType]);
                 }
                 break;
             }
             case SSDKResponseStateFail: {
                 if (fail) {
                     fail(YES);
                 }
                 break;
             }
             case SSDKResponseStateCancel: {
                 if (cancel) {
                     cancel(YES);
                 }
                 break;
             }
         }         
     }];
}

@end

功能效果

下面我们就看几个分享的效果图。

基于ShareSDK平台的分享实现_第1张图片
效果1
基于ShareSDK平台的分享实现_第2张图片
效果2
基于ShareSDK平台的分享实现_第3张图片
效果3
基于ShareSDK平台的分享实现_第4张图片
效果4
基于ShareSDK平台的分享实现_第5张图片
效果5

可见,实现了分享。

后记

分享就到此结束了,后期还会和分享更多的好玩的。谢谢大家的支持。

基于ShareSDK平台的分享实现_第6张图片

你可能感兴趣的:(基于ShareSDK平台的分享实现)