【ThirdParty】iOS-使用ShareSDK,輕松實(shí)現(xiàn)分享

無(wú)Demo,手殘刪掉了....
我們?cè)陂_(kāi)發(fā)過(guò)程中常碰到要進(jìn)行第三方分享的需求,我使用的是ShareSDK.

關(guān)于ShareSDK的使用,官方文檔已介紹的很詳細(xì)了,在這里跟大家分享一下我自己的思路.

ShareSDK功能介紹

1.分享與轉(zhuǎn)換
2.社會(huì)化登陸
3.評(píng)論和贊
4.社會(huì)化數(shù)據(jù)統(tǒng)計(jì)

此篇文章只介紹分享與轉(zhuǎn)換,使用的是ShareSDK-iOS v3.x,其中一張效果圖如下

IMG_6228.JPG
使用步驟1-非代碼部分
  • 獲取App Key,登陸后臺(tái),添加應(yīng)用即可
  • 下載SDK并加入工程
  • 參見(jiàn)文檔,添加你需要的相應(yīng)的平臺(tái)依賴庫(kù)

  • 對(duì)你需要分享的社交平臺(tái)授權(quán)登陸

  • 適配iOS9系統(tǒng)

  • 新浪微博需要單獨(dú)配置”O(jiān)bjC”

  • 社交平臺(tái)授權(quán)登陸詳解

    1. 在需要的開(kāi)放平臺(tái)注冊(cè)應(yīng)用,并取得相應(yīng)的AppID
      各社交平臺(tái)申請(qǐng)AppKey的網(wǎng)址及申請(qǐng)流程匯總
      你申請(qǐng)時(shí)填寫(xiě)的應(yīng)用名稱和圖標(biāo)均會(huì)顯示
4B615883-F186-4DC7-92DB-DC31AE79023E.png

B45C1F48-1F9A-4347-BDD8-BCA94C827F51.png
2. 在info.plist文件下的URL Types中添加授權(quán),配置各個(gè)社交平臺(tái)登錄時(shí)需要的url schemes即可

3. 目前新浪微博SDK需要在項(xiàng)目的Build Settings中的Other Linker Flags添加”-ObjC”,如果不配置有可能會(huì)崩潰
  • 適配iOS9系統(tǒng)詳解

    1. iOS9系統(tǒng)默認(rèn)會(huì)攔截對(duì)http協(xié)議接口的訪問(wèn),但大部分社交平臺(tái)接口不支持https協(xié)議,可能無(wú)法授權(quán)分享,解決辦法為關(guān)閉https, 使用http協(xié)議

    2. iOS9新建項(xiàng)目默認(rèn)需要支持Bitcode,但小部分社交平臺(tái)SDK不支持Bitcode,解決辦法為暫時(shí)關(guān)閉對(duì)Bitcode的支持

    3. iOS9如果涉及到平臺(tái)客戶端跳轉(zhuǎn),系統(tǒng)會(huì)自動(dòng)到項(xiàng)目
      info.plist下檢測(cè)是否設(shè)置了平臺(tái)Scheme,對(duì)于需要配置的平臺(tái),如果沒(méi)有配置,就無(wú)法正常跳轉(zhuǎn)平臺(tái)客戶端。因此要支持客戶端的分享和授權(quán)等,需要配置Scheme名單,即白名單

注意! 若想跳轉(zhuǎn)客戶端,既要添加白名單,又要在URL Types里面添加授權(quán),缺一不可!

使用步驟2-代碼部分
  • 導(dǎo)入ShareSDK及其他平臺(tái)SDK頭文件
  • 設(shè)置ShareSDK的App Key
  • 初始化第三方平臺(tái)
  • 添加分享實(shí)現(xiàn)代碼
