iOS-关于对ShareSDK第三方登录、分享功能的封装

(更新:本文Demo https://github.com/yangfangxue/YFX_ShareSDK)
一、完成对微信好友、微信朋友圈、微信收藏、新浪微博、QQ好友、QQ空间、短信、邮件等主流平台的图片、文字、URL分享等功能 在博主的项目中我增加了对本地视频文件的分享功能(仅微信好友)、另有QQ空间的视频分享功能(ShareSDK For iOS v3.4.0(2016-08-03)

二、第三方QQ/微信/微博的登录

三、开发环境:Xcode7.3.1 ShareSDK版本为ShareSDK For iOS v3.3.1
PS:(ShareSDK For iOS v3.4.0(2016-08-03)支持了QQ空间的视频分享功能

四、对于MOB官方文档中所介绍的方法 写在我们VC的分享按钮触发事件中 显得太杂乱、没有灵活性,在这里博主对ShareSDK的分享功能做了一次封装,可以让我们在VC中调用的时候显得更加的灵活且好管理

1、关于ShareSDK的准备工作,在MOB的官方文档中有详细的说明在这里我就不多说了。

2、步入正题之代码

iOS-关于对ShareSDK第三方登录、分享功能的封装_第1张图片

3、在APPDelegate中调用


iOS-关于对ShareSDK第三方登录、分享功能的封装_第2张图片
Paste_Image.png

具体步骤
1、新建ShareSDKTool类继承与NSObject
2、代码部分
(1)导入ShareSDK类库中这些类名

iOS-关于对ShareSDK第三方登录、分享功能的封装_第3张图片
Paste_Image.png

(2)注册ShareSDK

#pragma mark -  注册ShareSDK
+(void)registerShare
{   //registerApp 初始化SDK并且初始化第三方平台
    [ShareSDK registerApp:K_ShareSDK_AppKey
          activePlatforms:@[
                            @(SSDKPlatformTypeSinaWeibo),
                            @(SSDKPlatformTypeMail),
                            @(SSDKPlatformTypeSMS),
                            @(SSDKPlatformTypeWechat),
                            @(SSDKPlatformTypeQQ),
                            ]
                 onImport:^(SSDKPlatformType platformType) {
                     switch (platformType)
                     {
                         case SSDKPlatformTypeWechat:{
                             [ShareSDKConnector connectWeChat:[WXApi class]];
                             break;
                         }
                         case SSDKPlatformTypeSinaWeibo:
                             [ShareSDKConnector connectWeibo:[WeiboSDK class]];
                             break;
                         case SSDKPlatformTypeQQ:
                             [ShareSDKConnector connectQQ:[QQApiInterface class] tencentOAuthClass:[TencentOAuth class]];
                             break;
                         default:
                             break;
                     }
                     
                 } onConfiguration:^(SSDKPlatformType platformType, NSMutableDictionary *appInfo) {
                     switch (platformType)
                     {
                         case SSDKPlatformTypeWechat:
                             [appInfo SSDKSetupWeChatByAppId:K_WX_AppID
                                                   appSecret:K_WX_AppSecret];
                             break;
                         case SSDKPlatformTypeSinaWeibo:
                
                             [appInfo SSDKSetupSinaWeiboByAppKey:K_Sina_AppKey
                                                       appSecret:K_Sina_AppSecret
                                                     redirectUri:K_Share_Url
                                                        authType:SSDKAuthTypeBoth];
                             break;
                         case SSDKPlatformTypeQQ:
                             [appInfo SSDKSetupQQByAppId:K_QQ_AppId
                                                  appKey:K_QQ_AppKey
                                                authType:SSDKAuthTypeBoth];
                             break;
                         default:
                             break;
                     }
                 }
     ];
}

(3)分享图片、文本、URL等至微信好友、微信朋友圈、微信收藏、新浪微博、QQ好友、QQ空间、短信、邮件等平台

#pragma mark- 分享
+(void)shareContentWithShareContentType:(SSDKContentType)shareContentType
                           contentTitle:(NSString *)contentTitle
                     contentDescription:(NSString *)contentDescription
                           contentImage:(id)contentImage
                             contentURL:(NSString *)contentURL
                             showInView:(UIView *)showInView
                                success:(void (^)())success
                                failure:(void (^)(NSString *failureInfo))failure
                    OtherResponseStatus:(void (^)(SSDKResponseState state))otherResponseStatus{
    //1. 创建分享参数
    NSMutableDictionary *shareParams = [NSMutableDictionary dictionary];
    
    [shareParams SSDKEnableUseClientShare];
    
    [shareParams SSDKSetupShareParamsByText:contentDescription
                                     images:contentImage
                                        url:[NSURL URLWithString:contentURL]
                                      title:contentTitle
                                       type:shareContentType];
    //2. 分享,显示分享view
    SSUIShareActionSheetController *sheet =[ShareSDK showShareActionSheet:showInView
                             items:nil
                       shareParams:shareParams
               onShareStateChanged:^(SSDKResponseState state, SSDKPlatformType platformType, NSDictionary *userData, SSDKContentEntity *contentEntity, NSError *error, BOOL end) {
                   
                   switch (state) {
                           
                       case SSDKResponseStateBegin:
                       {
                           
                           break;
                       }
                       case SSDKResponseStateSuccess:
                       {
                           
                           success();
                           break;
                           
                       }
                       case SSDKResponseStateFail:
                       {
                           
                           if (platformType == SSDKPlatformTypeSMS && [error code] == 201)
                           {
                               failure(@"失败原因可能是:1、短信应用没有设置帐号;2、设备不支持短信应用;3、短信应用在iOS 7以上才能发送带附件的短                                                     信。");
                               break;
                               
                           }
                           else if(platformType == SSDKPlatformTypeMail && [error code] == 201)
                           {
                               
                              
                               failure(@"失败原因可能是:1、邮件应用没有设置帐号;2、设备不支持邮件应用。");
                               break;
                               
                           }
                           else
                           {
                               failure([NSString stringWithFormat:@"%@",error]);
                               break;
                               
                           }
                           break;
                       }
                       case SSDKResponseStateCancel:
                       {
                           otherResponseStatus(SSDKResponseStateCancel);
                           break;
                       }
                       default:
                           break;
                   }
                   if (state != SSDKResponseStateBegin)
                   {
                       failure([NSString stringWithFormat:@"%@",error]);
                       
                   }
               }];
    
    [sheet.directSharePlatforms addObject:@(SSDKPlatformTypeSinaWeibo)];
    [sheet.directSharePlatforms addObject:@(SSDKPlatformTypeMail)];
    [sheet.directSharePlatforms addObject:@(SSDKPlatformTypeSMS)];
}

(4)文件分享(本地相册中的视频)分享-->仅微信好友可分享(设置title、sourceFileExtension、sourceFileData,以及thumbImage等参数)

#pragma mark 分享文件代码(仅支持微信好友)
+ (void)shareVideoContentParamsByText:(NSString *)text
                                title:(NSString *)title
                                  url:(NSURL *)url
                           thumbImage:(id)thumbImage
                                image:(id)image
                         musicFileURL:(NSURL *)musicFileURL
                              extInfo:(NSString *)extInfo
                             fileData:(id)fileData
                         emoticonData:(id)emoticonData
                  sourceFileExtension:(NSString *)fileExtension
                       sourceFileData:(id)sourceFileData
                                 type:(SSDKContentType)type
                   forPlatformSubType:(SSDKPlatformType)platformSubType
                           showInView:(UIView *)showInView
                              success:(void (^)())success
                              failure:(void (^)(NSString *failureInfo))failure
                  OtherResponseStatus:(void (^)(SSDKResponseState state))otherResponseStatus{
    //1. 创建分享参数
    NSMutableDictionary *shareParams = [NSMutableDictionary dictionary];
    
    [shareParams SSDKEnableUseClientShare];
    
    [shareParams SSDKSetupWeChatParamsByText:text title:title url:url thumbImage:thumbImage image:image musicFileURL:musicFileURL extInfo:extInfo fileData:fileData emoticonData:emoticonData sourceFileExtension:fileExtension sourceFileData:sourceFileData type:type forPlatformSubType:platformSubType];
    
    NSArray *items = @[@(SSDKPlatformSubTypeWechatSession)];
    //2. 分享->显示分享view
    SSUIShareActionSheetController *sheet = [ShareSDK showShareActionSheet:showInView                                                                    items:items
        shareParams:shareParams
        onShareStateChanged:^(SSDKResponseState state, SSDKPlatformType platformType, NSDictionary *userData, SSDKContentEntity *contentEntity, NSError *error, BOOL end) {
            
            switch (state) {
                    
                case SSDKResponseStateBegin:
                {
                    break;
                }
                case SSDKResponseStateSuccess:
                {
                    
                    success();
                    break;
                    
                }
                case SSDKResponseStateFail:
                {
                    
                    if (platformType == SSDKPlatformTypeSMS && [error code] == 201)
                    {
                        failure(@"失败原因可能是:1、短信应用没有设置帐号;2、设备不支持短信应用;3、短信应用在iOS 7以上才能发送带附件的短                                                     信。");
                        break;
                        
                    }
                    else if(platformType == SSDKPlatformTypeMail && [error code] == 201)
                    {
                        
                        failure(@"失败原因可能是:1、邮件应用没有设置帐号;2、设备不支持邮件应用。");
                        break;
                    }
                    else
                    {
                        failure([NSString stringWithFormat:@"%@",error]);
                        break;
                    }
                    break;
                }
                case SSDKResponseStateCancel:
                {
                    otherResponseStatus(SSDKResponseStateCancel); //捕获不准确
                    break;
                }
                default:
                    break;
            }
            if (state != SSDKResponseStateBegin)
           {
              failure([NSString stringWithFormat:@"%@",error]);
           }
    }];
    [sheet.directSharePlatforms addObject:@(SSDKPlatformTypeSinaWeibo)];
    [sheet.directSharePlatforms addObject:@(SSDKPlatformTypeMail)];
    [sheet.directSharePlatforms addObject:@(SSDKPlatformTypeSMS)];
    
}

(5)QQ空间视频分享

#pragma mark QQ
+(void)ShareQQParamsByText:(NSString *)text
                          title:(NSString *)title
                            url:(NSURL *)url
                  audioFlashURL:(NSURL *)audioFlashURL
                  videoFlashURL:(NSURL *)videoFlashURL
                     thumbImage:(id)thumbImage
                         images:(id)images
                           type:(SSDKContentType)type
             forPlatformSubType:(SSDKPlatformType)platformSubType
                showInView:(UIView *)showInView{
    //1. 创建分享参数
    NSMutableDictionary *shareParams = [NSMutableDictionary dictionary];
    
    [shareParams SSDKEnableUseClientShare];
    
    NSArray *items = @[@(SSDKPlatformSubTypeQZone)];
    
    [shareParams SSDKSetupQQParamsByText:text title:title url:url audioFlashURL:audioFlashURL videoFlashURL:audioFlashURL thumbImage:thumbImage images:images type:type forPlatformSubType:platformSubType];
    //2. 分享->显示分享view
    [ShareSDK showShareActionSheet:showInView                                                                    items:items
                                                               shareParams:shareParams
                                                       onShareStateChanged:^(SSDKResponseState state, SSDKPlatformType platformType, NSDictionary *userData, SSDKContentEntity *contentEntity, NSError *error, BOOL end) {
                                                           
                                                           switch (state) {
                                                                   
                                                               case SSDKResponseStateSuccess:
                                                               {
                                                                   //分享成功
                                                                  
                                                                   NSLog(@"%@",userData);
                                                                   break;
                                                                   
                                                               }
                                                               case SSDKResponseStateFail:
                                                               {
                                                                   
                                                                   NSLog(@"%@",error);
                                                                   
                                                                   break;
                                                               }
                                                               case SSDKResponseStateCancel:
                                                               {
                                                                   
                                                                   break;
                                                               }
                                                               default:
                                                                   break;
                                                           }
                                                           
                                                       }];
    
}

(6)调用-分享图片

[YYShareSDKTool shareContentWithShareContentType:SSDKContentTypeImage contentTitle:@"锐拍" contentDescription:@"锐拍" contentImage:_imageList[indexPath.row] contentURL:nil showInView:self.view success:^{
                    
                    [UIAlertController showAlertControllerMessage:@"分享成功" WithTaraget:self];
                    
                } failure:^(NSString *failureInfo) {
                    
                    [UIAlertController showAlertControllerMessage:failureInfo WithTaraget:self];
                    
                } OtherResponseStatus:^(SSDKResponseState state) {
                    switch (state) {
                        case SSDKResponseStateCancel:{
                            
                            [UIAlertController showAlertControllerMessage:@"用户取消分享" WithTaraget:self];
                            break;
                        }
                        default:
                            break;
                    }
                }];

(7)调用-分享视频之微信好友

  [self.library assetForURL:_videoAssUrlList[indexPath.row] resultBlock:^(ALAsset *asset)
                     {
                         ALAssetRepresentation *rep = [asset defaultRepresentation];
                         
                         Byte *buffer               = (Byte*)malloc(rep.size);
                         
                         NSUInteger buffered        = [rep getBytes:buffer fromOffset:0.0 length:rep.size error:nil];
                         
                         NSData *data               = [NSData dataWithBytesNoCopy:buffer length:buffered freeWhenDone:YES];
                         
                         if (data) {
                             
                             [YYShareSDKTool shareVideoContentParamsByText:@"锐拍" title:@"锐拍" url:nil thumbImage:_videoImageList[indexPath.row] image:_videoImageList[indexPath.row] musicFileURL:nil extInfo:nil fileData:nil emoticonData:data sourceFileExtension:@"MOV" sourceFileData:data type:SSDKContentTypeFile forPlatformSubType:SSDKPlatformSubTypeWechatSession showInView:self.view success:^{
                                 
                                 [UIAlertController showAlertControllerMessage:@"分享成功" WithTaraget:self];
                                 
                             } failure:^(NSString *failureInfo) {
                                 
                                  [UIAlertController showAlertControllerMessage:failureInfo WithTaraget:self];
                                 
                             } OtherResponseStatus:^(SSDKResponseState state) {
                                 switch (state) {
                                     case SSDKResponseStateCancel:{
                                         
                                         [UIAlertController showAlertControllerMessage:@"用户取消分享" WithTaraget:self];
                                         break;
                                     }
                                     default:
                                         break;
                                 }
                             }];
                         }
                     }failureBlock:^(NSError *err) {
                         NSLog(@"Error: %@",[err localizedDescription]);
                     }];

(8)调用QQ空间视频分享(博主已经封装好了,大家在需要调用的地方调用即可,具体参数参照下图)


iOS-关于对ShareSDK第三方登录、分享功能的封装_第4张图片
Paste_Image.png

(9)第三方登录之QQ、微博、微信(微信分享免费、微信登录和支付要申请开发者认证300块一年)

#pragma mark ====第三方登录====
+(void)thirdLoginWithType:(SSDKPlatformType)type result:(ThirdLoginResult)loginResult{
    
    [ShareSDK cancelAuthorize:type];
    
    [ShareSDK getUserInfo:type onStateChanged:^(SSDKResponseState state, SSDKUser *user, NSError *error) {
        
        switch (state) {
            
            case SSDKResponseStateSuccess:{
                
                NSDictionary *dataDic  = @{kUser_Name:user.nickname,kUser_Sex:@(user.gender)};
                
                [[XRConfigs manager]setUserDic:dataDic];
                
                [[XRConfigs manager]setResPhoneDic:nil];
               
                loginResult(YES,nil);
                
                break;
            }
            case SSDKResponseStateFail:{
                
                loginResult(NO,error.localizedDescription);
                
                break;
            }
            default:
                break;
        }
    }];
}

(10)调用之第三方登录

 SSDKPlatformType type;
    
    switch (sender.tag) {
            
        case 151:{
            
            type = SSDKPlatformTypeSinaWeibo;
            
          break;
        }
        case 152:{
            
            type = SSDKPlatformTypeWechat;
            
            break;
        }
        case 153:{
            
            type = SSDKPlatformTypeQQ;
            
            break;
        }
        default:
            break;
    }
    [YYShareSDKTool thirdLoginWithType:type result:^(BOOL success, NSString *errorString) {
        
        if (success) {
            
            UIAlertController *alertC = [UIAlertController alertControllerWithTitle:@"提示" message:@"登录成功" preferredStyle:UIAlertControllerStyleAlert];
            [alertC addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                [self.navigationController popViewControllerAnimated:YES];
            }]];
            
            [self presentViewController:alertC animated:YES completion:nil];
            
        }else {
           
            [UIAlertController showAlertControllerMessage:errorString WithTaraget:self];
        }
    }];

总结:核心思想-->把block当成参数放入方法中供外界调用-->在内部分享完成之后调用block-->之后block会回到外界的block实现部分执行它的实现部分代码。
补充个小知识点->block有三部分
(1)声明
(2)实现
(3)调用
在block被调用的时候 它会回过头来走它的实现部分 这就是block的回调

本次封装就是利用了block的这一特点,在外界调用我分享方法的时候-->代码会走我这个工具类-->成功之后调用block-->然后在回到外界分享的地方继续往下执行

你可能感兴趣的:(iOS-关于对ShareSDK第三方登录、分享功能的封装)