集成友盟分享

首先添加友盟的sdk及设置分享跳转的URL types,还有设置白名单

分享界面的设置,一般都是要求文字配图片,图片在上,文字在下,这个网上有好多的方法

我是自定义控件button

- (id)initWithFrame:(CGRect)frame

{

self = [super initWithFrame:frame];

if (self) {

self.titleLabel.textAlignment = NSTextAlignmentCenter;

self.titleLabel.font = [UIFont systemFontOfSize:11.0];

[self setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];

[self setTitleColor:[UIColor colorWithRed:0.64f green:0.64f blue:0.64f alpha:1.00f] forState:UIControlStateHighlighted];

}

return self;

}

//取消按钮的高亮状态

- (void)setHighlighted:(BOOL)highlighted

{

}

-(void)layoutSubviews {

[super layoutSubviews];

// Center image

CGPoint center = self.imageView.center;

center.x = self.frame.size.width / 2;

center.y = self.imageView.frame.size.height / 2 + 15;

self.imageView.center = center;

self.imageView.size = CGSizeMake(50, 50);

//Center text

CGRect newFrame = [self titleLabel].frame;

newFrame.origin.x = 0;

newFrame.origin.y = self.imageView.frame.size.height + 20;

newFrame.size.width = self.frame.size.width;

self.titleLabel.frame = newFrame;

self.titleLabel.frame = CGRectMake(0, newFrame.origin.y, newFrame.size.width, 20);

self.titleLabel.textAlignment = NSTextAlignmentCenter;

}

然后是在视图中设置布局,使用的是9宫格,要求的效果图


.h

//点击按钮block回调

@property (nonatomic,copy) void(^btnClick)(NSInteger);

- (id)initWithShareHeadOprationWith:(NSArray *)titleArray andImageArry:(NSArray *)imageArr;

.m

- (id)initWithShareHeadOprationWith:(NSArray *)titleArray andImageArry:(NSArray *)imageArr

{

if (self = [super init]) {

self.shareBtnImgArray = imageArr;

self.shareBtnTitleArray = titleArray;

self.frame = CGRectMake(0, 0, SCREENW, SCREENH);

self.backgroundColor = ARGBColor(0.0, 0.0, 0.0, 0.3);

self.userInteractionEnabled = YES;

UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tappedCancel)];

[self addGestureRecognizer:tapGesture];

[self loadUiConfig];

}

return self;

}

///  加载9宫格的分享

- (void)loadUiConfig

{

[self addSubview:self.backGroundView];

[_backGroundView addSubview:self.cancelBtn];

for (NSInteger i = 0; i < _shareBtnImgArray.count; i++)

{

ActionButton *button = [ActionButton buttonWithType:UIButtonTypeCustom];

if (_shareBtnImgArray.count % 5 == 0) {

button.frame = CGRectMake(_backGroundView.bounds.size.width / 5 *(i % 5), (i / 5) * 100, _backGroundView.bounds.size.width / 5, 100);

}else{

button.frame = CGRectMake(_backGroundView.bounds.size.width / 5 * (i % 5), (i / 5) * 100, _backGroundView.bounds.size.width / 5, 100);

}

[button setTitle:_shareBtnTitleArray[i] forState:UIControlStateNormal];

[button setImage:[UIImage imageNamed:_shareBtnImgArray[i]] forState:UIControlStateNormal];

button.tag = 200 + i;

[button addTarget:self action:@selector(BtnClick:) forControlEvents:UIControlEventTouchUpInside];

[self.backGroundView addSubview:button];

}

if (_shareBtnImgArray.count > 5) {

UIView *lineView = [[UIView alloc]initWithFrame:CGRectMake(0, 100, SCREENW, 1)];

lineView.backgroundColor = ARGBColor(215.0, 215.0, 215.0, 1.0);

[self.backGroundView addSubview:lineView];

}

[UIView animateWithDuration:0.25 animations:^{

_backGroundView.frame = CGRectMake(0, SCREENH - CGRectGetHeight(_backGroundView.frame), SCREENW, CGRectGetHeight(_backGroundView.frame));

}];

}

