简单的使用CocoaPods集成ShareSDK

一、首先 cd 至项目的根目录,执行 pod setup;
二、按需在 Podfile 文件中添加命令:

```
# 主模块(必须)
pod 'mob_sharesdk'
# UI模块(非必须,须要用到ShareSDK提供的分享菜单栏和分享编辑页面须要如下1行)
pod 'mob_sharesdk/ShareSDKUI'
# 平台SDK模块(对照一下平台,须要的加上。若是只须要QQ、微信、新浪微博,只须要如下3行)
pod 'mob_sharesdk/ShareSDKPlatforms/QQ'
pod 'mob_sharesdk/ShareSDKPlatforms/SinaWeibo'
pod 'mob_sharesdk/ShareSDKPlatforms/WeChat'   //(微信sdk不带支付的命令)
# pod 'mob_sharesdk/ShareSDKPlatforms/WeChatFull' //(微信sdk带支付的命令,和上面不带支付的不能共存,只能选择一个)
 
# ShareSDKPlatforms模块其余平台,按需添加
# 若是须要的平台没有对应的平台语句,有2种状况——一、不须要添加这个平台的语句,如Twitter就是这个状况。二、ShareSDK暂时不支持此平台。
pod 'mob_sharesdk/ShareSDKPlatforms/RenRen'
pod 'mob_sharesdk/ShareSDKPlatforms/AliPaySocial'
pod 'mob_sharesdk/ShareSDKPlatforms/Kakao'
pod 'mob_sharesdk/ShareSDKPlatforms/Yixin'
pod 'mob_sharesdk/ShareSDKPlatforms/Facebook'
pod 'mob_sharesdk/ShareSDKPlatforms/Copy'
pod 'mob_sharesdk/ShareSDKPlatforms/Evernote'
pod 'mob_sharesdk/ShareSDKPlatforms/GooglePlus'
pod 'mob_sharesdk/ShareSDKPlatforms/Instagram'
pod 'mob_sharesdk/ShareSDKPlatforms/Instapaper'
pod 'mob_sharesdk/ShareSDKPlatforms/Line'
pod 'mob_sharesdk/ShareSDKPlatforms/Mail'
pod 'mob_sharesdk/ShareSDKPlatforms/SMS'
pod 'mob_sharesdk/ShareSDKPlatforms/WhatsApp'
pod 'mob_sharesdk/ShareSDKPlatforms/MeiPai'
pod 'mob_sharesdk/ShareSDKPlatforms/DingTalk'
pod 'mob_sharesdk/ShareSDKPlatforms/YouTube'
pod 'mob_sharesdk/ShareSDKPlatforms/Twitter'
pod 'mob_sharesdk/ShareSDKPlatforms/Dropbox'
# 使用配置文件分享模块(非必需)
pod 'mob_sharesdk/ShareSDKConfigFile'
# 扩展模块(在调用能够弹出咱们UI分享方法的时候是必需的)
pod 'mob_sharesdk/ShareSDKExtension'
```
若是搜索不到咱们这个mob_sharesdk时:
一、请先进行:pod setup
二、再清空一下搜索索引,让pod重建索引:
rm ~/Library/Caches/CocoaPods/search_index.json
注意:以前的命令写法以后会慢慢废弃,建议都使用这个新的命令写法
CocoaPods的介绍和使用,能够参考:网页连接
若是安装过程当中遇到问题,能够参考:网页连接
注意:若是pod更新比较慢的话,能够在后面加源,须要集成的命令写法点击此处
3、快速集成
 第一步:设置ShareSDK的Appkey并初始化对应的第三方社交平台
在项目工程的Info.plist 中如图增长 MOBAppkey 和 MOBAppSecret 两个字段
在 MOBAppkey中 设置ShareSDK的appKey,若是还没有在ShareSDK官网注册过App,请移步到登陆后台进行应用注册  也能够点击连接看里面的操做步骤。


