將內(nèi)容分享到其他平臺(tái)是個(gè)非常普遍的功能,今天和大家分享一下,如何用ShareSDK實(shí)現(xiàn)分享到微信好友、微信收藏、微信朋友圈、QQ、QQ空間、印象筆記以及復(fù)制的功能。首先,我們需要去各個(gè)社交平臺(tái)申請(qǐng)對(duì)應(yīng)的APPKey,各個(gè)平臺(tái)的網(wǎng)址匯總可參考:
ShareSDK各社交平臺(tái)申請(qǐng)APPkey 的網(wǎng)址及申請(qǐng)流程匯總
。之后,我們導(dǎo)入ShareSDK的庫。
用pod導(dǎo)入。
在Pod file中添加
pod 'ShareSDK3'
pod 'MOBFoundation'pod 'ShareSDK3/ShareSDKUI'
pod 'ShareSDK3/ShareSDKPlatforms/QQ'
pod 'ShareSDK3/ShareSDKPlatforms/SinaWeibo'
pod 'ShareSDK3/ShareSDKPlatforms/WeChat'
其中pod 'ShareSDK3' pod 'MOBFoundation'是必須的,其他的根據(jù)需求相應(yīng)的添加,比如,如果你需要使用ShareSDK的UI,那么你就需要導(dǎo)入pod 'ShareSDK3/ShareSDKUI',然后需要分享到哪個(gè)社交平臺(tái)就添加相應(yīng)的平臺(tái)。準(zhǔn)備工作做好后我們就可以進(jìn)行實(shí)現(xiàn)了。
1、在AppDelegate中導(dǎo)入頭文件
#import<ShareSDK/ShareSDK.h>
#import<WXApi.h>
#import<ShareSDKConnector/ShareSDKConnector.h>
#import<TencentOpenAPI/QQApiInterface.h>
#import<TencentOpenAPI/TencentOAuth.h>
#import<WeiboSDK.h>
2、 在Appdelegate中初始化ShareSDK
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[ShareSDK registerApp:@"AppKey" activePlatforms:@[@(SSDKPlatformSubTypeWechatSession),@(SSDKPlatformSubTypeWechatTimeline),@(SSDKPlatformSubTypeWechatFav),@(SSDKPlatformTypeSinaWeibo), @(SSDKPlatformTypeQQ),@(SSDKPlatformTypeYinXiang),@(SSDKPlatformSubTypeQZone), @(SSDKPlatformTypeCopy)] onImport:^(SSDKPlatformType platformType) {
switch (platformType) {
case SSDKPlatformTypeWechat: {
[ShareSDKConnector connectWeChat:[WXApi class]];
}
break;
case SSDKPlatformTypeQQ: {
[ShareSDKConnector connectQQ:[QQApiInterface class] tencentOAuthClass:[TencentOAuth class]];
}
break;
default:
break;
}
} onConfiguration:^(SSDKPlatformType platformType, NSMutableDictionary *appInfo) {
switch (platformType) {
case SSDKPlatformTypeSinaWeibo: {
[appInfo SSDKSetupSinaWeiboByAppKey:@"sinaAppKey" appSecret:@"apply對(duì)應(yīng)的密碼" redirectUri:@"https://api.weibo.com/oauth2/default.html" authType:SSDKAuthTypeBoth];
}
break;
case SSDKPlatformTypeWechat: {
[appInfo SSDKSetupWeChatByAppId:@"微信appkey" appSecret:@"微信apply對(duì)應(yīng)的密碼"];
}
break;
case SSDKPlatformTypeQQ: {
[appInfo SSDKSetupQQByAppId:@"QQappID" appKey:@"QQappkey" authType:SSDKAuthTypeSSO];
}
break;
case SSDKPlatformTypeYinXiang:
[appInfo SSDKSetupEvernoteByConsumerKey:@"印象筆記appkey" consumerSecret:@"印象筆記appkey對(duì)應(yīng)的密碼" sandbox:NO];
break;
default:
break;
}
}];
return YES;
}
// 這個(gè)方法是用于從微信返回第三方App
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url {
[WXApi handleOpenURL:url delegate:self];
return YES;
}
這里需要注意的一個(gè)地方是印象筆記分享中的sandbox參數(shù),如果在測(cè)試階段,把參數(shù)設(shè)成YES,也就是使用沙箱環(huán)境;如果項(xiàng)目要上傳AppStore了將其改成NO.在沙箱環(huán)境下,分享成功后在印象筆記的客戶端是看不到已經(jīng)分享的內(nèi)容的,需要到印象筆記的沙箱環(huán)境(https://sandbox.evernote.com)中查看分享的內(nèi)容,只有sandbox參數(shù)為NO的時(shí)候分享成功的內(nèi)容才可直接在印象筆記客戶端中查看。
2.添加跳轉(zhuǎn)白名單。
右擊plist文件,用source code的方式打開,如圖

然后加入如下圖所示的代碼
<key>LSApplicationQueriesSchemes</key>
<array>
<string>wechat</string>
<string>mqqapi</string>
<string>sinaweibo</string>
<string>sinaweibohd</string>
<string>sinaweibosso</string>
<string>sinaweibohdsso</string>
<string>TencentWeibo</string>
<string>wtloginmqq2</string>
<string>mqqopensdkapiV3</string>
<string>mqqopensdkapiV2</string>
<string>mqqOpensdkSSoLogin</string>
<string>mqq</string>
<string>weixin</string>
<string>alipay</string>
<string>alipayshare</string>
</array>
或者也可以在plist文件中用key type value的方式添加,如下圖

3.將bit
![Uploading 4_740074.png . . .]code關(guān)掉

4、設(shè)置各個(gè)平臺(tái)的URL Types



5、添加-ObjC支持。如圖

6、在需要分享的地方,實(shí)現(xiàn)分享方法。
NSMutableDictionary *shareParams = [NSMutableDictionary dictionary];
分享的參數(shù)有:shareContent(分享的內(nèi)容)、shareLink(分享的鏈接)、shareTitle(分享的標(biāo)題)、netImageUrl(網(wǎng)絡(luò)圖片,給一個(gè)URL地址即可,若要分享本地圖片,采用[UIImage imageNamed:@""])
1.//如果所有平臺(tái)要分享的內(nèi)容一致,可直接采用
[shareParams SSDKSetupShareParamsByText:[NSString stringWithFormat:@"%@%@", shareContent, [NSURL URLWithString:shareLink]]
images:netImageUrl
url:[NSURL URLWithString:shareLink]
title:shareTitle
type:SSDKContentTypeAuto];
2、如果想自定義各個(gè)平臺(tái)的分享內(nèi)容,則用其相應(yīng)的API
// 微信朋友圈
[shareParams SSDKSetupWeChatParamsByText:[NSString stringWithFormat:@"%@%@", shareContent, [NSURL URLWithString:shareLink]] title:shareTitle url:[NSURL URLWithString:shareLink] thumbImage:nil image:[UIImage imageNamed:@"60x60@2x"] musicFileURL:nil extInfo:nil fileData:nil emoticonData:nil type:SSDKContentTypeAuto forPlatformSubType:SSDKPlatformSubTypeWechatSession];
// 微信收藏
[shareParams SSDKSetupWeChatParamsByText:[NSString stringWithFormat:@"%@", shareContent] title:shareTitle url:nil thumbImage:nil image:[UIImage imageNamed:@"60x60@2x"] musicFileURL:nil extInfo:nil fileData:nil emoticonData:nil type:SSDKContentTypeAuto forPlatformSubType:SSDKPlatformSubTypeWechatFav];
// 微信好友
[shareParams SSDKSetupWeChatParamsByText:[NSString stringWithFormat:@"%@", shareContent] title:shareTitle url:[NSURL URLWithString:shareLink] thumbImage:nil image:[UIImage imageNamed:@"60x60@2x"] musicFileURL:nil extInfo:nil fileData:nil emoticonData:nil type:SSDKContentTypeAuto forPlatformSubType:SSDKPlatformSubTypeWechatTimeline];
// 新浪微博,分享到微博的標(biāo)題及URL需拼接到內(nèi)容里,單獨(dú)放在對(duì)應(yīng)的參數(shù)里不好使。
[shareParams SSDKSetupSinaWeiboShareParamsByText:[NSString stringWithFormat:@"【%@】%@%@",shareTitle, shareContent, [NSURL URLWithString:shareLink]] title:shareTitle image:netImageUrl url:[NSURL URLWithString:shareLink] latitude:0 longitude:0 objectID:nil type:SSDKContentTypeAuto];
// QQ好友
[shareParams SSDKSetupQQParamsByText:[NSString stringWithFormat:@"%@", shareTitle] title:nil url:[NSURL URLWithString:shareLink] thumbImage:nil image:netImageUrl type:SSDKContentTypeAuto forPlatformSubType:SSDKPlatformTypeQQ];
// QQ空間
[shareParams SSDKSetupQQParamsByText:[NSString stringWithFormat:@"%@", shareTitle] title:nil url:[NSURL URLWithString:shareLink] thumbImage:nil image:netImageUrl type:SSDKContentTypeAuto forPlatformSubType:SSDKPlatformSubTypeQZone];
// 印象筆記
[shareParams SSDKSetupEvernoteParamsByText:[NSString stringWithFormat:@"【%@】%@ %@",shareTitle ,shareContent,shareLink] images:netImageUrl
title:shareTitle notebook:nil tags:nil platformType:SSDKPlatformTypeYinXiang];
// 復(fù)制
[shareParams SSDKSetupCopyParamsByText:nil images:nil url:[NSURL URLWithString:shareLink] type:SSDKContentTypeAuto];
//分享界面,items中的平臺(tái)的順序可以調(diào)整
SSUIShareActionSheetController *sheet = [ShareSDK showShareActionSheet:nil
items:@[@(SSDKPlatformSubTypeWechatSession),
@(SSDKPlatformSubTypeWechatTimeline),
@(SSDKPlatformSubTypeWechatFav),
@(SSDKPlatformTypeSinaWeibo),
@(SSDKPlatformTypeQQ),
@(SSDKPlatformSubTypeQZone),
@(SSDKPlatformTypeYinXiang),
@(SSDKPlatformTypeCopy)]
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(@"復(fù)制成功");
}else{
NSLog(@"分享成功");
}
break;
case SSDKResponseStateFail:
if (platformType == SSDKPlatformTypeCopy) {
NSLog(@"復(fù)制失敗");
}else{
NSLog(@"分享失敗");
}
NSLog(@"失?。?@", error);
break;
default:
break;
}
}];
[sheet.directSharePlatforms addObject:@(SSDKPlatformTypeCopy)];//復(fù)制功能不用顯示分享的編輯界面,所以采用直接分享的方式;若其他平臺(tái)也不需要出現(xiàn)分享編輯的界面的話也可以直接采用此方法將對(duì)應(yīng)平臺(tái)加上。
最后效果如圖下,點(diǎn)擊相應(yīng)的平臺(tái)則可進(jìn)行分享

shareSDK現(xiàn)在已經(jīng)更新到3.5.2了,今天更新最新的sdk做分享的時(shí)候發(fā)現(xiàn)崩潰了,崩潰信息是
+[ShareSDK isClientInstalled:]: unrecognized selector sent to class 0x10fe2eb50
后面發(fā)現(xiàn),新的sdk在用pod管理的時(shí)候需要添加
pod 'ShareSDK3/ShareSDKExtension'
這樣點(diǎn)擊分享按鈕進(jìn)行分享的時(shí)候就不會(huì)崩潰了