我们在做分享时纠结于新浪的分享显示和别的平台不一样,新浪的分享链接显示蓝色的文字,其他平台如果分享内容同意显示链接(比如:http://www.xxx.com),那么恭喜你省了一点功夫,但是如果需求上说内容不允许出现链接怎么办?上一篇内容写了在ShareSDK中如何自定义分享的内容,下面说下在友盟中如何来自定义,先看图:
如图是同一个组件中分享出来的,可以看到,显示是不一样的。
下面看下代码:
/*
自定义分享样式,可以自己构造分享列表页面
*/
-(IBAction)showShareList3:(UIButton *)sender
{
//iOS8下使用UIAlertController
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 80000
if([[UIDevice currentDevice].systemVersion floatValue] >= 8.0){
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"图文分享" message:@"图文分享" preferredStyle:UIAlertControllerStyleActionSheet];
for (NSString *snsName in [UMSocialSnsPlatformManager sharedInstance].allSnsValuesArray) {
UMSocialSnsPlatform *snsPlatform = [UMSocialSnsPlatformManager getSocialPlatformWithName:snsName];
UIAlertAction *alertAction = [UIAlertAction actionWithTitle:snsPlatform.displayName style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
//设置分享内容,和回调对象
NSString *shareText;
if ([snsName isEqualToString:@"sina"]) {
shareText= @"友盟社会化组件可以让移动应用快速具备社会化分享、登录、评论、喜欢等功能,并提供实时、全面的社会化数据统计分析服务。 http://www.umeng.com/social";
}
else
shareText = @"Hello";
UIImage *shareImage = [UIImage imageNamed:@"UMS_social_demo"];
[[UMSocialControllerService defaultControllerService] setShareText:shareText shareImage:shareImage socialUIDelegate:self];
UMSocialSnsPlatform *snsPlatform = [UMSocialSnsPlatformManager getSocialPlatformWithName:snsName];
snsPlatform.snsClickHandler(self,[UMSocialControllerService defaultControllerService],YES);
}];
[alertController addAction:alertAction];
}
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
[alertController dismissViewControllerAnimated:YES completion:nil];
}];
[alertController addAction:cancelAction];
UIPopoverPresentationController *popover = alertController.popoverPresentationController;
if (popover)
{
popover.sourceView = sender;
popover.sourceRect = sender.bounds;
popover.permittedArrowDirections = UIPopoverArrowDirectionAny;
}
[self presentViewController:alertController animated:YES completion:nil];
} else {
#endif
UIActionSheet * editActionSheet = [[UIActionSheet alloc] initWithTitle:@"图文分享" delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil];
for (NSString *snsName in [UMSocialSnsPlatformManager sharedInstance].allSnsValuesArray) {
UMSocialSnsPlatform *snsPlatform = [UMSocialSnsPlatformManager getSocialPlatformWithName:snsName];
[editActionSheet addButtonWithTitle:snsPlatform.displayName];
}
[editActionSheet addButtonWithTitle:@"取消"];
editActionSheet.tag = kTagShareEdit;
[editActionSheet showFromTabBar:self.tabBarController.tabBar];
editActionSheet.delegate = self;
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 80000
}
#endif
}
-(IBAction)showShareList4:(id)sender
{
UIActionSheet * editActionSheet = [[UIActionSheet alloc] initWithTitle:@"直接分享到微博" delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil];
for (NSString *snsName in [UMSocialSnsPlatformManager sharedInstance].allSnsValuesArray) {
UMSocialSnsPlatform *snsPlatform = [UMSocialSnsPlatformManager getSocialPlatformWithName:snsName];
[editActionSheet addButtonWithTitle:snsPlatform.displayName];
}
[editActionSheet addButtonWithTitle:@"取消"];
editActionSheet.tag = kTagSharePost;
editActionSheet.cancelButtonIndex = editActionSheet.numberOfButtons - 1;
[editActionSheet showFromTabBar:self.tabBarController.tabBar];
editActionSheet.delegate = self;
}
/*
在自定义分享样式中,根据点击不同的点击来处理不同的的动作
*/
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex + 1 >= actionSheet.numberOfButtons ) {
return;
}
NSLog(@"click button index is %d",buttonIndex);
//分享编辑页面的接口,snsName可以换成你想要的任意平台,例如UMShareToSina,UMShareToWechatTimeline
NSString *snsName = [[UMSocialSnsPlatformManager sharedInstance].allSnsValuesArray objectAtIndex:buttonIndex];
NSString *shareText;
if (buttonIndex == 0) {
shareText= @"友盟社会化组件可以让移动应用快速具备社会化分享、登录、评论、喜欢等功能,并提供实时、全面的社会化数据统计分析服务。 http://www.umeng.com/social";
}
else
shareText = @"Hello";
UIImage *shareImage = [UIImage imageNamed:@"UMS_social_demo"];
if (actionSheet.tag == kTagShareEdit) {
//设置分享内容,和回调对象
[[UMSocialControllerService defaultControllerService] setShareText:shareText shareImage:shareImage socialUIDelegate:self];
UMSocialSnsPlatform *snsPlatform = [UMSocialSnsPlatformManager getSocialPlatformWithName:snsName];
snsPlatform.snsClickHandler(self,[UMSocialControllerService defaultControllerService],YES);
} else if (actionSheet.tag == kTagSharePost){
[[UMSocialDataService defaultDataService] postSNSWithTypes:@[snsName] content:shareText image:shareImage location:nil urlResource:nil presentedController:self completion:^(UMSocialResponseEntity * response){
if (response.responseCode == UMSResponseCodeSuccess) {
UIAlertView * alertView = [[UIAlertView alloc] initWithTitle:@"成功" message:@"分享成功" delegate:nil cancelButtonTitle:@"好" otherButtonTitles:nil];
[alertView show];
} else if(response.responseCode != UMSResponseCodeCancel) {
UIAlertView * alertView = [[UIAlertView alloc] initWithTitle:@"失败" message:@"分享失败" delegate:nil cancelButtonTitle:@"好" otherButtonTitles:nil];
[alertView show];
}
}];
}
}
以上代码来源于友盟的Demo,博主在里面加入了自己的判断,就实现了标题中的功能,上面有两种分享的方式,第一种直接在设置分享内容时给定不一样的自定义内容,第二种在分享时,根据不同的分享平台给定不同的分享内容,看完之后是不是觉得很简单呢?
关键代码如下:
NSString *shareText;
if ([snsName isEqualToString:@"sina"])
{
shareText= @"友盟社会化组件可以让移动应用快速具备社会化分享、登录、评论、喜欢等功能,并提供实时、全面的社会化数据统计分析服务。 http://www.umeng.com/social";
}
else
shareText = @"Hello";
和
if (buttonIndex == 0)
{
shareText= @"友盟社会化组件可以让移动应用快速具备社会化分享、登录、评论、喜欢等功能,并提供实时、全面的社会化数据统计分析服务。 http://www.umeng.com/social";
}
else
shareText = @"Hello";
注意上面是两种分享方式,所以才有了两种判断,原理都一样的。
有需要Demo的请自行下载。
Demo下载地址