iOS分享---使用友盟分享(自定义分享面板)

在新的项目中,需要实现分享功能,比较后接入友盟分享。友盟分享的使用文档以及常见问题都非常的详细,接入也较为简单。只是分享面板与我们的App风格不太相符,所以自定义了一个分享面板。

项目地址在这里

实现的过程

1. ShareView

  • .h文件中,主要属性及初始化方法
//  点击按钮block回调
@property (nonatomic,copy) void(^btnClick)(NSInteger);

//  头部提示文字
@property (nonatomic,copy) NSString *proStr;

//  设置弹窗背景蒙板灰度(0~1)
@property (nonatomic,assign) CGFloat duration;

/**
 *  初始化
 *
 *  @param titleArray 标题数组
 *  @param imageArray 图片数组(如果不需要的话传空数组(@[])进来)
 *  @param proTitle   最顶部的标题  不需要的话传@""
 *
 *  @return ShareView
 */

- (instancetype)initWithShareHeadOprationWith:(NSArray *)titleArray andImageArry:(NSArray *)imageArray andProTitle:(NSString *)proTitle;
  • 初始化时,为视图添加手势,设置背景蒙板灰度,加载自定义视图
- (instancetype)initWithShareHeadOprationWith:(NSArray *)titleArray andImageArry:(NSArray *)imageArray andProTitle:(NSString *)proTitle {
    
    self = [super init];
    if (self) {
        
        _shareBtnTitleArray = titleArray;
        _shareBtnImageArray = imageArray;
        _protext = proTitle;
        
        self.frame = CGRectMake(0, 0, ScreenWidth, ScreenHeight);
        //  背景,带灰度
        self.backgroundColor = WINDOW_COLOR;
        //  可点击
        self.userInteractionEnabled = YES;
        //  点击背景,收起底部分享面板,移除本视图
        UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tappedCancel)];
        [self addGestureRecognizer:tapGesture];
        
        //  加载分享面板
        [self loadUIConfig];
    }
    return self;
}

  • 加载视图,依次添加分享的按钮,视图显示时,主要分享面板从底部弹出
/**
 加载自定义视图,按钮的tag依次为(200 + i)
 */
- (void)loadUIConfig {
    
    [self addSubview:self.bgView];
    [self.bgView addSubview:self.topSheetView];
    [self.bgView addSubview:self.cancelBtn];
    
    self.proLbl.text = _protext;
    //  按钮
    for (NSInteger i = 0; i < self.shareBtnTitleArray.count; i++) {
        
        CGFloat x = self.bgView.bounds.size.width / 3 * ( i % 3);
        CGFloat y = LABEL_HEIGHT + (i / 3) * LINE_HEIGHT;
        CGFloat w = self.bgView.bounds.size.width / 3;
        CGFloat h = 70;
        
        CGRect frame =  CGRectMake(x, y, w, h);
        ImageWithLabel *item = [ImageWithLabel imageLabelWithFrame:frame Image:[UIImage imageNamed:self.shareBtnImageArray[i]] LabelText:self.shareBtnTitleArray[i]];
        item.labelOffsetY = 6;
        
        item.tag = 200 + i;
        UITapGestureRecognizer *tapGes = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(itemClick:)];
        [item addGestureRecognizer:tapGes];
        [self.topSheetView addSubview:item];
        
        [self.buttons addObject:item];
    }
    //  弹出
    [UIView animateWithDuration:ANIMATE_DURATION animations:^{
        self.bgView.frame = CGRectMake(0, ScreenHeight - CGRectGetHeight(self.bgView.frame), ScreenWidth, CGRectGetHeight(self.bgView.frame));
    }];
    
    //  icon 动画
    [self iconAnimation];
}

  • 背景面板(这是一排三个的设计,个人认为更好看一些,由于本次分享平台较少,并没有限制高度或者做多高之后可以滚动。。。)
- (UIView *)bgView {
    
    if (_bgView == nil) {
        
        _bgView = [[UIView alloc] init];
        
        //  根据图标个数,计算行数,计算 backgroundView 的高度
        NSInteger index;
        if (_shareBtnTitleArray.count % 3 == 0) {
            
            index = _shareBtnTitleArray.count / 3;
        } else {
            
            index = _shareBtnTitleArray.count / 3 + 1;
        }
        _bgView.frame = CGRectMake(0, ScreenHeight, ScreenWidth, BUTTON_HEIGHT + (_protext.length == 0 ? 0 : 45) + LINE_HEIGHT * index);
    }
    return _bgView;
}
  • 点击背景面板或者取消按钮
