比較常見的就是QQ颁股、微信及新浪微博的分享嫁审。
第一步:
集成shareSDK,我使用的是CocoaPods克锣,導(dǎo)入所需三方即可茵肃。
# 主模塊(必須)
*pod'ShareSDK3'*
*# Mob 公共庫(必須)*
*pod'MOBFoundation'*
*# UI模塊(非必須,需要用到ShareSDK提供的分享菜單欄和分享編輯頁面需要以下1行)*
*pod'ShareSDK3/ShareSDKUI'*
*# 平臺SDK模塊(對照一下平臺娶耍,需要的加上免姿。如果只需要QQ、微信榕酒、新浪微博胚膊,只需要以下3行)*
*pod'ShareSDK3/ShareSDKPlatforms/QQ'*
*pod'ShareSDK3/ShareSDKPlatforms/SinaWeibo'*
*pod'ShareSDK3/ShareSDKPlatforms/WeChat'*
第二步:
在 "AppDelegate.m" 文件中導(dǎo)入:
#import <ShareSDK/ShareSDK.h>
#import <ShareSDKConnector/ShareSDKConnector.h>
//騰訊開放平臺(對應(yīng)QQ和QQ空間)SDK頭文件
#import <TencentOpenAPI/TencentOAuth.h>
#import <TencentOpenAPI/QQApiInterface.h>
//微信SDK頭文件
#import"WXApi.h"
//新浪微博SDK頭文件
#import"WeiboSDK.h"
//新浪微博SDK需要在項(xiàng)目Build Settings中的Other Linker Flags添加"-ObjC"
第三步:
在 "AppDelegate.m" 文件中編寫代碼:
- (BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions {
// 此處應(yīng)填入微信的appId
[WXApiregisterApp:@"wx3be9e948cd67b3af"];
[self share ];
}
- (void)share{
/**
*設(shè)置ShareSDK的appKey,如果尚未在ShareSDK官網(wǎng)注冊過App想鹰,請移步到[http://mob.com/login](http://mob.com/login)登錄后臺進(jìn)行應(yīng)用注冊紊婉,
*在將生成的AppKey傳入到此方法中。
*方法中的第二個(gè)第三個(gè)參數(shù)為需要連接社交平臺SDK時(shí)觸發(fā)辑舷,
*在此事件中寫入連接代碼喻犁。第四個(gè)參數(shù)則為配置本地社交平臺時(shí)觸發(fā),根據(jù)返回的平臺類型來配置平臺信息何缓。
*如果您使用的時(shí)服務(wù)端托管平臺信息時(shí)肢础,第二、四項(xiàng)參數(shù)可以傳入nil碌廓,第三項(xiàng)參數(shù)則根據(jù)服務(wù)端托管平臺來決定要連接的社交SDK传轰。
*/
[ShareSDKregisterApp:@"1c12345678ff4"
activePlatforms:@[
@(SSDKPlatformTypeSinaWeibo),
@(SSDKPlatformTypeWechat),
@(SSDKPlatformTypeQQ),
]
onImport:^(SSDKPlatformTypeplatformType)
{
switch(platformType)
{
caseSSDKPlatformTypeWechat:
[ShareSDKConnectorconnectWeChat:[WXApiclass]];
break;
caseSSDKPlatformTypeQQ:
[ShareSDKConnectorconnectQQ:[QQApiInterfaceclass]tencentOAuthClass:[TencentOAuthclass]];
break;
caseSSDKPlatformTypeSinaWeibo:
[ShareSDKConnectorconnectWeibo:[WeiboSDKclass]];
break;
default:
break;
}
}
onConfiguration:^(SSDKPlatformTypeplatformType,NSMutableDictionary*appInfo)
{
switch(platformType)
{
caseSSDKPlatformTypeSinaWeibo:
//設(shè)置新浪微博應(yīng)用信息,其中authType設(shè)置為使用SSO+Web形式授權(quán)
[appInfoSSDKSetupSinaWeiboByAppKey:@"3239212345"
appSecret:@"cc88dbf7cf6678c12345c64e48d1b8a1"
redirectUri:@"http://www.weibo.com"
authType:SSDKAuthTypeBoth];
break;
caseSSDKPlatformTypeWechat:
[appInfoSSDKSetupWeChatByAppId:@"wx4fd1234e1bbbe871"
appSecret:@"12bf06ff7f31acb3456bb17f5f4bf76c"];
break;
caseSSDKPlatformTypeQQ:
[appInfoSSDKSetupQQByAppId:@"1106012345"
appKey:@"ZqwHCqweviEabc4"
authType:SSDKAuthTypeBoth];
break;
default:
break;
}
}];
}
第四步:
在 URL Types 添加相關(guān)參數(shù):
QQ +(QQ平臺的)AppKey的16進(jìn)制;
content + (QQ平臺的)AppKey的10進(jìn)制谷婆;
(微信平臺的)AppId慨蛙;
wb+(新浪微博的)AppKey;
第五步:
封裝分享方法纪挎,通過不同的平臺參數(shù)來分享到不同的平臺:
/**
*
QQ平臺(好友)(SSDKPlatformTypeQQ)
QQ空間(SSDKPlatformSubTypeQZone)
微信平臺(好友)(SSDKPlatformTypeWechat)
微信朋友圈(SSDKPlatformSubTypeWechatTimeline)
新浪微博(SSDKPlatformTypeSinaWeibo)
*/
+ (void)SharePlatform:(SSDKPlatformType)platformType urlStr:(NSString*)urlStr titleStr:(NSString*)titleStr picStr:(NSString*)picStr{
//1期贫、創(chuàng)建分享參數(shù)(必要)
NSMutableDictionary*shareParams = [NSMutableDictionarydictionary];
[shareParamsSSDKEnableUseClientShare];
NSArray* imageArray =@[[UIImageimageNamed:@"logo"]];
// 新浪微博的鏈接需寫到text參數(shù)里
NSString *shareText = @"";
if (platformType == SSDKPlatformTypeSinaWeibo) {
shareText = [NSString stringWithFormat:@"%@ %@",titleStr,urlStr];
}else{
shareText = nil;
}
[shareParamsSSDKSetupShareParamsByText:titleStr
images:imageArray
url:[NSURLURLWithString:urlStr]
title:@"標(biāo)題"
type:SSDKContentTypeAuto];
//進(jìn)行分享
[ShareSDKshare:platformType
parameters:shareParams
onStateChanged:^(SSDKResponseStatestate,NSDictionary*userData,SSDKContentEntity*contentEntity,NSError*error) {
switch(state) {
caseSSDKResponseStateSuccess:
{
// 分享成功
break;
}
caseSSDKResponseStateFail:
{
// 分享失敗
break;
}
caseSSDKResponseStateCancel:
{
// 分享已取消
break;
}
default:
break;
}
}];
}