由于當(dāng)初沒有用markdown編寫特姐,文章有點(diǎn)亂巷查,現(xiàn)在重新編寫了一下歌馍,如果有需要握巢,可以看這個(gè)?使用ShareSDK實(shí)現(xiàn)分享功能
將內(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见转、在- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions;中初始化ShareSDK
[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;
}
}];`
這里需要注意的一個(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的方式打開房匆,如圖
然后加入如下圖所示的代碼
或者也可以在plist文件中用key type value的方式添加耸成,如下圖
3.將bit 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) {
[MBProgressHUD showToastToView:[UIApplication sharedApplication].keyWindow.rootViewController.view withText:@"復(fù)制成功"];
}else{ ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?[MBProgressHUD showToastToView:[UIApplication sharedApplication].keyWindow.rootViewController.view withText:@"分享成功"];
}
break;
case ?SSDKResponseStateFail:
if (platformType == SSDKPlatformTypeCopy) {
[MBProgressHUD showToastToView:[UIApplication sharedApplication].keyWindow.rootViewController.view withText:@"復(fù)制失敗"];
}else{
[MBProgressHUD showToastToView:[UIApplication sharedApplication].keyWindow.rootViewController.view withText:@"分享失敗"];
}
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)行分享
以上就是簡(jiǎn)單的分享功能的實(shí)現(xiàn)危彩,如果有問題或者建議都可以留言攒磨,愿與大家共同進(jìn)步泳桦。