列舉幾個(gè)常用的分享實(shí)現(xiàn)方法
  • 首先要設(shè)置分享參數(shù)(標(biāo)題,內(nèi)容,圖片,路徑,內(nèi)容類型)
  • 可直接定制社交平臺(tái)的分享參數(shù)(各平臺(tái)參數(shù)不同)
  • 直接分享內(nèi)容(設(shè)置要分享的平臺(tái),分享參數(shù),回調(diào))
  • 進(jìn)行內(nèi)容編輯后分享(比直接分享內(nèi)容,多了一個(gè)其他分享平臺(tái)類型的參數(shù))
  • 顯示分享菜單(視圖,可調(diào)整順序的菜單項(xiàng),分享參數(shù)[可統(tǒng)一內(nèi)容,也可單獨(dú)設(shè)置每個(gè)平臺(tái)分享的不同內(nèi)容],狀態(tài)變更事件)
  • 還有很多方法,例如"一鍵分享至多個(gè)平臺(tái)""@好友和話題"等等,可查閱頭文件,在這里不一一列舉了

設(shè)置分享參數(shù)

- (void)SSDKSetupShareParamsByText:(NSString *)text
                            images:(id)images
                               url:(NSURL *)url
                             title:(NSString *)title
                              type:(SSDKContentType)type;

直接定制微信好友的分享參數(shù)

- (void)SSDKSetupWeChatParamsByText:(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
                               type:(SSDKContentType)type
                 forPlatformSubType:(SSDKPlatformType)platformSubType;

直接分享內(nèi)容

+ (void)share:(SSDKPlatformType)platformType
   parameters:(NSMutableDictionary *)parameters
onStateChanged:(SSDKShareStateChangedHandler)stateChangedHandler;

進(jìn)行內(nèi)容編輯后分享

+ (SSUIShareContentEditorViewController *)showShareEditor:(SSDKPlatformType)platformType
                                        otherPlatformTypes:(NSArray *)otherPlatformTypes
                                              shareParams:(NSMutableDictionary *)shareParams
                                      onShareStateChanged:(SSUIShareStateChangedHandler)shareStateChangedHandler;

顯示分享菜單

+ (SSUIShareActionSheetController *)showShareActionSheet:(UIView *)view
                                                   items:(NSArray *)items
                                             shareParams:(NSMutableDictionary *)shareParams
                                     onShareStateChanged:(SSUIShareStateChangedHandler)shareStateChangedHandler;
  • 總結(jié),若想分享到某一社交平臺(tái):
    要添加社交平臺(tái)依賴庫(kù)
    一定要在社交平臺(tái)獲得授權(quán)并在你的工程內(nèi)授權(quán)登陸
    不要忘記添加白名單
    在AppDelegate中導(dǎo)入社交平臺(tái)SDK頭文件
    在AppDelegate中初始化社交平臺(tái)
    然后寫(xiě)代碼就OK了
Demo1: 實(shí)現(xiàn)直接分享至新浪微博功能

初始化新浪微博平臺(tái)代碼

    /**
     *  參數(shù)1:    傳入AppKey
     *  參數(shù)2:    需要連接社交平臺(tái)SDK時(shí)觸發(fā)
     *  參數(shù)3:    需要連接社交平臺(tái)SDK時(shí)觸發(fā)
     *  參數(shù)4:    在此事件中寫(xiě)入連接代碼。第四個(gè)參數(shù)則為配置本地社交平臺(tái)時(shí)觸發(fā),根據(jù)返回的平臺(tái)類型來(lái)配置平臺(tái)信息。
     */
    
    [ShareSDK registerApp:@"你的App Key"
          activePlatforms:@[
                            @(SSDKPlatformTypeSinaWeibo),
                            ]
                 onImport:^(SSDKPlatformType platformType)
                            {
                                switch (platformType)
                                    {
                                        case SSDKPlatformTypeSinaWeibo:
                                            [ShareSDKConnector connectWeibo:[WeiboSDK class]];
                                            break;
                                        default:
                                            break;
                                    }
                            }
     
          onConfiguration:^(SSDKPlatformType platformType, NSMutableDictionary *appInfo)
                            {
         
                                switch (platformType)
                                    {
                                        case SSDKPlatformTypeSinaWeibo:
                                            //設(shè)置新浪微博應(yīng)用信息,其中authType設(shè)置為使用SSO+Web形式授權(quán)
                                            [appInfo SSDKSetupSinaWeiboByAppKey:@"你的Key"
                                                     appSecret:@"你的Secret"
                                                     redirectUri:@"你要分享的路徑"
                                                     authType:SSDKAuthTypeBoth];
                                            break;
                                        default:
                                            break;
                                    }
         
                            }
     ];

實(shí)現(xiàn)分享代碼

    //調(diào)用構(gòu)造分享參數(shù)接口和分享的接口
        NSArray* imageArray = @[[UIImage imageNamed:@"222.jpg"],[UIImage imageNamed:@"111.jpg"]];
           if (imageArray)
           {
            //創(chuàng)建分享參數(shù)
            NSMutableDictionary *shareParamDic = [NSMutableDictionary dictionary];
            [shareParamDic SSDKSetupShareParamsByText:@"我是分享的具體內(nèi)容" images:imageArray url:[NSURL URLWithString:@"http://www.baidu.com"] title:@"我是分享的標(biāo)題" type:SSDKContentTypeAuto];
    
            //進(jìn)行分享(可以彈出我們的分享菜單和編輯界面)
            [ShareSDK share:SSDKPlatformTypeSinaWeibo
                      parameters:shareParamDic
                      onStateChanged:^(SSDKResponseState state, NSDictionary *userData, SSDKContentEntity *contentEntity, NSError *error)
               {
                  switch (state)
                   {
                     case SSDKResponseStateSuccess:
                     {
                         UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"分享成功"                                                                         message:nil                                                                         delegate:nil                                                              cancelButtonTitle:@"確定"                                                            otherButtonTitles:nil];
                         [alertView show];
                         break;
                     }
                     case SSDKResponseStateFail:
                     {
                         UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"分享失敗"                                                                         message:[NSString stringWithFormat:@"%@", error]                                                                        delegate:nil                                                            cancelButtonTitle:@"確定"                                                             otherButtonTitles:nil];
                         [alertView show];
                         break;
                     }
                     case SSDKResponseStateCancel:
                     {
                         UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"分享已取消"                                                                         message:nil                                                                       delegate:nil                                                              cancelButtonTitle:@"確定"                                                                otherButtonTitles:nil];
                         [alertView show];
                         break;
                     }
                     default:
                         break;
                 }
             }];
        }

