成員
shareSDK:外部分享調(diào)用的接口
shareServiceManager:內(nèi)部底層邏輯的實(shí)現(xiàn)者
注冊
注冊三方分享的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
分享需要用到的參數(shù)
請注意各個分享渠道的SDK的參數(shù)說明,例如:微博title不能超過140個漢字(280個字節(jié))跟磨,微信縮略圖大小最大為32k......,否則會導(dǎo)致無法跳轉(zhuǎn)對應(yīng)App或者分享失敗
所需要的數(shù)據(jù)有:
1)微信微博等的分享標(biāo)題和分享文案
2)分享用到的標(biāo)題旁邊的縮略圖(微信需要壓縮到32k以下宛裕,QQ控件300k以下)
3)分享用到的原圖圖片和使用UIImageJPEGRepresentation(UIImage,CGFloat)壓縮之后的NSData
4)分享渠道拼參威蕉,NSDictionary @{@“shareChannelTitle”:@“新浪微博”,@“SHARETYE”:@“ShareTypeSinaWeibo”};
拼接參數(shù)隅津,調(diào)用對外接口shareSDK,彈出分享浮層
- (void)showShareViewWithShareDict:(NSDictionary *)shareDic{
//分享標(biāo)題劲室,內(nèi)容伦仍,鏈接,圖片
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:@"分享默認(rèn)圖"];
}
if (shareURL.length == 0) {
shareURL = self.URLString;
}
//分享的具體內(nèi)容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字節(jié)
//分享的圖片
dataModel.image = shareIMG;
dataModel.orignalImageData = UIImageJPEGRepresentation(dataModel.image, 1.0f); //原圖
//壓縮圖片
dataModel.thumbImage = [self compressImage:shareIMG]; //需要壓縮到32K后的
dataModel.imageData = UIImageJPEGRepresentation(dataModel.thumbImage, 1.0f); //需要壓縮后的很洋,具體大小不知道
// 短信分享內(nèi)容
dataModel.SMSDescription = [NSString stringWithFormat:@"%@ %@", shareContent, shareURL];
//分享渠道UI數(shù)據(jù)拼參
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:@"復(fù)制鏈接",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];
//添加分享渠道到數(shù)組中
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];
}
點(diǎn)擊某一個分享渠道充蓝,以微信為例詳解底層邏輯:
點(diǎn)擊微信后,相應(yīng)代理方法喉磁,先判斷是否安裝了微信谓苟,未安裝則提示未安裝微信;如果安裝了微信,將分享的參數(shù)傳遞給微信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:@"網(wǎng)絡(luò)連接失敗,請稍后再試" 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發(fā)起跳轉(zhuǎn)請求后:[WXApi sendReq:req]斤讥,得在appdelegate方法中通過WXApi 處理跳轉(zhuǎn)
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
//delegate用于處理分享結(jié)果的回調(diào)
return [WXApi handleOpenURL:url delegate:self];
}
在代理方法實(shí)現(xiàn)分享結(jié)果邏輯處理
-(void) onResp:(id)resp
{
//微信好友纱皆,朋友圈分享
if([resp isKindOfClass:[SendMessageToWXResp class]])
{
NSString *resultMsg = nil;
int respondStatus = [resp errCode];
if(respondStatus == 0)
{
resultMsg = @"分享成功";
}
else
{
resultMsg = @"分享失敗湾趾,可以再試試~";
}
//提示分享結(jié)果
[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];
}
}
至此,整個分享流程就結(jié)束了铛楣,還需要注意的是iOS9需要將你要在外部調(diào)用的URL scheme列為白名單近迁,才可以完成跳轉(zhuǎn)跳轉(zhuǎn)第三方應(yīng)用,否則會報(bào)錯
canOpenURL: failed **for** URL : "mqzone://qqapp" - error: "This app is not allowed to query for scheme mqzone"