友情提示:若是您同时多个MOBSDK产品 在info.plist中的设置一次就能够了。
打开*AppDelegate.m(*表明你的工程名字)导入头文件
```
#import <ShareSDK/ShareSDK.h>
#import <ShareSDKConnector/ShareSDKConnector.h>
//腾讯开放平台(对应QQ和QQ空间)SDK头文件
#import <TencentOpenAPI/TencentOAuth.h>
#import <TencentOpenAPI/QQApiInterface.h>
//微信SDK头文件
#import “WXApi.h”
//新浪微博SDK头文件<
#import “WeiboSDK.h”
//新浪微博SDK须要在项目Build Settings中的Other Linker Flags添加”-ObjC”
//人人SDK头文件
#import <RennSDK/RennSDK.h>
```
在- (BOOL)application: didFinishLaunchingWithOptions:方法中调用registerApp方法来初始化SDK而且初始化第三方平台(各社交平台申请AppKey的网址及申请流程汇总)
```
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
/**初始化ShareSDK应用
@param activePlatforms
使用的分享平台集合
@param importHandler (onImport)
导入回调处理,当某个平台的功能须要依赖原平台提供的SDK支持时,须要在此方法中对原平台SDK进行导入操做
@param configurationHandler (onConfiguration)
配置回调处理,在此方法中根据设置的platformType来填充应用配置信息
*/
[ShareSDK registerActivePlatforms:@[
                            @(SSDKPlatformTypeSinaWeibo),
                            @(SSDKPlatformTypeMail),
                            @(SSDKPlatformTypeSMS),
                            @(SSDKPlatformTypeCopy),
                            @(SSDKPlatformTypeWechat),
                            @(SSDKPlatformTypeQQ),
                            @(SSDKPlatformTypeRenren),
                            @(SSDKPlatformTypeFacebook),
                            @(SSDKPlatformTypeTwitter),
                            @(SSDKPlatformTypeGooglePlus),
]
           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 SSDKPlatformTypeRenren:
                          [ShareSDKConnector connectRenren:[RennClient class]];
                    break;
                  default:
                    break;
                }
          }
          onConfiguration:^(SSDKPlatformType platformType, NSMutableDictionary *appInfo)
          {
              switch (platformType)
              {
                  case SSDKPlatformTypeSinaWeibo:
                      //设置新浪微博应用信息,其中authType设置为使用SSO+Web形式受权
                      [appInfo SSDKSetupSinaWeiboByAppKey:@”568898243″
                                                appSecret:@”38a4f8204cc784f81f9f0daaf31e02e3″
                                              redirectUri:@”http://www.sharesdk.cn“
                                                 authType:SSDKAuthTypeBoth];
                   break;
                  case SSDKPlatformTypeWechat:
                      [appInfo SSDKSetupWeChatByAppId:@”wx4868b35061f87885″
                                            appSecret:@”64020361b8ec4c99936c0e3999a9f249″];
                   break;
                  case SSDKPlatformTypeQQ:
                      [appInfo SSDKSetupQQByAppId:@”100371282″
                                           appKey:@”aed9b0303e3ed1e27bae87c33761161d”
                                         authType:SSDKAuthTypeBoth];
                   break;
                  case SSDKPlatformTypeRenren:
                      [appInfo        SSDKSetupRenRenByAppId:@”226427″
                                               appKey:@”fc5b8aed373c4c27a05b712acba0f8c3″
                                            secretKey:@”f29df781abdd4f49beca5a2194676ca4″
                                             authType:SSDKAuthTypeBoth];
                    break;
                  case SSDKPlatformTypeFacebook:
                      [appInfo SSDKSetupFacebookByApiKey:@”107704292745179″
                                               appSecret:@”38053202e1a5fe26c80c753071f0b573″
                                            displayName:@”shareSDK”
                                             authType:SSDKAuthTypeBoth];
                    break;
                  case SSDKPlatformTypeTwitter:
                      [appInfo SSDKSetupTwitterByConsumerKey:@”LRBM0H75rWrU9gNHvlEAA2aOy”
                                               consumerSecret:@”gbeWsZvA9ELJSdoBzJ5oLKX0TU09UOwrzdGfo9Tg7DjyGuMe8G”
                                            redirectUri:@”http://mob.com”]
                    break;
                   case SSDKPlatformTypeGooglePlus:
                     [appInfo SSDKSetupGooglePlusByClientID:@”232554794995.apps.googleusercontent.com“
                                                clientSecret:@”PEdFgtrMw97aCvf0joQj7EMk”
                                                 redirectUri:@”http://localhost“];
                    break;
                   default:
                    break;
              }
          }];
return YES;
}
(注意:每个case对应一个break不要忘记填写,否则极可能有没必要要的错误,新浪微博的外部库若是不要客户端分享或者不须要加关注微博的功能能够不添加,不然要添加,QQ,微信,google+这些外部库文件必需要加)
```