效果圖

IMG_6224.JPG

注意:
正常情況下,現(xiàn)在已經(jīng)可以跳轉(zhuǎn)新浪微博,直接進(jìn)行分享了,但若有以下幾種情況,會(huì)導(dǎo)致無(wú)法正常分享

  • 在sina開(kāi)放平臺(tái)中,你的應(yīng)用程序必須綁定正確的Bundle Id,若填寫(xiě)錯(cuò)誤,會(huì)顯示下圖
IMG_6222.JPG
  • 在sina開(kāi)放平臺(tái)中,你的應(yīng)用程序必須是審核通過(guò)的,如果未通過(guò),無(wú)法實(shí)現(xiàn)自動(dòng)登錄微博分享,會(huì)彈出網(wǎng)頁(yè)需要你手動(dòng)登錄,登錄后也不會(huì)跳轉(zhuǎn)微博,會(huì)直接返回提示你分享成功
![FullSizeRender(2).jpg](http://upload-images.jianshu.io/upload_images/1602974-7cb4da7576fbfd1f.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
  • 補(bǔ)充一個(gè),如果沒(méi)有初始化社交平臺(tái),會(huì)出現(xiàn)下圖
FullSizeRender(3).jpg
Demo2: 實(shí)現(xiàn)顯示分享菜單功能

初始化各個(gè)平臺(tái)代碼

    [ShareSDK registerApp:@"你的App Key"
          activePlatforms:@[
                            @(SSDKPlatformTypeSinaWeibo),
                            @(SSDKPlatformSubTypeWechatSession),
                            @(SSDKPlatformSubTypeWechatTimeline),
                            @(SSDKPlatformSubTypeWechatFav),
                            @(SSDKPlatformTypeQQ),
                            @(SSDKPlatformSubTypeQZone),
                            ]
                 onImport:^(SSDKPlatformType platformType)
                            {
                                switch (platformType)
                                    {
                                        case SSDKPlatformTypeSinaWeibo:
                                            [ShareSDKConnector connectWeibo:[WeiboSDK class]];
                                            break;
                                        case SSDKPlatformTypeWechat:
                                        {                               
                                            [ShareSDKConnector connectWeChat:[WXApi class]];                                           
                                        }
                                        case SSDKPlatformTypeQQ:
                                        {                                            
                                            [ShareSDKConnector connectQQ:[QQApiInterface class] tencentOAuthClass:[TencentOAuth class]];                                          
                                        }
                                        default:
                                            break;
                                    }
                            }
     
          onConfiguration:^(SSDKPlatformType platformType, NSMutableDictionary *appInfo)
                            {         
                                switch (platformType)
                                    {
                                        case SSDKPlatformTypeSinaWeibo:
                                            //設(shè)置新浪微博應(yīng)用信息,其中authType設(shè)置為使用SSO+Web形式授權(quán)
                                            [appInfo SSDKSetupSinaWeiboByAppKey:@"你的Key"
                                                     appSecret:@"你的Secret"                                                     redirectUri:@"你的分享鏈接"
                                                     authType:SSDKAuthTypeBoth];
                                            break;
                                        case SSDKPlatformTypeWechat:
                                            [appInfo SSDKSetupWeChatByAppId:@""
                                                            appSecret:@""];
                                            break;
                                        case SSDKPlatformTypeQQ:                                            
                                            [appInfo SSDKSetupQQByAppId:@"" appKey:@"" authType:SSDKAuthTypeSSO];
                                           break;
                                        default:
                                            break;
                                    }
         
                            }
     ];


實(shí)現(xiàn)分享代碼

    //設(shè)置分享參數(shù),內(nèi)容一致
    NSMutableDictionary *shareParams = [NSMutableDictionary dictionary];
    [shareParams SSDKSetupShareParamsByText:[NSString stringWithFormat:@"%@%@", @"你好", [NSURL URLWithString:@"http://wiki.mob.com/ios簡(jiǎn)潔版快速集成/"]] 
                                     images:[NSURL URLWithString:@"111.jpg"]   
                                        url:[NSURL URLWithString:@"111.jpg"]     
                                      title:@"llalal"   
                                       type:SSDKContentTypeAuto];
   // 顯示分享菜單   
    SSUIShareActionSheetController *sheet =  [ShareSDK showShareActionSheet:nil                                   
                                                                      items:@[
                                                                              @(SSDKPlatformSubTypeWechatFav),                                                                              @(SSDKPlatformTypeSinaWeibo),                                                                             @(SSDKPlatformTypeQQ),                                                                              @(SSDKPlatformSubTypeQZone),
@(SSDKPlatformSubTypeWechatSession),
@(SSDKPlatformSubTypeWechatTimeline),
]                                                               shareParams:shareParams
onShareStateChanged:^(SSDKResponseState state, SSDKPlatformType platformType, NSDictionary *userData, SSDKContentEntity *contentEntity, NSError *error, BOOL end) {             
                                                            switch (state) 
{                                                             
                                                                case SSDKResponseStateBegin: 
                                                                    break;   
                                                                case SSDKResponseStateSuccess:           
                                                                    if (platformType == SSDKPlatformTypeCopy)
 {
NSLog(@"復(fù)制成功");
 }
else
{
NSLog(@"分享成功");
}
break;
 case  SSDKResponseStateFail:
if (platformType == SSDKPlatformTypeCopy) 
{                                                                      NSLog(@"復(fù)制失敗");
}
else
{
NSLog(@"分享失敗");
}
NSLog(@"失?。?@", error);
 break;
default:
break;
}
 }];
    

效果圖為本文第一張圖

  • 備注:如果要中文顯示,需要在info.plist文件中設(shè)置一下,如下圖所示
![LBRJWGWJ9B(X376F~~QF4`U.jpg](http://upload-images.jianshu.io/upload_images/1602974-3b8bd7947f2ba4ee.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

效果圖如下

IMG_6308.PNG
參考文檔

ShareSDK官方文檔
適配iOS9文檔

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容