1. 準(zhǔn)備工作
1.去各大平臺注冊appId侣监,同時記得在工程里配置URL types渣触。
2.下載ShareSDK赚哗,順便要把它的適配文檔看一看哦浇雹。
2.搭建工程
1.APP啟動時注冊ShareSDK
[ShareSDK registerApp:kShareSDKKey
activePlatforms:@[
@(SSDKPlatformTypeSinaWeibo),
@(SSDKPlatformTypeWechat),
@(SSDKPlatformTypeQQ)
]
onImport:^(SSDKPlatformType platformType) {
switch (platformType)
{
case SSDKPlatformTypeWechat:
[ShareSDKConnector connectWeChat:[WXApi class] delegate:self];
break;
case SSDKPlatformTypeQQ:
[ShareSDKConnector connectQQ:[QQApiInterface class]
tencentOAuthClass:[TencentOAuth class]];
break;
case SSDKPlatformTypeSinaWeibo:
[ShareSDKConnector connectWeibo:[WeiboSDK class]];
break;
default:
break;
}
}
onConfiguration:^(SSDKPlatformType platformType, NSMutableDictionary *appInfo) {
switch (platformType)
{
case SSDKPlatformTypeSinaWeibo:
//設(shè)置新浪微博應(yīng)用信息,其中authType設(shè)置為使用SSO+Web形式授權(quán)
[appInfo SSDKSetupSinaWeiboByAppKey:kSinaAppKey
appSecret:kSinaAppSecret
redirectUri:kSinaAppRedirectUrl
authType:SSDKAuthTypeBoth];
break;
case SSDKPlatformTypeWechat:
[appInfo SSDKSetupWeChatByAppId:kWexinAppKey
appSecret:kWexinAppSecret];
break;
case SSDKPlatformTypeQQ:
[appInfo SSDKSetupQQByAppId:kQQAppKey
appKey:kQQAppSecret
authType:SSDKAuthTypeBoth];
break;
default:
break;
}
}];
2.自定義UI下分享
先自定義一個shareModel沉御,方便塞數(shù)據(jù)。
@interface ShareSDKObject : NSObject
@property (nonatomic, strong) NSString *shareUrl;
@property (nonatomic, strong) NSString *shareContent;
@property (nonatomic, strong) NSString *shareTitle;
@property (nonatomic, strong) NSArray *imageArray; //數(shù)組對象可以為UIImage 或 NSString(圖片地址)
@property (nonatomic, assign) SSDKContentType contentType;
+ (instancetype)shareObjectWithShareUrl:(NSString *)shareUrl shareContent:(NSString *)shareContent shareTitle:(NSString *)shareTitle imageArray:(NSArray *)imageArray;
- (NSMutableDictionary *)getShareParams;
@end
然后用自定義UI點(diǎn)擊某個平臺后到這里分享昭灵,注意showShareEditor是有文字編輯框的吠裆,此方法在<ShareSDKUI/ShareSDKUI.h>這個頭文件中,若需要一鍵分享看下一段代碼烂完。
- (void)shareWithObject:(ShareSDKObject *)shareObject
shareSuccess:(void(^)(SSDKPlatformType platform))success
shareFailed:(void(^)(NSError *error))failed
shareCancel:(void(^)(void))cancel {
if (shareObject) {
ShareView *shareView = [[ShareView alloc] init];
[shareView performSelector:@selector(showWithAnimationInView:) withObject:nil afterDelay:0.1];
[shareView setShareButtonClickBlock:^(SSDKPlatformType shareType) {
if (shareType == SSDKPlatformTypeSinaWeibo) {
shareObject.shareContent = [shareObject.shareContent stringByAppendingString:shareObject.shareUrl];
}
[ShareSDK showShareEditor:shareType otherPlatformTypes:nil shareParams:[shareObject getShareParams] onShareStateChanged:^(SSDKResponseState state, SSDKPlatformType platformType, NSDictionary *userData, SSDKContentEntity *contentEntity, NSError *error, BOOL end) {
switch (state) {
case SSDKResponseStateSuccess:
{
if (success) {
success(platformType);
}
break;
}
case SSDKResponseStateFail:
{
if (failed) {
failed(error);
}
break;
}
case SSDKResponseStateCancel:
{
if (cancel) {
cancel();
}
break;
}
default:
break;
}
}];
}];
}
}
接著是實(shí)現(xiàn)沒有文字編輯框的一鍵分享试疙,ShareSDK share的方法在ShareSDK.h中,用這個就可以直接跳平臺窜护。同時在<ShareSDKExtension/SSEShareHelper.h>中有一個名為oneKeyShare的方法效斑,然并卵的是用它時,它會報錯說不支持微信等平臺。
- (void)oneKeyShareWithType:(SSDKPlatformType) shareType
shareObject:(ShareSDKObject *)shareObject
shareSuccess:(void(^)(SSDKPlatformType platform))success
shareFailed:(void(^)(NSError *error))failed
shareCancel:(void(^)(void))cancel {
[ShareSDK share:shareType parameters:[shareObject getShareParams] onStateChanged:^(SSDKResponseState state, NSDictionary *userData, SSDKContentEntity *contentEntity, NSError *error) {
switch (state) {
case SSDKResponseStateSuccess:
{
if (success) {
success(shareType);
}
break;
}
case SSDKResponseStateFail:
{
if (failed) {
failed(error);
}
break;
}
case SSDKResponseStateCancel:
{
if (cancel) {
cancel();
}
break;
}
default:
break;
}
}];
}
以上就快速開發(fā)出分享功能缓屠,是不是很簡單