第二步:添加实现代码 打开须要集成分享功能的视图源码,把以下代码复制并粘贴到你要分享的位置,例如到响应分享按钮的方法中。而且修改相应的参数便可。
1.须要在分享的试图中导入的头文件
```
#import <ShareSDK/ShareSDK.h>
#import <ShareSDKUI/ShareSDK+SSUI.h>
```
2.调用构造分享参数接口和分享的接口
```
//一、建立分享参数
    NSArray* imageArray = @[[UIImage imageNamed:@"shareImg.png"]];
    (注意:图片必需要在Xcode左边目录里面,名称必需要传正确,若是要分享网络图片,能够这样传iamge参数 images:@[@"http://mob.com/Assets/images/logo.png?v=20150320"])
    if (imageArray) {
 
        NSMutableDictionary *shareParams = [NSMutableDictionary dictionary];
        [shareParams SSDKSetupShareParamsByText:@"分享内容"
                                         images:imageArray
                                            url:[NSURL URLWithString:@"http://mob.com"]
                                          title:@"分享标题"
                                           type:SSDKContentTypeAuto];
//二、分享(能够弹出咱们的分享菜单和编辑界面)
        [ShareSDK showShareActionSheet:nil //要显示菜单的视图, iPad版中此参数做为弹出菜单的参照视图,只有传这个才能够弹出咱们的分享菜单,能够传分享的按钮对象或者本身建立小的view 对象,iPhone能够传nil不会影响
                                 items:nil
                           shareParams:shareParams
                   onShareStateChanged:^(SSDKResponseState state, SSDKPlatformType platformType, NSDictionary *userData, SSDKContentEntity *contentEntity, NSError *error, BOOL end) {
 
                       switch (state) {
                           case SSDKResponseStateSuccess:
                           {
                               UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"分享成功"
                                                                                   message:nil
                                                                                  delegate:nil
                                                                         cancelButtonTitle:@"肯定"
                                                                         otherButtonTitles:nil];
                               [alertView show];
                               break;
                           }
                           case SSDKResponseStateFail:
                           {
                               UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"分享失败"
                                                                                   message:[NSString stringWithFormat:@"%@",error]
                                                                                  delegate:nil
                                                                         cancelButtonTitle:@"OK"
                                                                         otherButtonTitles:nil, nil];
                                [alert show];
                                break;
                               }
                            default:
                               break;
                       }];
 
//你们请注意:4.1.2版本开始由于UI重构了下,因此这个弹出分享菜单的接口有点改变,若是集成的是4.1.2以及之后版本,以下调用:
[ShareSDK showShareActionSheet:nil customItems:nil shareParams:shareParams sheetConfiguration:nil onStateChanged:^(SSDKResponseState state, SSDKPlatformType platformType, NSDictionary *userData, SSDKContentEntity *contentEntity, NSError *error, BOOL end) {
        switch (state) { 
                           case SSDKResponseStateSuccess:
                           {
                               UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"分享成功"
                                                                                   message:nil
                                                                                  delegate:nil
                                                                         cancelButtonTitle:@"肯定"
                                                                         otherButtonTitles:nil];
                               [alertView show];
                               break;
                           }
                           case SSDKResponseStateFail:
                           {
                               UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"分享失败"
                                                                                   message:[NSString stringWithFormat:@"%@",error]
                                                                                  delegate:nil
                                                                         cancelButtonTitle:@"OK"
                                                                         otherButtonTitles:nil, nil];
                                [alert show];
                                break;
                            }
                            default:
                               break;
                            }
}];
```
编译并运行后,点击分享按钮就成功发送到社交平台的话说明你已经基本集成成功了。可是,为了使您的用户有更好的分享体验,以及您的应用有更多的分享量,更好地在社交平台上传播,咱们建议您参考“进阶功能”的指引进行SSO(免登陆)和更多社交平台的设置。
可选:支持微信所需的相关配置及代码 在微信开放平台(http://open.weixin.qq.com/ )注册应用并取得应用的AppID,而后打开下图位置,在URL Types中添加微信AppID


打开*AppDelegate.m(*表明你的工程名字)文件,导入微信SDK的头文件并增长微信的外部库接口
```
#import <ShareSDKConnector/ShareSDKConnector.h>
//微信SDK头文件
#import "WXApi.h"
//初始化的import参数注意要连接原生微信SDK。
case SSDKPlatformTypeWechat:
     [ShareSDKConnector connectWeChat:[WXApi class]];
break;
```
可选:支持QQ所需的相关配置及代码 登陆QQ互联(http://connect.qq.com/ )注册成为开发者并登记应用取得AppId,而后打开下图位置,在URL Types中添加QQ的AppID,其格式为:”QQ” + AppId的16进制(若是appId转换的16进制数不够8位则在前面补0,如转换的是:5FB8B52,则最终填入为:QQ05FB8B52 注意:转换后的字母要大写) 转换16进制的方法:echo ‘ibase=10;obase=16;801312852’ |bc,其中801312852为QQ的AppID,见下图


而后打开工程中的*AppDelegate.m(*表明你的工程名字)文件,导入QQSDK的头文件并增长QQ的外部库文件接口:
```
#import <ShareSDK/ShareSDK.h>
#import <ShareSDKConnector/ShareSDKConnector.h>
//腾讯开放平台(对应QQ和QQ空间)SDK头文件
#import <TencentOpenAPI/TencentOAuth.h>
#import <TencentOpenAPI/QQApiInterface.h>
//初始化的import参数注意要连接原生QQSDK。
case SSDKPlatformTypeQQ:
      [ShareSDKConnector connectQQ:[QQApiInterface class] tencentOAuthClass:[TencentOAuth class]];
break;
```
可选:配置新浪微博、Facebook的SSO 登陆新浪微博和facebook的开放平台,获取AppID 。新浪微博的url scheme的设置格式为wb+Appkey(这个appkey是在微博上注册应用获得的appkey),facebook的设置格式为fb+AppID(这个appid是在facebook上注册应用获得的appkey)。具体参考下图:


附:目前的新浪微博SDK须要在项目的Build Settings中的Other Linker Flags添加”-ObjC”,若是不配置有可能会崩溃。步骤以下:


当新浪微博须要客户端分享或者增长关注微博的功能,须要打开工程中的*AppDelegate.m(*表明你的工程名字)文件,导入新浪微博SDK的头文件并增长新浪微博的外部库文件接口:
```
#import <ShareSDK/ShareSDK.h>
#import <ShareSDKConnector/ShareSDKConnector.h>
//新浪微博SDK头文件
#import "WeiboSDK.h"
//初始化的import参数注意要连接原生新浪微博SDK。
case SSDKPlatformTypeSinaWeibo:
      [ShareSDKConnector connectWeibo:[WeiboSDK class]];
break;
```
相关文章
相关标签/搜索