1. 准备工作
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:
//设置新浪微博应用信息,其中authType设置为使用SSO+Web形式授权
[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,方便塞数据。
@interface ShareSDKObject : NSObject
@property (nonatomic, strong) NSString *shareUrl;
@property (nonatomic, strong) NSString *shareContent;
@property (nonatomic, strong) NSString *shareTitle;
@property (nonatomic, strong) NSArray *imageArray; //数组对象可以为UIImage 或 NSString(图片地址)
@property (nonatomic, assign) SSDKContentType contentType;
+ (instancetype)shareObjectWithShareUrl:(NSString *)shareUrl shareContent:(NSString *)shareContent shareTitle:(NSString *)shareTitle imageArray:(NSArray *)imageArray;
- (NSMutableDictionary *)getShareParams;
@end
然后用自定义UI点击某个平台后到这里分享,注意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;
}
}];
}];
}
}
接着是实现没有文字编辑框的一键分享,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;
}
}];
}
以上就快速开发出分享功能,是不是很简单