簡單的使用CocoaPods集成ShareSDK

1、首先 cd 至項目的根目錄,執行 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模塊其他平臺,按需添加# 如果需要的平臺沒有對應的平臺語句,有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'# 擴展模塊(在調用可以彈出我們UI分享方法的時候是必需的)pod 'mob_sharesdk/ShareSDKExtension'

如果搜索不到我們這個mob_sharesdk時:

1、請先進行:pod setup

2、再清空一下搜索索引,讓pod重建索引:

rm ~/Library/Caches/CocoaPods/search_index.json

注意:之前的命令寫法之後會慢慢廢棄,建議都使用這個新的命令寫法

CocoaPods的介紹和使用,可以參考:https://www.jianshu.com/p/c1449e8389a3

如果安裝過程中遇到問題,可以參考:wiki.mob.com/

注意:如果pod更新比較慢的話,可以在後面加源,需要集成的命令寫法,點擊此處:http://bbs.mob.com/forum.php?mod=viewthread&tid=23432&page=1&extra=#pid56409

快速集成

第一步:設置ShareSDK的Appkey並初始化對應的第三方社交平臺

在項目工程的Info.plist 中如圖增加 MOBAppkey 和 MOBAppSecret 兩個字段

在 MOBAppkey中 設置ShareSDK的appKey,如果尚未在ShareSDK官網註冊過App,請移步到登錄後臺進行應用註冊 也可以點擊鏈接看裡面的操作步驟。

簡單的使用CocoaPods集成ShareSDK

友情提示:如果您同時多個MOBSDK產品 在info.plist中的設置一次就可以了。

打開AppDelegate.m(代表你的工程名字)導入頭文件

#import #import //騰訊開放平臺(對應QQ和QQ空間)SDK頭文件#import #import //微信SDK頭文件#import “WXApi.h”//新浪微博SDK頭文件

在- (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 #import 

2.調用構造分享參數接口和分享的接口

//1、創建分享參數 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];//2、分享(可以彈出我們的分享菜單和編輯界面) [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(免登錄)和更多社交平臺的設置。

簡單的使用CocoaPods集成ShareSDK

#import //微信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,見下圖

簡單的使用CocoaPods集成ShareSDK

簡單的使用CocoaPods集成ShareSDK

然後打開工程中的AppDelegate.m(代表你的工程名字)文件,導入QQSDK的頭文件並增加QQ的外部庫文件接口:

#import #import //騰訊開放平臺(對應QQ和QQ空間)SDK頭文件#import #import //初始化的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)。具體參考下圖:

簡單的使用CocoaPods集成ShareSDK

附:目前的新浪微博SDK需要在項目的Build Settings中的Other Linker Flags添加”-ObjC”,如果不配置有可能會崩潰。步驟如下:

簡單的使用CocoaPods集成ShareSDK

當新浪微博需要客戶端分享或者增加關注微博的功能,需要打開工程中的AppDelegate.m(代表你的工程名字)文件,導入新浪微博SDK的頭文件並增加新浪微博的外部庫文件接口:

#import #import //新浪微博SDK頭文件#import "WeiboSDK.h"//初始化的import參數注意要鏈接原生新浪微博SDK。case SSDKPlatformTypeSinaWeibo: [ShareSDKConnector connectWeibo:[WeiboSDK class]];break;


分享到:


相關文章: