1歹嘹、首先 cd 至項目的根目錄箩绍,執(zhí)行?pod setup;
2荞下、按需在 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模塊其他平臺迹淌,按需添加
#如果需要的平臺沒有對應(yīng)的平臺語句河绽,有2種情況——1、不需要添加這個平臺的語句唉窃,如Twitter就是這個情況耙饰。2、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'
#擴展模塊(在調(diào)用可以彈出我們UI分享方法的時候是必需的)
pod 'mob_sharesdk/ShareSDKExtension'
```
如果搜索不到我們這個mob_sharesdk時:
1苟跪、請先進行:pod setup
2、再清空一下搜索索引蔓涧,讓pod重建索引:
rm ~/Library/Caches/CocoaPods/search_index.json
注意:之前的命令寫法之后會慢慢廢棄件已,建議都使用這個新的命令寫法
CocoaPods的介紹和使用,可以參考:網(wǎng)頁鏈接
如果安裝過程中遇到問題元暴,可以參考:網(wǎng)頁鏈接
注意:如果pod更新比較慢的話篷扩,可以在后面加源,需要集成的命令寫法點擊此處
三茉盏、快速集成
第一步:設(shè)置ShareSDK的Appkey并初始化對應(yīng)的第三方社交平臺
在項目工程的Info.plist中如圖增加 MOBAppkey 和 MOBAppSecret 兩個字段
在MOBAppkey中?設(shè)置ShareSDK的appKey鉴未,如果尚未在ShareSDK官網(wǎng)注冊過App,請移步到登錄后臺進行應(yīng)用注冊 ?也可以點擊鏈接看里面的操作步驟鸠姨。
友情提示:如果您同時多個MOBSDK產(chǎn)品 在info.plist中的設(shè)置一次就可以了歼狼。
打開*AppDelegate.m(*代表你的工程名字)導(dǎo)入頭文件
```
#import
#import
//騰訊開放平臺(對應(yīng)QQ和QQ空間)SDK頭文件
#import
#import
//微信SDK頭文件
#import“WXApi.h”
//新浪微博SDK頭文件<
#import“WeiboSDK.h”
//新浪微博SDK需要在項目Build Settings中的Other Linker Flags添加”-ObjC”
//人人SDK頭文件
#import
```
在- (BOOL)application: didFinishLaunchingWithOptions:方法中調(diào)用registerApp方法來初始化SDK并且初始化第三方平臺(各社交平臺申請AppKey的網(wǎng)址及申請流程匯總)
```
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
/**初始化ShareSDK應(yīng)用
@param activePlatforms
使用的分享平臺集合
@param importHandler (onImport)
導(dǎo)入回調(diào)處理,當(dāng)某個平臺的功能需要依賴原平臺提供的SDK支持時享怀,需要在此方法中對原平臺SDK進行導(dǎo)入操作
@param configurationHandler (onConfiguration)
配置回調(diào)處理,在此方法中根據(jù)設(shè)置的platformType來填充應(yīng)用配置信息
*/
[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:
//設(shè)置新浪微博應(yīng)用信息,其中authType設(shè)置為使用SSO+Web形式授權(quán)
[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對應(yīng)一個break不要忘記填寫趟咆,不然很可能有不必要的錯誤添瓷,新浪微博的外部庫如果不要客戶端分享或者不需要加關(guān)注微博的功能可以不添加梅屉,否則要添加,QQ鳞贷,微信坯汤,google+這些外部庫文件必須要加)
```
第二步:添加實現(xiàn)代碼打開需要集成分享功能的視圖源碼,把如下代碼復(fù)制并粘貼到你要分享的位置搀愧,例如到響應(yīng)分享按鈕的方法中惰聂。并且修改相應(yīng)的參數(shù)即可。
1.需要在分享的試圖中導(dǎo)入的頭文件
```
#import
#import
```
2.調(diào)用構(gòu)造分享參數(shù)接口和分享的接口
```
//1咱筛、創(chuàng)建分享參數(shù)
????NSArray* imageArray = @[[UIImage imageNamed:@"shareImg.png"]];
(注意:圖片必須要在Xcode左邊目錄里面搓幌,名稱必須要傳正確,如果要分享網(wǎng)絡(luò)圖片迅箩,可以這樣傳iamge參數(shù) images:@[@"http://mob.com/Assets/images/logo.png?v=20150320"])
????if (imageArray) {
????????NSMutableDictionary *shareParams = [NSMutableDictionary dictionary];
[shareParams SSDKSetupShareParamsByText:@"分享內(nèi)容"
???????????????????????????????????????? images:imageArray
????????????????????????????????????????????url:[NSURL URLWithString:@"http://mob.com"]
title:@"分享標題"
?????????????????????????????????????????? type:SSDKContentTypeAuto];
//2溉愁、分享(可以彈出我們的分享菜單和編輯界面)
[ShareSDK showShareActionSheet:nil //要顯示菜單的視圖, iPad版中此參數(shù)作為彈出菜單的參照視圖,只有傳這個才可以彈出我們的分享菜單饲趋,可以傳分享的按鈕對象或者自己創(chuàng)建小的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重構(gòu)了下,所以這個彈出分享菜單的接口有點改變奕塑,如果集成的是4.1.2以及以后版本堂污,如下調(diào)用:
[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;
????????????????????????????}
}];
```
編譯并運行后,點擊分享按鈕就成功發(fā)送到社交平臺的話說明你已經(jīng)基本集成成功了龄砰。但是盟猖,為了使您的用戶有更好的分享體驗,以及您的應(yīng)用有更多的分享量寝贡,更好地在社交平臺上傳播扒披,我們建議您參考“進階功能”的指引進行SSO(免登錄)和更多社交平臺的設(shè)置。
可選:支持微信所需的相關(guān)配置及代碼在微信開放平臺(http://open.weixin.qq.com/)注冊應(yīng)用并取得應(yīng)用的AppID圃泡,然后打開下圖位置碟案,在URL Types中添加微信AppID
打開*AppDelegate.m(*代表你的工程名字)文件,導(dǎo)入微信SDK的頭文件并增加微信的外部庫接口
```
#import
//微信SDK頭文件
#import "WXApi.h"
//初始化的import參數(shù)注意要鏈接原生微信SDK颇蜡。
case SSDKPlatformTypeWechat:
???? [ShareSDKConnector connectWeChat:[WXApi class]];
break;
```
可選:支持QQ所需的相關(guān)配置及代碼 登錄QQ互聯(lián)(http://connect.qq.com/ )注冊成為開發(fā)者并登記應(yīng)用取得AppId价说,然后打開下圖位置,在URL Types中添加QQ的AppID风秤,其格式為:”QQ” + AppId的16進制(如果appId轉(zhuǎn)換的16進制數(shù)不夠8位則在前面補0鳖目,如轉(zhuǎn)換的是:5FB8B52,則最終填入為:QQ05FB8B52 注意:轉(zhuǎn)換后的字母要大寫) 轉(zhuǎn)換16進制的方法:echo ‘ibase=10;obase=16;801312852’ |bc缤弦,其中801312852為QQ的AppID领迈,見下圖
然后打開工程中的*AppDelegate.m(*代表你的工程名字)文件,導(dǎo)入QQSDK的頭文件并增加QQ的外部庫文件接口:
```
#import
#import
//騰訊開放平臺(對應(yīng)QQ和QQ空間)SDK頭文件
#import
#import
//初始化的import參數(shù)注意要鏈接原生QQSDK。
case SSDKPlatformTypeQQ:
??????[ShareSDKConnector connectQQ:[QQApiInterface class] tencentOAuthClass:[TencentOAuth class]];
break;
```
可選:配置新浪微博狸捅、Facebook的SSO 登錄新浪微博和facebook的開放平臺衷蜓,獲取AppID 。新浪微博的url scheme的設(shè)置格式為wb+Appkey(這個appkey是在微博上注冊應(yīng)用得到的appkey)尘喝,facebook的設(shè)置格式為fb+AppID(這個appid是在facebook上注冊應(yīng)用得到的appkey)磁浇。具體參考下圖:
附:目前的新浪微博SDK需要在項目的Build Settings中的Other Linker Flags添加”-ObjC”,如果不配置有可能會崩潰朽褪。步驟如下:
當(dāng)新浪微博需要客戶端分享或者增加關(guān)注微博的功能置吓,需要打開工程中的*AppDelegate.m(*代表你的工程名字)文件,導(dǎo)入新浪微博SDK的頭文件并增加新浪微博的外部庫文件接口:
```
#import
#import
//新浪微博SDK頭文件
#import "WeiboSDK.h"
//初始化的import參數(shù)注意要鏈接原生新浪微博SDK缔赠。
case SSDKPlatformTypeSinaWeibo:
??????[ShareSDKConnector connectWeibo:[WeiboSDK class]];
break;
```
ZE?k?*??9???