//

- (void)BtnClick:(UIButton *)btn

{

[self tappedCancel];

if (btn.tag < 200) {

_btnClick(btn.tag - 100);

}else{

_btnClick(btn.tag - 200);

}

}

- (void)tappedCancel

{

[UIView animateWithDuration:0.25 animations:^{

[self.backGroundView setFrame:CGRectMake(0, SCREENH, SCREENW, 0)];

self.alpha = 0;

} completion:^(BOOL finished) {

if (finished) {

[self removeFromSuperview];

}

}];

}

#pragma mark - 懒加载

- (UIView *)backGroundView

{

if (_backGroundView == nil) {

_backGroundView = [[UIView alloc] init];

_backGroundView.backgroundColor = ARGBColor(229.0, 229.0, 229.0, 1.0);

if (_shareBtnImgArray.count < 5) {

_backGroundView.frame = CGRectMake(0, SCREENH, SCREENW, 150);

}else{

NSInteger index;

if (_shareBtnTitleArray.count % 5 == 0) {

index =_shareBtnTitleArray.count / 5;

}else{

index = _shareBtnTitleArray.count / 5 + 1;

}

_backGroundView.frame = CGRectMake(0, SCREENH, SCREENW, 50 + 100 * index );

}

UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(noTap)];

[_backGroundView addGestureRecognizer:tapGesture];

}

return _backGroundView;

}

- (void)noTap

{}

- (UIButton *)cancelBtn

{

if (_cancelBtn == nil) {

_cancelBtn = [UIButton buttonWithType:UIButtonTypeCustom];

_cancelBtn.frame = CGRectMake(0, CGRectGetHeight(_backGroundView.frame) - 50, CGRectGetWidth(_backGroundView.frame), 50);

[_cancelBtn setTitle:NSLocalizedString(@"button_cancel", nil) forState:UIControlStateNormal];

_cancelBtn.backgroundColor = [UIColor whiteColor];

[_cancelBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];

[_cancelBtn addTarget:self action:@selector(tappedCancel) forControlEvents:UIControlEventTouchUpInside];

}

return _cancelBtn;

}

分享是在工具类中,避免appdelegate重代码太多,有点小强迫

/**

*  分享到各个平台

*

*  @param PlatformName  平台名称,在试图控制器里配置,具体哪些平台参考UMSocialSnsPlatformManager.h

*  @param text          要分享的文本信息

*  @param description    要分享的描述信息

*  @param image          分享的图片信息(可以传NSData或UImage)

*  @param url            要分享的网址

*  @param viewController 从哪个视图控制器传过来的(一般直接写self)

*/

+ (void)shareToSocialPlatform:(id)PlatformName WithTitle:(NSString *)text  andDescription:(NSString *)description andImage:(id)image andUrl:(NSString *)url showOn:(UIViewController *)viewController;

在.m中实现

+ (void)shareToSocialPlatform:(id)PlatformName WithTitle:(NSString *)text andDescription:(NSString *)description andImage:(id)image andUrl:(NSString *)url showOn:(UIViewController *)viewController

{

if ([PlatformName isEqualToString:UMShareToSina]) {

// __weak id weakSelf = viewController;

[[UMSocialControllerService defaultControllerService] setShareText:[NSString stringWithFormat:@"%@%@",text, url] shareImage:image socialUIDelegate:(id)viewController];

[UMSocialSnsPlatformManager getSocialPlatformWithName:UMShareToSina].snsClickHandler(viewController,[UMSocialControllerService defaultControllerService],YES);

}else{

if ([PlatformName isEqualToString:UMShareToQQ]){

[UMSocialData defaultData].extConfig.qqData.url = url;

}else if ([PlatformName isEqualToString:UMShareToQzone]){

[UMSocialData defaultData].extConfig.qzoneData.url = url;

}else if ([PlatformName isEqualToString:UMShareToWechatSession]){

[UMSocialData defaultData].extConfig.wechatSessionData.url = url;

}else if ([PlatformName isEqualToString:UMShareToWechatTimeline]){

[UMSocialData defaultData].extConfig.wechatTimelineData.url = url;

}

[UMSocialData defaultData].extConfig.title = text;

[[UMSocialDataService defaultDataService] postSNSWithTypes:@[PlatformName] content:description image:image location:nil urlResource:nil presentedController:viewController completion:^(UMSocialResponseEntity* response) {

if (response.responseCode == UMSResponseCodeSuccess) {

}

}];

}

}

