提要:(后面粘上代碼!!!如果就是按照我的方法實(shí)現(xiàn),直接復(fù)制代碼,稍作調(diào)整即可)
本文著重介紹:就本人使用代碼實(shí)現(xiàn)<友盟分享彈出的選擇界面的自定義方法、不同的按鈕實(shí)現(xiàn)相應(yīng)的第三方分享>
1、項(xiàng)目需求
1.1.1:
下面是我們項(xiàng)目的需求圖:(如果用原生的是一行四個(gè)按鈕)
1.1.2:
友盟注冊,以及SDK下載和導(dǎo)入項(xiàng)目不在說,直接進(jìn)入正題。(注意,6.0.1的SDK對類名做了替換,大家AppDelegate里面也要替換)
2、界面
2.1.1:
本人使用了九宮格布局,為了方便大家,我給大家提供一個(gè)鏈接地址:GitHub - GeekZP/ZPActionSheet: 簡單的兩種彈窗方式 一種適用于9宮格類型的分享,另一種適用于替換系統(tǒng)的actionsheet,這個(gè)就是直接上面的UI效果,很方便的。
3、項(xiàng)目代碼
3.1.1:
在你點(diǎn)擊分享按鈕里寫入一下代碼:
NSArray *titlearr = @[@"微信好友",@"微信朋友圈",@"QQ好友",@"QQ空間",@"新浪微博",@"復(fù)制鏈接"];
NSArray *imageArr = @[@"UMS_wechat_session_icon",@"UMS_wechat_timeline_icon",@"UMS_qq_icon",@"UMS_qzone_icon",@"UMS_sina_icon",@"fuzhilainjie"];
ActionSheetView *actionsheet = [[ActionSheetView alloc] initWithShareHeadOprationWith:titlearr andImageArry:imageArr andProTitle:@"測試" and:ShowTypeIsShareStyle];
說明:imageArr:圖片字典 ? ? ? ? ? titlearr:文字字典
上面的代碼就可以實(shí)現(xiàn)了上面的UI效果。
3.1.2
在剛才的代碼下面加入以下代碼:
[actionsheet setBtnClick:^(NSInteger btnTag) {
// 這里面可以加入分享到某個(gè)第三方的點(diǎn)擊
}
在上面的大括號里實(shí)現(xiàn)下面圖片里的判斷方法:
說明:“btnTag == 0” 就是點(diǎn)擊了九宮格第一個(gè)按鈕觸發(fā)的方法,以此類推
——————————————————————————————————3.1.3(6.0.1之前的版本)——————
——————————————————————————
在if判斷里面寫入下面的代碼實(shí)現(xiàn)分享:【功能實(shí)現(xiàn)后,可以自行提取一下】
注意:圖片的紅框框住的是對應(yīng)第三方平臺的,記得更改,微信,朋友圈,QQ,QQ空間,他們四個(gè)是一樣的,但是,但是,但是——————————————新浪微博————————————————是獨(dú)特的,它的實(shí)現(xiàn)方法是下面這樣的:
注意到了嗎,微博的標(biāo)題和鏈接必須是寫在一起的,就是我紅框框住的地方。
到此就可以實(shí)現(xiàn)分享功能了。
——————————————————————————————————3.1.3(6.0.1最新的版本)——————
————————————————————————
在if判斷里面寫入下面的代碼實(shí)現(xiàn)分享:【功能實(shí)現(xiàn)后,可以自行提取一下】
在這里我就以分享帶圖片、文字和鏈接為例-----------------------------
先創(chuàng)建分享信息對象,title:標(biāo)題; ? descr:詳情; ? image:圖片鏈接; ??
webpageUrl:網(wǎng)址鏈接; ? ?
接下來是點(diǎn)擊按鈕后,用到的方法:
注意:圖片的紅框框住的是對應(yīng)第三方平臺的,記得更改,微信,朋友圈,QQ,QQ空間,他們四個(gè)是一樣的,但是,但是,但是——————————————新浪微博————————————————是獨(dú)特的,它的實(shí)現(xiàn)方法是下面這樣的:
注意到了嗎,微博的標(biāo)題和鏈接必須是寫在一起的,就是我紅框框住的地方。
到此就可以實(shí)現(xiàn)分享功能了。
不過,我把復(fù)制鏈接的方法也分享給大家吧:
UIPasteboard *pastboad = [UIPasteboard generalPasteboard];
pastboad.string = @"鏈接地址";
就已經(jīng)放在粘貼板上面了。
這是代碼部分(我沒有提取,微信,朋友圈,QQ,QQ空間內(nèi)容一樣的,大家可自行提取):
舊版:
/**友盟分享*/
- (void)rightBarButtonAction
{
NSArray *titlearr = @[@"微信好友",@"微信朋友圈",@"QQ好友",@"QQ空間",@"新浪微博",@"復(fù)制鏈接"];
NSArray *imageArr = @[@"UMS_wechat_session_icon",@"UMS_wechat_timeline_icon",@"UMS_qq_icon",@"UMS_qzone_icon",@"UMS_sina_icon",@"fuzhilainjie"];
UIImage *cachedImage = [[SDImageCache sharedImageCache] imageFromDiskCacheForKey:self.imgUrl];
ActionSheetView *actionsheet = [[ActionSheetView alloc] initWithShareHeadOprationWith:titlearr andImageArry:imageArr andProTitle:@"測試" and:ShowTypeIsShareStyle];
[actionsheet setBtnClick:^(NSInteger btnTag) {
[UMSocialData defaultData].extConfig.title = self.shareTitle;
[WXApi isWXAppInstalled];
// 顯示在前面的圖片
if (btnTag ==0) {
[UMSocialData defaultData].extConfig.wechatSessionData.url = self.ashareurl;
[UMSocialData defaultData].extConfig.wechatSessionData.title = self.titleName;
[[UMSocialDataService defaultDataService]? postSNSWithTypes:@[UMShareToWechatSession]
content:self.describes
image:cachedImage? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
location:nil
urlResource:nil
presentedController:self completion:^(UMSocialResponseEntity *response){
if (response.responseCode == UMSResponseCodeSuccess) {
NSLog(@"分享成功");
}
}];
}
else if (btnTag ==1)
{
[UMSocialData defaultData].extConfig.wechatTimelineData.url = self.ashareurl;
[UMSocialData defaultData].extConfig.wechatTimelineData.title = self.titleName;
[[UMSocialDataService defaultDataService]? postSNSWithTypes:@[UMShareToWechatTimeline]
content:self.describes
image:cachedImage
location:nil
urlResource:nil
presentedController:self completion:^(UMSocialResponseEntity *response){
if (response.responseCode == UMSResponseCodeSuccess) {
NSLog(@"分享成功");
}
}];
}
else if (btnTag ==2)
{
[UMSocialData defaultData].extConfig.qqData.url = self.ashareurl;
[UMSocialData defaultData].extConfig.qqData.title = self.titleName;
[[UMSocialDataService defaultDataService]? postSNSWithTypes:@[UMShareToQQ]
content:self.describes
image:cachedImage
location:nil
urlResource:nil
presentedController:self completion:^(UMSocialResponseEntity *response){
if (response.responseCode == UMSResponseCodeSuccess) {
NSLog(@"分享成功");
}
}];
}
else if (btnTag ==3)
{
[UMSocialData defaultData].extConfig.qzoneData.url = self.ashareurl;
[UMSocialData defaultData].extConfig.qzoneData.title = self.titleName;
[[UMSocialDataService defaultDataService]? postSNSWithTypes:@[UMShareToQzone]
content:self.describes
image:cachedImage
location:nil
urlResource:nil
presentedController:self completion:^(UMSocialResponseEntity *response){
if (response.responseCode == UMSResponseCodeSuccess) {
NSLog(@"分享成功");
}
}];
}
else if (btnTag ==4)
{
[[UMSocialDataService defaultDataService]? postSNSWithTypes:@[UMShareToSina]
content:[NSString stringWithFormat:@"%@%@",self.describes,self.ashareurl]
image:cachedImage
location:nil
urlResource:nil
presentedController:self completion:^(UMSocialResponseEntity *response){
if (response.responseCode == UMSResponseCodeSuccess) {
NSLog(@"分享成功");
}
}];
}
else if (btnTag ==5)
{
UIPasteboard *pastboad = [UIPasteboard generalPasteboard];
pastboad.string = self.ashareurl;
ZTShowAlertMessage(@"復(fù)制鏈接成功");
}
}];
[[UIApplication sharedApplication].keyWindow addSubview:actionsheet];
}
新版:
/**友盟分享*/
- (void)secondBarButtonAction
{
NSArray *titlearr = @[@"微信好友",@"微信朋友圈",@"QQ好友",@"QQ空間",@"新浪微博",@"復(fù)制鏈接"];
NSArray *imageArr = @[@"UMS_wechat_session_icon",@"UMS_wechat_timeline_icon",@"UMS_qq_icon",@"UMS_qzone_icon",@"UMS_sina_icon",@"fuzhilainjie"];
ActionSheetView *actionsheet = [[ActionSheetView alloc] initWithShareHeadOprationWith:titlearr andImageArry:imageArr andProTitle:@"測試" and:ShowTypeIsShareStyle];
[actionsheet setBtnClick:^(NSInteger btnTag) {
//創(chuàng)建分享消息對象
UMSocialMessageObject *messageObject = [UMSocialMessageObject messageObject];
//創(chuàng)建網(wǎng)頁內(nèi)容對象
UMShareWebpageObject *shareObject = [UMShareWebpageObject shareObjectWithTitle:self.titleName descr:self.describes thumImage:self.shareImageurl];
//設(shè)置網(wǎng)頁地址
shareObject.webpageUrl = self.ashareurl;
//分享消息對象設(shè)置分享內(nèi)容對象
messageObject.shareObject = shareObject;
// 顯示在前面的圖片
if (btnTag ==0) {
//調(diào)用分享接口
[[UMSocialManager defaultManager] shareToPlatform:UMSocialPlatformType_WechatSession messageObject:messageObject currentViewController:self completion:^(id data, NSError *error) {
if (error) {
NSLog(@"************Share fail with error %@*********",error);
}else{
if ([data isKindOfClass:[UMSocialShareResponse class]]) {
UMSocialShareResponse *resp = data;
//分享結(jié)果消息
NSLog(@"response message is %@",resp.message);
//第三方原始返回的數(shù)據(jù)
NSLog(@"response originalResponse data is %@",resp.originalResponse);
}else{
NSLog(@"response data is %@",data);
}
}
}];
}
else if (btnTag ==1)
{
[[UMSocialManager defaultManager] shareToPlatform:UMSocialPlatformType_WechatTimeLine messageObject:messageObject currentViewController:self completion:^(id data, NSError *error) {
if (error) {
NSLog(@"************Share fail with error %@*********",error);
}else{
if ([data isKindOfClass:[UMSocialShareResponse class]]) {
UMSocialShareResponse *resp = data;
//分享結(jié)果消息
NSLog(@"response message is %@",resp.message);
//第三方原始返回的數(shù)據(jù)
NSLog(@"response originalResponse data is %@",resp.originalResponse);
}else{
NSLog(@"response data is %@",data);
}
}
}];
}
else if (btnTag ==2)
{
[[UMSocialManager defaultManager] shareToPlatform:UMSocialPlatformType_QQ messageObject:messageObject currentViewController:self completion:^(id data, NSError *error) {
if (error) {
NSLog(@"************Share fail with error %@*********",error);
}else{
if ([data isKindOfClass:[UMSocialShareResponse class]]) {
UMSocialShareResponse *resp = data;
//分享結(jié)果消息
NSLog(@"response message is %@",resp.message);
//第三方原始返回的數(shù)據(jù)
NSLog(@"response originalResponse data is %@",resp.originalResponse);
}else{
NSLog(@"response data is %@",data);
}
}
}];
}
else if (btnTag ==3)
{
[[UMSocialManager defaultManager] shareToPlatform:UMSocialPlatformType_Qzone messageObject:messageObject currentViewController:self completion:^(id data, NSError *error) {
if (error) {
NSLog(@"************Share fail with error %@*********",error);
}else{
if ([data isKindOfClass:[UMSocialShareResponse class]]) {
UMSocialShareResponse *resp = data;
//分享結(jié)果消息
NSLog(@"response message is %@",resp.message);
//第三方原始返回的數(shù)據(jù)
NSLog(@"response originalResponse data is %@",resp.originalResponse);
}else{
NSLog(@"response data is %@",data);
}
}
}];
}
else if (btnTag ==4)
{
UMSocialMessageObject *messageObject = [UMSocialMessageObject messageObject];
messageObject.text = [NSString stringWithFormat:@"%@%@",self.titleName, self.ashareurl];
//創(chuàng)建圖片內(nèi)容對象
UMShareImageObject *shareObject = [[UMShareImageObject alloc] init];
//如果有縮略圖,則設(shè)置縮略圖
[shareObject setShareImage:self.shareImageurl];
//分享消息對象設(shè)置分享內(nèi)容對象
messageObject.shareObject = shareObject;
[[UMSocialManager defaultManager] shareToPlatform:UMSocialPlatformType_Sina messageObject:messageObject currentViewController:self completion:^(id data, NSError *error) {
if (error) {
NSLog(@"************Share fail with error %@*********",error);
}else{
if ([data isKindOfClass:[UMSocialShareResponse class]]) {
UMSocialShareResponse *resp = data;
//分享結(jié)果消息
NSLog(@"response message is %@",resp.message);
//第三方原始返回的數(shù)據(jù)
NSLog(@"response originalResponse data is %@",resp.originalResponse);
}else{
NSLog(@"response data is %@",data);
}
}
}];
}
else if (btnTag ==5)
{
UIPasteboard *pastboad = [UIPasteboard generalPasteboard];
pastboad.string = self.ashareurl;
ZTShowAlertMessage(@"復(fù)制鏈接成功");
}
}];
[[UIApplication sharedApplication].keyWindow addSubview:actionsheet];
}