這是我第一次寫博客 ,寫的不好請見諒.
最近在項目中涉及到新浪微博,qq分享,還有微信和朋友圈分享,于是就采用了shareSDK的框架來實現.下面來分享下步驟
1.先去mob官網注冊賬號,然后注冊你的應用,步驟官網都有,就不啰嗦了.
2.下載集成sdk 根據需求來下載,有些不必要的就不必下載了,屬于冗余的.然后倒入相關的依賴庫
具體可以參考 ?官方demo下載地址
3.設置 AppDelegate.m 文件相關頭文件 并且在在didFinishLaunchingWithOptions方法中加入如下代碼。
/**
*? 設置ShareSDK的appKey,如果尚未在ShareSDK官網注冊過App,請移步到http://mob.com/login 登錄后臺進行應用注冊
*? 在將生成的AppKey傳入到此方法中。
*? 方法中的第二個第三個參數為需要連接社交平臺SDK時觸發(fā),
*? 在此事件中寫入連接代碼。第四個參數則為配置本地社交平臺時觸發(fā),根據返回的平臺類型來配置平臺信息。
*? 如果您使用的時服務端托管平臺信息時,第二、四項參數可以傳入nil,第三項參數則根據服務端托管平臺來決定要連接的社交SDK。
*/
// 這里的iosv1101要替換成你在ShareSDK官網注冊時得到的AppKey
[ShareSDK registerApp:@"iosv1101"
// 這個數組裝的都是分享時會展示出來的應用,可以根據自己需要來進行刪減,比如 GooglePlus 我一般就刪掉了
activePlatforms:@[
@(SSDKPlatformTypeSinaWeibo),
@(SSDKPlatformTypeMail),
@(SSDKPlatformTypeSMS),
@(SSDKPlatformTypeCopy),
@(SSDKPlatformTypeWechat),
@(SSDKPlatformTypeQQ),
@(SSDKPlatformTypeRenren),
@(SSDKPlatformTypeGooglePlus)]
onImport:^(SSDKPlatformType platformType)
{
// 改動以下代碼要注意,一個case對應一個break
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)
{
// 以下的AppKey和appSecret都是 ShareSDK 官方程序帶的,測試的時候可以用用,建議用自己去申請的。QQ、微信、微博授權都得分別去對應的開發(fā)者平臺去申請
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;
// 如果上面代碼已經把 GooglePlus 排除掉了,那么這里也要刪除,不過不刪除也沒什么
case SSDKPlatformTypeGooglePlus:
[appInfo SSDKSetupGooglePlusByClientID:@"232554794995.apps.googleusercontent.com"
clientSecret:@"PEdFgtrMw97aCvf0joQj7EMk"
redirectUri:@"http://localhost"];
break;
default:
break;
}
}];
4.出發(fā)分享調用方法
//1、創(chuàng)建分享參數
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版中此參數作為彈出菜單的參照視圖,只有傳這個才可以彈出我們的分享菜單,可以傳分享的按鈕對象或者自己創(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;
}
}
];}
總結一下遇到的坑:
1. 導入相關依賴庫漏掉了,都怪自己太粗心,shareSDK一直在進步,新的方法和之前有所改進如果是用的老版本的,請更新到最新的版本.
2.關于騰訊qq的集成 在騰訊開放平臺獲取到的appid 是十進制的, URL Schemes要換算成十六進制的要大寫
解釋幾點:URL Schemes里wx開頭的那串,代表微信。tencent 100371282是對應的是QQ空間,QQ05FB8B52對應QQ好友分享。100371282是你在騰訊申請的APP ID,05FB8B52是AppID的16進制(如果AppID轉換的16進制數不夠8位則在前面補0,如轉換的是:5FB8B52,則最終填入為:QQ05FB8B52 注意:轉換后的字母要大寫)
3.關于新浪微博的分享這個是比較坑的,首先要配置回調地址,新浪微博分享有2種方式,
若使用sso授權的,在新浪開發(fā)平臺注冊應用的時候那個安全域是必須配置的,比方說配置@"www.baidu.com",不然那會一直報錯"appkey not bind domia",而且在分享的參數配置的時候也必須在那個text里面帶有這個安全域,不然分享會失敗.這個sso授權的機制比較坑,只要授權一次了,以后都不要登陸,直接就給發(fā)送一條微博,都不能編輯內容.另外如果要分享網絡圖片要申請高級權限
如果是采用客戶端 那個安全域名是可以不配置,也不必帶在那個text參數的,這種情況會跳到新浪微博的編輯頁面輸入一些說明文字或者表情.
(ps:再次吐槽一下新浪微博開放平臺,賬號密碼輸入錯誤了沒有提示,一直停留在那里,搞得在下以來懵逼,給新浪反饋了,說問題還在解決中,我也是醉了)