公司项目需要做分享功能,本来想着只是一个简单的分享功能,没想到却耗了我近一天的时间,那个心累啊,写下这篇给那些最近需要集成分享功能的朋友们
1. 到底选择哪一个第三方呢?
相信大家一开始也不知道改用那个吧,就功能来讲,个人觉得貌似没什么差别,只说一下我的经历,因为我之前做分享都是用的友盟,所以,我第一想到的就是友盟,有事二话没说,就用Pod 搜了一下友盟,结果是这样的:
但是看官网,最新版却是:
Pod 到的最新版是5.2.1 而 最新SDK的版本是6.0.1的,我们当然要用最新的了,因为最新的支持iOS10,还支持自动隐藏未安装的客户端等新特性,像友盟分享,这样的第三方,能用Pod 还是用Pod ,因为,首先Pod可以给我做SDK包依赖等,省去开发者的很多环境配置,主要还是,随着iOS系统的升级、更新,share的SDK势必也要更新,而我们只需要pod update就好了,不要做更多是操作,好处不言而喻了, 可是现在的这种情况是...我想,友盟新版不会不支持Pod了吧.....
于是,我果断的问了客服,客服的告诉我 :目前新版还不支持Pod,是的,你没看错,是不支持,但是说这两天就会更新的,可以耐心的等待下。。。。
老实说,我倒是想耐心等待,可是项目不等人啊,没办法硬着头皮去看shareSDK了,结果发现,它是支持Pod的,而且是最新版的。心想:貌似shareSDK效率比UMeng靠谱很多啊,于是准备Pod。。
经过漫长等待后,得到的结果只这样的,如官方的示例
我不会告诉你,我从昨天Pod少说Pod了一二十遍了,结果无一例外,但就在写这篇博文的时候,我又试了一下,结果竟然又成功了,我还能说什呢???郁闷
2. 一个好扯淡的问题
整理好心情,昨天的Pod不给力,那没办法,只能集成SDK了,既然是集成SDK,我当然还是用我熟悉的UMeng啦
按照官方给出的方法,一一做就好了,我就不多啰嗦了
- 我只是做新浪、微信、qq这几种,so URL Types 的配置如下:
还有需要适配iOS10,需要在info.plist中加入安全域名白名单,值得一提的是,iOS10对权限的要求更高了,相册,相机等也需要配置权限,详细的信息配置 可以再友盟的官网看到,我就不废话了。,提示大家,配置代码不要粘错了哦。
一切准备就绪后,运行程序,在link包的时候爆出 Duplicate Symbol 的错误,具体不记得了,翻译过里啊,就是说有2 个重复的标识,一个是Tencent的包里面,还有一个指的是TCWebViewController,这这两个重复了,因为项目的前缀是TC ,估计是这文件和腾讯里的某个文件重名了吧,所以,改了TCWebViewController为TCWKWebViewController,没想过倒是解决了,感觉好扯啊,之后集成就很顺利了。
3. 自定义分享弹出界面
因为项目只需要,微博、QQ 、微信 共个图标,所以,用友盟的默认图标集成UI界面 好丑啊,简直看不下去,而且,自己的扩展性也不高,所以,因为,模拟器看不出效果,所以就不贴图了,直接上代码:
#import <UIKit/UIKit.h>
#import <UMSocialCore/UMSocialCore.h>
typedef void(^UMSocialSharePlatformSelectionBlock)(UIView *NewsShareView,UMSocialPlatformType platformType);
@interface TCHomeNewsShareView : UIView
@property (nonatomic, assign) UMSocialPlatformType selectionPlatform;
@property (nonatomic, copy) UMSocialSharePlatformSelectionBlock shareSelectionBlock;
///可分享的数组
@property (nonatomic, strong) NSMutableArray *sharePlatformInfoArray;
- (void)show;
- (void)hiddenShareMenuView;
#import "TCHomeNewsShareView.h"
#import "UMShareMenuItem.h"
static NSString *kUMSplatformType = @"kUMSplatformType";
static NSString *kUMSSharePlatformType = @"kUMSSharePlatformType";
static NSString *kUMSSharePlatformIconName = @"kUMSSharePlatformIconName";
static NSString *kUMSSharePlatformItemView = @"UMSSharePlatformItemView";
@interface TCHomeNewsShareView ()
///半透明背景视图
@property (nonatomic, strong) UIView *backgroundGrayView;
///退出按钮
@property (nonatomic, strong) UIButton *cancelButton;
///列数
@property (nonatomic, assign) NSInteger columnCount;
///按钮大小
@property (nonatomic, assign) CGSize itemSize;//按钮大小
@end
@implementation TCHomeNewsShareView
- (instancetype)init {
self = [super initWithFrame:CGRectMake(0, kScreenH, kScreenW, 188)];
if (self){
[self addSubview:self.cancelButton];
self.columnCount = 3;
self.itemSize = CGSizeMake(70, 70);
self.backgroundColor = [UIColor whiteColor];
//获取可分享平台
NSMutableArray *platformArray = [[NSMutableArray alloc] init];
for (NSNumber *platformType in [UMSocialManager defaultManager].platformTypeArray) {
NSMutableDictionary *dict = [self dictWithPlatformName:platformType];
[dict setObject:platformType forKey:kUMSSharePlatformType];
if (dict) {
[platformArray addObject:dict];
}
}
if (platformArray.count == 0) {//如果没有有效的分享平台,则不创建分享菜单
UMSocialLogDebug(@"There is no any valid platform");
return nil;
}
[platformArray sortUsingComparator:^NSComparisonResult(id _Nonnull obj1, id _Nonnull obj2) {
if ([[obj1 valueForKey:kUMSSharePlatformType] integerValue] > [[obj2 valueForKey:kUMSSharePlatformType] integerValue]) {
return NSOrderedDescending;
}else{
return NSOrderedAscending;
}
}];
self.sharePlatformInfoArray = platformArray;
[self reloadViewWithitems:self.sharePlatformInfoArray];
}
return self;
}
- (void)reloadViewWithitems:(NSArray *)items {
CGFloat margin = (kScreenW - (self.itemSize.width * self.columnCount))/(self.columnCount + 1);
for (NSInteger index = 0; index < items.count; index ++) {
NSLog(@"%@",items[index]);
NSInteger column = index % self.columnCount;//取余数作为列
NSInteger line = index / self.columnCount;//取商为行
UIView *itemView = [items[index] valueForKeyPath:kUMSSharePlatformItemView];
itemView.frame = CGRectMake((column+1) *margin + column * self.itemSize.width, line * (kMargen + self.itemSize.height), self.itemSize.width, self.itemSize.height);
[self addSubview:itemView];
}
}
#pragma mark - respond event
- (void)show {
[[UIApplication sharedApplication].keyWindow addSubview:self.backgroundGrayView];
[[UIApplication sharedApplication].keyWindow addSubview:self];
[UIView animateWithDuration:0.35 animations:^{
self.top = kScreenH - self.height;
}];
}
///隐藏视图
- (void)hiddenShareMenuView {
[UIView animateWithDuration:0.35 animations:^{
//向下移动
self.top = kScreenH;
} completion:^(BOOL finished) {
[self.backgroundGrayView removeFromSuperview];
[self removeFromSuperview];
}];
}
#pragma mark - lazy load
///半透明背景视图
- (UIView *)backgroundGrayView {
if (!_backgroundGrayView) {
_backgroundGrayView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenW, kScreenH)];
_backgroundGrayView.backgroundColor = [UIColor blackColor];
_backgroundGrayView.alpha = 0.3;
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hiddenShareMenuView)];
[_backgroundGrayView addGestureRecognizer:tap];
}
return _backgroundGrayView;
}
- (UIButton *)cancelButton {
if (!_cancelButton) {
_cancelButton = [UIButton buttonWithType:UIButtonTypeCustom];
_cancelButton.frame = CGRectMake(0, self.height - 40, self.frame.size.width, 40);
[_cancelButton addTarget:self action:@selector(hiddenShareMenuView) forControlEvents:UIControlEventTouchUpInside];
[_cancelButton setTitle:@"取消" forState:UIControlStateNormal];
[_cancelButton setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
_cancelButton.backgroundColor = [UIColor whiteColor];
}
return _cancelButton;
}
#pragma mark - get platform Info
- (NSMutableDictionary *)dictWithPlatformName:(NSNumber *)platformType
{
UMSocialPlatformType platformType_int = [platformType integerValue];
NSString *imageName = nil;
NSString *platformName = nil;
NSMutableDictionary *dict = [[NSMutableDictionary alloc] initWithCapacity:1];
switch (platformType_int) {
case UMSocialPlatformType_Sina:
imageName = @"UMS_sina_icon";
platformName = UMLocalizedString(@"sina",@"新浪微博");
break;
case UMSocialPlatformType_WechatSession:
imageName = @"UMS_wechat_session_icon";
platformName = UMLocalizedString(@"wechat",@"微信");
break;
case UMSocialPlatformType_WechatTimeLine:
imageName = @"UMS_wechat_timeline_icon";
platformName = UMLocalizedString(@"wechat_timeline",@"微信朋友圈");
break;
case UMSocialPlatformType_WechatFavorite:
imageName = @"UMS_wechat_favorite_icon";
platformName = UMLocalizedString(@"wechat_favorite",@"微信收藏");
break;
case UMSocialPlatformType_QQ:
imageName = @"UMS_qq_icon";
platformName = UMLocalizedString(@"qq",@"QQ");
break;
case UMSocialPlatformType_Qzone:
imageName = @"UMS_qzone_icon";
platformName = UMLocalizedString(@"qzone",@"QQ空间");
break;
case UMSocialPlatformType_TencentWb:
imageName = @"UMS_tencent_icon";
platformName = UMLocalizedString(@"tencentWB",@"腾讯微博");
break;
case UMSocialPlatformType_AlipaySession:
imageName = @"UMS_alipay_session_icon";
platformName = UMLocalizedString(@"alipay",@"支付宝");
break;
case UMSocialPlatformType_LaiWangSession:
imageName = @"UMS_laiwang_session";
platformName = UMLocalizedString(@"lw_session",@"点点虫");
break;
case UMSocialPlatformType_LaiWangTimeLine:
imageName = @"UMS_laiwang_timeline";
platformName = UMLocalizedString(@"lw_timeline",@"点点虫动态");
break;
case UMSocialPlatformType_YixinSession:
imageName = @"UMS_yixin_session";
platformName = UMLocalizedString(@"yixin_session",@"易信");
break;
case UMSocialPlatformType_YixinTimeLine:
imageName = @"UMS_yixin_timeline";
platformName = UMLocalizedString(@"yixin_timeline",@"易信朋友圈");
break;
case UMSocialPlatformType_YixinFavorite:
imageName = @"UMS_yixin_favorite";
platformName = UMLocalizedString(@"yixin_favorite",@"易信收藏");
break;
case UMSocialPlatformType_Douban:
imageName = @"UMS_douban_icon";
platformName = UMLocalizedString(@"douban",@"豆瓣");
break;
case UMSocialPlatformType_Renren:
imageName = @"UMS_renren_icon";
platformName = UMLocalizedString(@"renren",@"人人");
break;
case UMSocialPlatformType_Email:
imageName = @"UMS_email_icon";
platformName = UMLocalizedString(@"email",@"邮箱");
break;
case UMSocialPlatformType_Sms:
imageName = @"UMS_sms_icon";
platformName = UMLocalizedString(@"sms",@"短信");
break;
case UMSocialPlatformType_Facebook:
imageName = @"UMS_facebook_icon";
platformName = UMLocalizedString(@"facebook",@"Facebook");
break;
case UMSocialPlatformType_Twitter:
imageName = @"UMS_twitter_icon";
platformName = UMLocalizedString(@"twitter",@"Twitter");
break;
case UMSocialPlatformType_Instagram:
imageName = @"UMS_instagram_icon";
platformName = UMLocalizedString(@"instagram",@"Instagram");
break;
case UMSocialPlatformType_Line:
imageName = @"UMS_line_icon";
platformName = UMLocalizedString(@"line",@"Line");
break;
case UMSocialPlatformType_Flickr:
imageName = @"UMS_flickr_icon";
platformName = UMLocalizedString(@"flickr",@"Flickr");
break;
case UMSocialPlatformType_KakaoTalk:
imageName = @"UMS_kakao_icon";
platformName = UMLocalizedString(@"kakaoTalk",@"KakaoTalk");
break;
case UMSocialPlatformType_Pinterest:
imageName = @"UMS_pinterest_icon";
platformName = UMLocalizedString(@"pinterest",@"Pinterest");
break;
case UMSocialPlatformType_Tumblr:
imageName = @"UMS_tumblr_icon";
platformName = UMLocalizedString(@"tumblr",@"Tumblr");
break;
case UMSocialPlatformType_Linkedin:
imageName = @"UMS_linkedin_icon";
platformName = UMLocalizedString(@"linkedin",@"Linkedin");
break;
case UMSocialPlatformType_Whatsapp:
imageName = @"UMS_whatsapp_icon";
platformName = UMLocalizedString(@"whatsapp",@"Whatsapp");
break;
default:
break;
}
[dict setObject:UMSocialPlatformIconWithName(imageName) forKey:kUMSSharePlatformIconName];
[dict setObject:platformName forKey:kUMSplatformType];
//为各平台创建按钮
UMShareMenuItem *cell = [[UMShareMenuItem alloc] init];
[cell reloadDataWithImage:[UIImage imageNamed:UMSocialPlatformIconWithName(imageName)] platformName:platformName];
cell.index = platformType_int;
__weak typeof(self) weakSelf = self;
cell.tapActionBlock = ^(NSInteger index){
__strong typeof(TCHomeNewsShareView *)strongSelf = weakSelf;
strongSelf.selectionPlatform = index;
if (strongSelf.shareSelectionBlock) {
[strongSelf hiddenShareMenuView];
strongSelf.shareSelectionBlock(strongSelf, index);
}
};
[dict setObject:cell forKey:kUMSSharePlatformItemView];
return dict;
}
这里,已经加了隐藏未有的App图标了,然后在UMSocialUIManager.m里面
- (void)creatShareSelectionView
{
TCHomeNewsShareView *shareView = [[TCHomeNewsShareView alloc] init];
self.shareMenuView = shareView;
}
把View换成我们创建的View 就好了,后面根据UI修改我们创建的UI界面,就很方便了。如果有什么问题和错题,欢迎大家指正,不胜感谢!!!