成员
shareSDK:外部分享调用的接口
shareServiceManager:内部底层逻辑的实现者
注册
注册三方分享的AppKey。
Appdelegate方法中配置分享key
在app delegate
-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{
//注册微博:
[WeiboSDK registerApp:APPKEY];
//注册微信:
[WXApi registerApp:APPKEY];
//注册QQ:
TencentOAuth *oAuth = [[TencentOAuth alloc] initWithAppId:[keydic objectForKey:SHARE_KEY] andDelegate:self];
oAuth.accessToken = [userDefaults objectForKey:@"TencentAccessToken"];
oAuth.expirationDate = [userDefaults objectForKey:@"TencentExpireDate"];
oAuth.openId = [userDefaults objectForKey:@"TencentOpenID"];
}
中先注册各个分享渠道的key
分享需要用到的参数
请注意各个分享渠道的SDK的参数说明,例如:微博title不能超过140个汉字(280个字节),微信缩略图大小最大为32k......,否则会导致无法跳转对应App或者分享失败
所需要的数据有:
1)微信微博等的分享标题和分享文案
2)分享用到的标题旁边的缩略图(微信需要压缩到32k以下,QQ控件300k以下)
3)分享用到的原图图片和使用UIImageJPEGRepresentation(UIImage,CGFloat)压缩之后的NSData
4)分享渠道拼参,NSDictionary @{@“shareChannelTitle”:@“新浪微博”,@“SHARETYE”:@“ShareTypeSinaWeibo”};
拼接参数,调用对外接口shareSDK,弹出分享浮层
- (void)showShareViewWithShareDict:(NSDictionary *)shareDic{
//分享标题,内容,链接,图片
NSString *shareTitle = [shareDic objectForKey:@"shareTitle"];
NSString *shareContent = [shareDic objectForKey:@"shareContent"];
NSString *shareURL = [shareDic objectForKey:@"shareURL"];
UIImage *shareIMG = [shareDic objectForKey:@"shareIMG"];
if (!shareIMG||shareIMG == nil) {
shareIMG = [UIImage imageNamed:@"分享默认图"];
}
if (shareURL.length == 0) {
shareURL = self.URLString;
}
//分享的具体内容model
SouFunShareDataModel *dataModel = [[SouFunShareDataModel alloc]init];
dataModel.onlyText = NO;
dataModel.shareResource = @"热门活动";
//分享的链接地址
dataModel.webURL = shareURL;
//微信(QQ)
dataModel.weChatTitle = shareTitle;
dataModel.weChatDescription = shareContent;
//微信朋友圈
dataModel.weChatFriendTitle = shareContent;
//QQ空间
dataModel.QZoneTitle = shareTitle;
dataModel.QzoneDescription = shareContent;
//腾讯(新浪)微博
dataModel.blogDescription = [NSString stringWithFormat:@"%@%@", shareContent, shareURL];//长度小于280字节
//分享的图片
dataModel.image = shareIMG;
dataModel.orignalImageData = UIImageJPEGRepresentation(dataModel.image, 1.0f); //原图
//压缩图片
dataModel.thumbImage = [self compressImage:shareIMG]; //需要压缩到32K后的
dataModel.imageData = UIImageJPEGRepresentation(dataModel.thumbImage, 1.0f); //需要压缩后的,具体大小不知道
// 短信分享内容
dataModel.SMSDescription = [NSString stringWithFormat:@"%@ %@", shareContent, shareURL];
//分享渠道UI数据拼参
NSDictionary *sinaBlog = [[NSDictionary alloc] initWithObjectsAndKeys:@"新浪微博",SHARE_ICON_TILTE,ShareTypeSinaWeibo,SHARE_TYPE,nil];
NSDictionary *weiChat = [[NSDictionary alloc] initWithObjectsAndKeys:@"微信",SHARE_ICON_TILTE,ShareTypeWeixiSession,SHARE_TYPE,nil];
NSDictionary *message = [[NSDictionary alloc] initWithObjectsAndKeys:@"短信",SHARE_ICON_TILTE,ShareTypeSMS,SHARE_TYPE ,nil];
NSDictionary *urlCopy = [[NSDictionary alloc] initWithObjectsAndKeys:@"复制链接",SHARE_ICON_TILTE,ShareTypeCopy,SHARE_TYPE, nil];
NSDictionary *QQZone = [[NSDictionary alloc] initWithObjectsAndKeys:@"QQ空间",SHARE_ICON_TILTE,ShareTypeQQSpace,SHARE_TYPE, nil];
NSDictionary *weiChatFriendGroup =[[NSDictionary alloc] initWithObjectsAndKeys:@"朋友圈",SHARE_ICON_TILTE,ShareTypeWeixiTimeline,SHARE_TYPE, nil];
//添加分享渠道到数组中
NSMutableArray *shareChannelArr =[[NSMutableArray alloc] initWithObjects:sinaBlog,weiChat,weiChatFriendGroup,message,urlCopy,QQZone, nil];
SouFunShareSDK *share=[SouFunShareSDK shareInstance];
share.ShareChannels = shareChannelArr;
share.cellSize = CGSizeMake(54, 54);
//弹出分享浮层
[share showShareViewWithShareDataModel:dataModel inViewInViewController:self];
}
点击某一个分享渠道,以微信为例详解底层逻辑:
点击微信后,相应代理方法,先判断是否安装了微信,未安装则提示未安装微信;如果安装了微信,将分享的参数传递给微信SDK
-(void)gotoViewControllerWithMark:(NSString *)shareType
}
if([shareType isEqualToString:ShareTypeWeixiSession] )
{
if(![WXApi isWXAppInstalled])
{
[ShareUIToastView showToastViewWithContent:@"您未安装微信,请先下载安装" andRect:KTOASTRECT andTime:3.0 andObject:_parentController];
return;
}
[self weChatShare:_shareDataModel];
}
else if([shareType isEqualToString:ShareTypeWeixiTimeline])
{
if(![WXApi isWXAppInstalled])
{
[ShareUIToastView showToastViewWithContent:@"您未安装微信,请先下载安装" andRect:KTOASTRECT andTime:3.0 andObject:_parentController];
return;
}
[self weChatFriendShare:_shareDataModel];
}else if ([UtilitiesReachability isAblityForNetwork]==NO){
[ShareUIToastView showToastViewWithContent:@"网络连接失败,请稍后再试" andRect:KTOASTRECT andTime:2.0 andObject:_parentController];
}
}
//微信好友分享
-(void)weChatShare:(SouFunShareDataModel *)shareDataModel
{
_shareDataModel.currentShareType = @"WXSceneSession";
if(shareDataModel.onlyText)
{
SendMessageToWXReq* req = [[SendMessageToWXReq alloc] init];
req.bText = YES;
req.text = shareDataModel.weChatDescription;
req.scene = WXSceneSession;
[WXApi sendReq:req];
}
else
{
WXMediaMessage *message = [WXMediaMessage message];
message.title = shareDataModel.weChatTitle;
message.description = shareDataModel.weChatDescription;
[message setThumbImage:shareDataModel.thumbImage];
WXWebpageObject *ext = [WXWebpageObject object];
ext.webpageUrl = shareDataModel.webURL;
message.mediaObject = ext;
SendMessageToWXReq* req = [[SendMessageToWXReq alloc] init];
req.bText = NO;
req.message = message;
req.scene = WXSceneSession;
[WXApi sendReq:req];
}
}
//微信朋友圈分享
-(void)weChatFriendShare:(SouFunShareDataModel *)shareDataModel
{
_shareDataModel.currentShareType = @"WXSceneTimeLine";
if(shareDataModel.onlyText)
{
SendMessageToWXReq* req = [[SendMessageToWXReq alloc] init];
req.bText = YES;
req.text = shareDataModel.weChaFriendtDescription;
req.scene = WXSceneTimeline;
[WXApi sendReq:req];
}
else
{
WXMediaMessage *message = [WXMediaMessage message];
message.title = shareDataModel.weChatFriendTitle;
message.description = shareDataModel.weChaFriendtDescription;
[message setThumbImage:shareDataModel.thumbImage];
WXWebpageObject *ext = [WXWebpageObject object];
ext.webpageUrl = shareDataModel.webURL;
message.mediaObject = ext;
SendMessageToWXReq* req = [[SendMessageToWXReq alloc] init];
req.bText = NO;
req.message = message;
req.scene = WXSceneTimeline;
[WXApi sendReq:req];
}
}
微信API发起跳转请求后:[WXApi sendReq:req],得在appdelegate方法中通过WXApi 处理跳转
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
//delegate用于处理分享结果的回调
return [WXApi handleOpenURL:url delegate:self];
}
在代理方法实现分享结果逻辑处理
-(void) onResp:(id)resp
{
//微信好友,朋友圈分享
if([resp isKindOfClass:[SendMessageToWXResp class]])
{
NSString *resultMsg = nil;
int respondStatus = [resp errCode];
if(respondStatus == 0)
{
resultMsg = @"分享成功";
}
else
{
resultMsg = @"分享失败,可以再试试~";
}
//提示分享结果
[ShareUIToastView showToastViewWithContent:resultMsg andRect:KTOASTRECT andTime:3.0 andObject:_parentController];
}
//QQ好友,QQ空间分享
else if([resp isKindOfClass:[SendMessageToQQResp class]])
{
NSString *resultMsg = nil;
int respondStatus = [[resp result] intValue];
if(respondStatus == 0)
{
resultMsg = @"分享成功";
}
else
{
resultMsg = @"分享失败,可以再试试~";
}
[ShareUIToastView showToastViewWithContent:resultMsg andRect:KTOASTRECT andTime:3.0 andObject:_parentController];
}
}
至此,整个分享流程就结束了,还需要注意的是iOS9需要将你要在外部调用的URL scheme列为白名单,才可以完成跳转跳转第三方应用,否则会报错
canOpenURL: failed **for** URL : "mqzone://qqapp" - error: "This app is not allowed to query for scheme mqzone"