都具备了,直接在控制器中实现分享,由于项目需要实现语言国际化,所以titleArr使用的是语言包,有些键值不是分享的所以需要另外做判断

shareArray = @[

@{@"titleArr":@[NSLocalizedString(@"menu_share_group", nil), NSLocalizedString(@"menu_share_friend", nil), NSLocalizedString(@"menu_share_weibo", nil), NSLocalizedString(@"menu_share_qq", nil), NSLocalizedString(@"menu_share_qzone", nil), NSLocalizedString(@"menu_copy_url", nil), NSLocalizedString(@"menu_add_reward", nil), NSLocalizedString(@"menu_add_comm", nil), NSLocalizedString(@"menu_ignore_meet", nil), NSLocalizedString(@"menu_delete_ask", nil)]},

@{@"imageArr":@[@"朋友圈.png", @"微信好友.png", @"微博.png", @"QQ.png", @"QQ空间.png", @"复制链接.png", @"追加悬赏.png", @"添加备注.png", @"省掉见面.png", @"删除.png"]},

@{@"platformArr":@[UMShareToWechatTimeline,UMShareToWechatSession, UMShareToSina, UMShareToQQ,UMShareToQzone,@"lianJie",@"xuanShang",@"beiZhu",@"jianMian",@"shanChu"]}

];

}

shareArray = [CJUMengShare registerInstalled:shareArray];

ActionSheetView *shareView = [[ActionSheetView alloc]initWithShareHeadOprationWith:shareArray[0][@"titleArr"] andImageArry:shareArray[1][@"imageArr"]];

__weak id weakSelf = self;

[shareView setBtnClick:^(NSInteger btnTag) {

[weakSelf shareWithTitle:btnTag];

}];

[self.view addSubview:shareView];

//  下面的方法是根据tag值来做相应的事情,因为视图中除了有分享还有别的功能逻辑,所以在下面判断另行处理了

- (void)shareWithTitle:(NSInteger)btnTag

{

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 217,509评论 6 504
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,806评论 3 394
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 163,875评论 0 354
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,441评论 1 293
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,488评论 6 392
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,365评论 1 302
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,190评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,062评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,500评论 1 314
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,706评论 3 335
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,834评论 1 347
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,559评论 5 345
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,167评论 3 328
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,779评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,912评论 1 269
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,958评论 2 370
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,779评论 2 354

推荐阅读更多精彩内容

  • 前言 友盟能实现分享功能,友盟+申请第三方账号的目的: 进行分享、授权操作需要在第三方平台创建应用并提交审核,友盟...
    CoderZb阅读 9,714评论 8 60
  • 需要添加白名单:在info.list中的LSApplicationQueriesSchemes中添加 当你的应用在...
    FallPine阅读 290评论 0 0
  • 1.通过Cocoapods集成 1.1配置平台Cocoapods集成U-Share SDK可灵活配置平台,如工程t...
    Lost_693d阅读 684评论 0 0
  • 因为是新项目,所以我就索性重新集成最新6.1.0 版本的友盟. 然而6版本的友盟分享,是全新重构的版本!所以有几大...
    光彩影阅读 16,637评论 29 13
  • 项目上线前,升级友盟分享的插件至新版本,发现android部分需要配置工程中各项内容,意味着每次打新版都需要多一系...
    lyx2002py阅读 1,798评论 0 0