將內(nèi)容分享到其他平臺是個非常普遍的功能智袭,今天和大家分享一下奔缠,如何用ShareSDK實現(xiàn)分享到微信好友、微信收藏吼野、微信朋友圈校哎、QQ、QQ空間箫锤、印象筆記以及復(fù)制的功能贬蛙。首先,我們需要去各個社交平臺申請對應(yīng)的APPKey,各個平臺的網(wǎng)址匯總可參考:
ShareSDK各社交平臺申請APPkey 的網(wǎ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',然后需要分享到哪個社交平臺就添加相應(yīng)的平臺帮寻。準(zhǔn)備工作做好后我們就可以進行實現(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對應(yīng)的密碼" redirectUri:@"https://api.weibo.com/oauth2/default.html" authType:SSDKAuthTypeBoth];
}
break;
case SSDKPlatformTypeWechat: {
[appInfo SSDKSetupWeChatByAppId:@"微信appkey" appSecret:@"微信apply對應(yīng)的密碼"];
}
break;
case SSDKPlatformTypeQQ: {
[appInfo SSDKSetupQQByAppId:@"QQappID" appKey:@"QQappkey" authType:SSDKAuthTypeSSO];
}
break;
case SSDKPlatformTypeYinXiang:
[appInfo SSDKSetupEvernoteByConsumerKey:@"印象筆記appkey" consumerSecret:@"印象筆記appkey對應(yīng)的密碼" sandbox:NO];
break;
default:
break;
}
}];
return YES;
}
// 這個方法是用于從微信返回第三方App
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url {
[WXApi handleOpenURL:url delegate:self];
return YES;
}
這里需要注意的一個地方是印象筆記分享中的sandbox參數(shù)浅蚪,如果在測試階段,把參數(shù)設(shè)成YES烫罩,也就是使用沙箱環(huán)境惜傲;如果項目要上傳AppStore了將其改成NO.在沙箱環(huán)境下,分享成功后在印象筆記的客戶端是看不到已經(jīng)分享的內(nèi)容的贝攒,需要到印象筆記的沙箱環(huán)境(https://sandbox.evernote.com)中查看分享的內(nèi)容盗誊,只有sandbox參數(shù)為NO的時候分享成功的內(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è)置各個平臺的URL Types
5、添加-ObjC支持串结。如圖
6哑子、在需要分享的地方舅列,實現(xiàn)分享方法。
NSMutableDictionary *shareParams = [NSMutableDictionary dictionary];
分享的參數(shù)有:shareContent(分享的內(nèi)容)卧蜓、shareLink(分享的鏈接)帐要、shareTitle(分享的標(biāo)題)、netImageUrl(網(wǎng)絡(luò)圖片弥奸,給一個URL地址即可榨惠,若要分享本地圖片,采用[UIImage imageNamed:@""])
1.//如果所有平臺要分享的內(nèi)容一致盛霎,可直接采用
[shareParams SSDKSetupShareParamsByText:[NSString stringWithFormat:@"%@%@", shareContent, [NSURL URLWithString:shareLink]]
images:netImageUrl
url:[NSURL URLWithString:shareLink]
title:shareTitle
type:SSDKContentTypeAuto];
2赠橙、如果想自定義各個平臺的分享內(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)容里期揪,單獨放在對應(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中的平臺的順序可以調(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ù)制功能不用顯示分享的編輯界面,所以采用直接分享的方式诞仓;若其他平臺也不需要出現(xiàn)分享編輯的界面的話也可以直接采用此方法將對應(yīng)平臺加上缤苫。
最后效果如圖下,點擊相應(yīng)的平臺則可進行分享
shareSDK現(xiàn)在已經(jīng)更新到3.5.2了墅拭,今天更新最新的sdk做分享的時候發(fā)現(xiàn)崩潰了活玲,崩潰信息是
+[ShareSDK isClientInstalled:]: unrecognized selector sent to class 0x10fe2eb50
后面發(fā)現(xiàn),新的sdk在用pod管理的時候需要添加
pod 'ShareSDK3/ShareSDKExtension'
這樣點擊分享按鈕進行分享的時候就不會崩潰了