iOS使用ShareSDK,輕松實現分享

愛你愛自己 8年前發布 | 38K 次閱讀 IOS iOS開發 移動開發

我們在開發過程中常碰到要進行第三方分享的需求,我使用的是ShareSDK.

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

ShareSDK功能介紹

1.分享與轉換
2.社會化登陸
3.評論和贊
4.社會化數據統計

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

iOS使用ShareSDK,輕松實現分享

使用步驟1-非代碼部分

  • 獲取App Key,登陸后臺,添加應用即可
  • 下載SDK并加入工程
  • 參見文檔,添加你需要的相應的平臺依賴庫
  • 對你需要分享的社交平臺授權登陸
  • 適配iOS9系統
  • 新浪微博需要單獨配置”ObjC”
iOS使用ShareSDK,輕松實現分享

4B615883-F186-4DC7-92DB-DC31AE79023E.png

iOS使用ShareSDK,輕松實現分享

B45C1F48-1F9A-4347-BDD8-BCA94C827F51.png

2. 在info.plist文件下的URL Types中添加授權,配置各個社交平臺登錄時需要的url schemes即可

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

    1. iOS9系統默認會攔截對http協議接口的訪問,但大部分社交平臺接口不支持https協議,可能無法授權分享,解決辦法為關閉https, 使用http協議

    2. iOS9新建項目默認需要支持Bitcode,但小部分社交平臺SDK不支持Bitcode,解決辦法為暫時關閉對Bitcode的支持

    3. iOS9如果涉及到平臺客戶端跳轉,系統會自動到項目
      info.plist下檢測是否設置了平臺Scheme,對于需要配置的平臺,如果沒有配置,就無法正常跳轉平臺客戶端。因此要支持客戶端的分享和授權等,需要配置Scheme名單,即白名單

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

使用步驟2-代碼部分

  • 導入ShareSDK及其他平臺SDK頭文件
  • 設置ShareSDK的App Key
  • 初始化第三方平臺
  • 添加分享實現代碼

列舉幾個常用的分享實現方法

  • 首先要設置分享參數(標題,內容,圖片,路徑,內容類型)
  • 可直接定制社交平臺的分享參數(各平臺參數不同)
  • 直接分享內容(設置要分享的平臺,分享參數,回調)
  • 進行內容編輯后分享(比直接分享內容,多了一個其他分享平臺類型的參數)
  • 顯示分享菜單(視圖,可調整順序的菜單項,分享參數[可統一內容,也可單獨設置每個平臺分享的不同內容],狀態變更事件)
  • 還有很多方法,例如"一鍵分享至多個平臺""@好友和話題"等等,可查閱頭文件,在這里不一一列舉了

設置分享參數

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

直接定制微信好友的分享參數

- (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;

直接分享內容

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

進行內容編輯后分享

+ (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;
  • 總結,若想分享到某一社交平臺:
    要添加社交平臺依賴庫
    一定要在社交平臺獲得授權并在你的工程內授權登陸
    不要忘記添加白名單
    在AppDelegate中導入社交平臺SDK頭文件
    在AppDelegate中初始化社交平臺
    然后寫代碼就OK了

Demo1: 實現直接分享至新浪微博功能

初始化新浪微博平臺代碼

    /**
     *  參數1:    傳入AppKey
     *  參數2:    需要連接社交平臺SDK時觸發
     *  參數3:    需要連接社交平臺SDK時觸發
     *  參數4:    在此事件中寫入連接代碼。第四個參數則為配置本地社交平臺時觸發,根據返回的平臺類型來配置平臺信息。
     */

    [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:
                                            //設置新浪微博應用信息,其中authType設置為使用SSO+Web形式授權
                                            [appInfo SSDKSetupSinaWeiboByAppKey:@"你的Key"
                                                     appSecret:@"你的Secret"
                                                     redirectUri:@"你要分享的路徑"
                                                     authType:SSDKAuthTypeBoth];
                                            break;
                                        default:
                                            break;
                                    }

                            }
     ];

實現分享代碼

    //調用構造分享參數接口和分享的接口
        NSArray* imageArray = @[[UIImage imageNamed:@"222.jpg"],[UIImage imageNamed:@"111.jpg"]];
           if (imageArray)
           {
            //創建分享參數
            NSMutableDictionary *shareParamDic = [NSMutableDictionary dictionary];
            [shareParamDic SSDKSetupShareParamsByText:@"我是分享的具體內容" images:imageArray url:[NSURL URLWithString:@"http://www.baidu.com"] title:@"我是分享的標題" type:SSDKContentTypeAuto];

            //進行分享(可以彈出我們的分享菜單和編輯界面)
            [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;
                 }
             }];
        }

效果圖

iOS使用ShareSDK,輕松實現分享

注意:
正常情況下,現在已經可以跳轉新浪微博,直接進行分享了,但若有以下幾種情況,會導致無法正常分享

  • 在sina開放平臺中,你的應用程序必須綁定正確的Bundle Id,若填寫錯誤,會顯示下圖

iOS使用ShareSDK,輕松實現分享

  • 在sina開放平臺中,你的應用程序必須是審核通過的,如果未通過,無法實現自動登錄微博分享,會彈出網頁需要你手動登錄,登錄后也不會跳轉微博,會直接返回提示你分享成功

iOS使用ShareSDK,輕松實現分享

iOS使用ShareSDK,輕松實現分享

  • 補充一個,如果沒有初始化社交平臺,會出現下圖

iOS使用ShareSDK,輕松實現分享

Demo2: 實現顯示分享菜單功能

初始化各個平臺代碼

    [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:
                                            //設置新浪微博應用信息,其中authType設置為使用SSO+Web形式授權
                                            [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;
                                    }

                            }
     ];

實現分享代碼

    //設置分享參數,內容一致
    NSMutableDictionary *shareParams = [NSMutableDictionary dictionary];
    [shareParams SSDKSetupShareParamsByText:[NSString stringWithFormat:@"%@%@", @"你好", [NSURL URLWithString:@"http://wiki.mob.com/ios簡潔版快速集成/"]] 
                                     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(@"復制成功");
 }
else
{
NSLog(@"分享成功");
}
break;
 case  SSDKResponseStateFail:
if (platformType == SSDKPlatformTypeCopy) 
{                                                                      NSLog(@"復制失敗");
}
else
{
NSLog(@"分享失敗");
}
NSLog(@"失敗:%@", error);
 break;
default:
break;
}
 }];
  • 備注:如果要中文顯示,需要在info.plist文件中設置一下,如下圖所示

iOS使用ShareSDK,輕松實現分享

iOS使用ShareSDK,輕松實現分享

效果圖如下

iOS使用ShareSDK,輕松實現分享

參考文檔

ShareSDK官方文檔
適配iOS9文檔


 

閱讀原文

 

 本文由用戶 愛你愛自己 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!