/**
 点击取消
 */
- (void)tappedCancel {
    
    [UIView animateWithDuration:ANIMATE_DURATION animations:^{
        [self.bgView setFrame:CGRectMake(0, ScreenHeight, ScreenWidth, 0)];
        self.alpha = 0;
    } completion:^(BOOL finished) {
        if (finished) {
            [self removeFromSuperview];
        }
    }];
}

  • 这是一个借用了 Facebook 开源框架写的图标弹出动画,有点像今日头条的分享面板(😳)。代码简洁效果炫酷,你值得拥有(😬)。
/**
 做一个 icon 依次粗线的弹簧动画
 */
- (void)iconAnimation {
    
    CGFloat duration = 0;
    
    for (UIView *icon in self.buttons) {
        CGRect frame = icon.frame;
        CGRect toFrame = icon.frame;
        frame.origin.y += frame.size.height;
        icon.frame = frame;
        
        POPSpringAnimation *animation = [POPSpringAnimation animationWithPropertyNamed:kPOPViewFrame];
        animation.toValue = [NSValue valueWithCGRect:toFrame];
        animation.beginTime = CACurrentMediaTime() + duration;
        animation.springBounciness = 10.0f;
        
        [icon pop_addAnimation:animation forKey:kPOPViewFrame];
        
        duration += 0.07;
    }
}

2. 创建与配置

  • 判断设备中是否安装了要分享的平台对应的App(微博平台不用判断):
BOOL hadInstalledWeixin = [[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"weixin://"]];
BOOL hadInstalledQQ = [[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"mqq://"]];
  • 将安装的了App对应的图标和标题放入数组;

  • 实现 Button 点击的 Block 回调(例),回调的方法里根据不同的 type 设置不同的分享内容:

case 0: {
    // 微信
    [self shareTextToPlatformType:UMSocialPlatformType_WechatSession shareType:type model:model];
}
  • 将分享面板展示:
[[UIApplication sharedApplication].keyWindow addSubview:shareView];

3. 区分不同的平台,设置分享内容

  • 创建分享消息对象(U-Share SDK类):
UMSocialMessageObject *messageObject = [UMSocialMessageObject messageObject];
//设置文本
messageObject.text = @"";
  • 分享多媒体对象,例分享网页(U-Share SDK方法):
UMShareWebpageObject *webPageObject = [UMShareWebpageObject shareObjectWithTitle:model.title descr:model.detail thumImage:model.thumImage];
webPageObject.webpageUrl = model.url;
messageObject.shareObject = webPageObject;
  • 调用分享接口(U-Share SDK方法)
[[UMSocialManager defaultManager] shareToPlatform:platformType messageObject:messageObject currentViewController:self completion:^(id data, NSError *error) {
    if (error) {
        Log(@"************Share fail with error %@*********",error);
        [SVProgressHUD showErrorWithStatus:@"分享失败"];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [SVProgressHUD dismiss];
        });
    }else{
        Log(@"response data is %@",data);
        [SVProgressHUD showSuccessWithStatus:@"分享成功"];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [SVProgressHUD dismiss];
        });
    }
}];

4. 外部调用

ShareModel *model = [ShareModel shareAppModel];
// 分享
[CommonUtils shareBoardBySelfDefinedWithType:ShareTypeApp model:model];

实现的效果

这 个 尺 寸 非 常 的 完 美.png

遇到的问题

  1. 最开始配置好各个分享平台的 URL Scheme 之后,分享功能仍然无法正常使用,经检查是因为 iOS 9 的白名单问题。加入后解决。
  2. 由于是自定义的分享面板,在调用时需要判断当前设备中是否安装了相应的平台的应用,如 QQ、Wechat等。如果当前设备中没有安装某个应用而面板中带有该平台的分享图标,在 App 审核时可能不会被通过。友盟自带的分享面板已经做过这层判断。
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 199,636评论 5 468
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 83,890评论 2 376
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 146,680评论 0 330
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 53,766评论 1 271
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 62,665评论 5 359
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,045评论 1 276
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,515评论 3 390
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,182评论 0 254
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,334评论 1 294
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,274评论 2 317
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,319评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,002评论 3 315
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,599评论 3 303
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,675评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,917评论 1 255
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,309评论 2 345
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 41,885评论 2 341

推荐阅读更多精彩内容