美文网首页热门
sharesdk自定义UI

sharesdk自定义UI

作者: 冬日无恙 | 来源:发表于2016-04-27 00:28 被阅读736次

    //sharesdk idk

    //腾讯idk

    #import TencentOpenAPI/TencentOAuth.h

    #import TencentOpenAPI/QQApiInterface.h

    #import "WXApi.h"

    #import "WeiboSDK.h"

    #import "ShareButton.h"

    @interface ShareModel : NSObject

    @property (nonatomic ,strong) NSString    *picUrl;

    @property (nonatomic ,strong) NSString    *title;

    @property (nonatomic ,strong) NSString    *content;

    @property (nonatomic ,strong) NSString    *link;

    @end

    @interface YZShare : NSObject

    //注册分享组件

    +(void)registApp;

    @end

    //分享界面

    typedef void(^SelectShareTypeBlock)(SSDKPlatformType platfromType ,NSInteger name);

    @interface YZShareView :       UIView

    @property (nonatomic ,retain)  UIVisualEffectView *blurView;

    @property (nonatomic ,copy)    SelectShareTypeBlock selectTypeBlock;

    @property (nonatomic ,strong)  UIView *whiteView;

    @end

    //阴影界面

    @interface ShareViewbg : UIView

    - (instancetype)initWithFrame:(CGRect)frame shareModel:(ShareModel *)shareModel;

    @property (nonatomic ,copy) void(^SSDKResponseStateSuccess)(NSUInteger indexId);

    @property (nonatomic ,copy) void(^SSDKResponseStateFail)(NSString *error);

    @property (nonatomic ,copy) void(^SSDKResponseStateCancel)();

    @property (nonatomic ,copy) void(^dimissShareView)();

    @property (nonatomic ,strong) YZShareView *shareView;

    @property (nonatomic ,strong) UIView      *bgView;

    @end

    #import "YZShare.h"

    #import "MyConst.h" //导入宏

    @interface YZShareView ()

    @end

    @implementation YZShare

    /**

    *初始化应用

    */

    + (void)registApp{

    [ShareSDK registerApp:@"*********" activePlatforms:@[@(SSDKPlatformSubTypeWechatTimeline),

    @(SSDKPlatformSubTypeWechatSession),

    @(SSDKPlatformTypeSinaWeibo),

    @(SSDKPlatformSubTypeQQFriend),

    @(SSDKPlatformSubTypeQZone),

    @(SSDKPlatformTypeCopy)

    ]

    onImport:^(SSDKPlatformType platformType) {

    switch (platformType) {

    case SSDKPlatformTypeWechat:

    [ShareSDKConnector connectWeChat:[WXApi class]];

    break;

    case SSDKPlatformTypeQQ:

    [ShareSDKConnector connectQQ:[QQApiInterface class] tencentOAuthClass:[TencentOAuth class]];

    break;

    case SSDKPlatformTypeSinaWeibo:

    [ShareSDKConnector connectWeibo:[WeiboSDK class]];

    break;

    case SSDKPlatformTypeCopy:

    {

    }

    break;

    case SSDKPlatformSubTypeQQFriend:

    {

    }

    break;

    case SSDKPlatformSubTypeQZone:

    {

    [ShareSDKConnector connectQQ:[QQApiInterface class] tencentOAuthClass:[TencentOAuth class]];

    }

    break;

    default:

    break;

    }

    } onConfiguration:^(SSDKPlatformType platformType, NSMutableDictionary *appInfo) {

    switch (platformType) {

    case SSDKPlatformTypeSinaWeibo:

    [appInfo SSDKSetupSinaWeiboByAppKey:@"********"

    appSecret:@"****************"

    redirectUri:@"https:********"

    authType:SSDKAuthTypeBoth];

    break;

    case SSDKPlatformTypeWechat:

    case SSDKPlatformSubTypeWechatSession:

    case SSDKPlatformSubTypeWechatTimeline:

    case SSDKPlatformSubTypeWechatFav:

    [appInfo SSDKSetupWeChatByAppId:@"********"

    appSecret:@"********"];

    break;

    case SSDKPlatformSubTypeQQFriend:

    case SSDKPlatformTypeQQ:

    case SSDKPlatformSubTypeQZone:

    [appInfo SSDKSetupQQByAppId:@"********"

    appKey:@"A********"

    authType:SSDKAuthTypeBoth];

    break;

    default:

    break;

    }

    }];

    }

    @end

    #pragma mark -分享界面

    @implementation YZShareView

    - (instancetype)initWithFrame:(CGRect)frame{

    self =[super initWithFrame:frame];

    if (self) {

    NSArray *btnTitle=[[NSArray alloc]initWithObjects:@"微信朋友圈",@"微信好友",@"微博",@"QQ好友",@"QQ空间",@"复制链接", nil];

    NSArray *images  =[[NSArray alloc]initWithObjects:@"shareWechatZone",@"shareWechant",@"shareWeibo",@"QQ",@"QQZone",@"copy", nil];

    int totallo = 3;

    CGFloat interval = (self.frame.size.width -totallo *80)/(totallo +1);

    for (int i=0; i<5; i++) {

    int row = i/totallo;

    int loc =i%totallo;

    CGFloat x = interval +(interval +80)*loc;

    CGFloat y = (self.frame.size.height-80*2-45)/2+135*row;

    ShareButton *button = [ShareButton buttonWithType:UIButtonTypeCustom];

    button.titleLabel.textAlignment = NSTextAlignmentCenter;

    button.tag=220+i;

    [button setTitle:btnTitle[i] forState:UIControlStateNormal];

    [button setImage:[UIImage imageNamed:images[i]] forState:UIControlStateNormal];

    button.titleLabel.font = [UIFont systemFontOfSize:12];

    [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];

    button.frame = CGRectMake(x, y, 80, 80);

    button.backgroundColor = [UIColor clearColor];

    [self addSubview:button];

    [button addTarget:self action:@selector(shareClick:) forControlEvents:UIControlEventTouchUpInside];

    #pragma mark- 根据验证该平台是否可以分享,来确定按钮的点击事件是否可以点击

    switch (i) {

    case 0:

    {

    button.enabled=[ShareSDK isClientInstalled:SSDKPlatformTypeWechat];

    if (button.enabled==NO) {

    button.hidden=YES;

    }

    break;

    }

    case 1:

    {

    button.enabled=[ShareSDK isClientInstalled:SSDKPlatformSubTypeWechatSession];

    if (button.enabled==NO) {

    button.hidden=YES;

    }

    break;

    }

    case 2:

    {

    [ShareSDK isClientInstalled:SSDKPlatformTypeSinaWeibo];

    //                    button.enabled=[ShareSDK isClientInstalled:SSDKPlatformTypeSinaWeibo];

    //                    if (button.enabled==NO) {

    //                        button.hidden=YES;

    //                    }

    }

    break;

    case 3:

    {

    button.enabled =[ShareSDK isClientInstalled:SSDKPlatformSubTypeQQFriend];

    if (button.enabled==NO) {

    button.hidden=YES;

    }

    break;

    }

    case 4:

    {

    button.enabled =[ShareSDK isClientInstalled:SSDKPlatformSubTypeQZone];

    if (button.enabled ==NO) {

    button.hidden =YES;

    }

    break;

    }

    case 5:

    #pragma mark 复制链接

    NSLog(@"----");

    break;

    default:

    break;

    }

    }

    }

    return self;

    }

    #pragma mark -分享点击事件

    - (void)shareClick:(ShareButton *)btn{

    switch (btn.tag) {

    case 220:

    _selectTypeBlock(SSDKPlatformSubTypeWechatTimeline,1);

    break;

    case 221:

    _selectTypeBlock(SSDKPlatformSubTypeWechatSession,2);

    break;

    case 222:

    _selectTypeBlock(SSDKPlatformTypeSinaWeibo,3);

    break;

    case 223:

    _selectTypeBlock(SSDKPlatformSubTypeQQFriend,4);

    break;

    case 224:

    _selectTypeBlock(SSDKPlatformSubTypeQZone,5);

    break;

    case 225:

    NSLog(@"复制链接");

    break;

    default:

    break;

    }

    }

    @end

    #pragma mark -阴影view

    @implementation ShareViewbg

    - (instancetype)initWithFrame:(CGRect)frame shareModel:(ShareModel *)shareModel{

    if (self =[super initWithFrame:frame]) {

    self.bgView =[[UIView alloc]initWithFrame:[UIScreen mainScreen].bounds];

    self.bgView.backgroundColor =[UIColor blackColor];

    self.bgView.alpha=0.6;

    [[UIApplication sharedApplication].keyWindow addSubview:self.bgView];

    UITapGestureRecognizer *tap =[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(dimissShareView:)];

    [self.bgView addGestureRecognizer:tap];

    if (shareModel==nil) {

    ShareModel *model=[ShareModel new];

    shareModel =model;

    model.picUrl=@"http://www.eurlion.com";

    model.title =@"理安保险";

    model.content=@"理安保险测试内容....";

    model.link=@"asdsadsa";

    }

    NSData *imageData =[NSData dataWithContentsOfURL:[NSURL URLWithString:shareModel.picUrl]];

    NSMutableDictionary *shareParams =[NSMutableDictionary dictionary];

    //设置分享的参数...

    [shareParams SSDKSetupShareParamsByText:shareModel.content images:[UIImage imageWithData:imageData] url:[NSURL URLWithString:shareModel.picUrl] title:shareModel.title type:SSDKContentTypeAuto];

    [self selectShareWithType:^(SSDKPlatformType platformType) {

    [ShareSDK share:platformType parameters:shareParams onStateChanged:^(SSDKResponseState state, NSDictionary *userData, SSDKContentEntity *contentEntity, NSError *error) {

    switch (state) {

    case SSDKResponseStateBegin:

    {

    //开始

    }

    break;

    case SSDKResponseStateSuccess:

    {

    //分享成功,分享界面消失

    self.SSDKResponseStateSuccess(platformType);

    [self dimissShareView:nil];

    }

    break;

    case SSDKResponseStateFail:

    {

    //分享失败

    self.SSDKResponseStateFail([NSString stringWithFormat:@"%@",error]);

    }

    break;

    case SSDKResponseStateCancel:

    {

    //分享取消,分享界面消失..

    self.SSDKResponseStateCancel();

    [self dimissShareView:nil];

    }

    break;

    default:

    break;

    }

    if (state !=SSDKResponseStateBegin) {

    //加载结束

    }

    }];

    }];

    }

    return self;

    }

    #pragma mark -显示分享选项

    - (void)selectShareWithType:(void(^)(SSDKPlatformType platformType))SelectedPlatformType{

    self.shareView =[[YZShareView alloc]initWithFrame:CGRectMake(screenWidth/2-(screenWidth-40)/2, screenHeigh/2-(screenWidth-40)/2, screenWidth-40, screenWidth-40)];

    self.shareView.backgroundColor =[UIColor whiteColor];

    self.shareView.clipsToBounds=YES;

    self.shareView.layer.cornerRadius=10;

    [[UIApplication sharedApplication].keyWindow addSubview:self.shareView];

    self.shareView.selectTypeBlock=^(SSDKPlatformType platfromType ,NSInteger nameId){

    SelectedPlatformType(platfromType);

    };

    }

    #pragma mark -消失

    - (void)dimissShareView:(id)sender{

    [self.bgView removeFromSuperview];

    [self.shareView removeFromSuperview];

    [self removeFromSuperview];

    }

    @end

    #pragma mark -分享数据的模型

    @implementation ShareModel

    @end

    相关文章

      网友评论

        本文标题:sharesdk自定义UI

        本文链接:https://www.haomeiwen.com/subject/eqgqrttx.html