文档章节

自定义第三方分享弹出框

萍水相逢OSC
 萍水相逢OSC
发布于 2017/02/10 17:19
字数 910
阅读 27
收藏 0

1、首先,可用nib创建一个view(如图):



2、各个分享按钮添加点击事件

//代码中的curViewController
 UIViewController* curViewController = [self topViewControllerForViewController:[UIApplication sharedApplication].keyWindow.rootViewController];
//分段
case 1: //weibo
        {
            [[UMSocialData defaultData].extConfig.sinaData.urlResource setResourceType:UMSocialUrlResourceTypeDefault url:self.href];
            [[UMSocialDataService defaultDataService] postSNSWithTypes:@[UMShareToSina]
                                                               content:[NSString stringWithFormat:@"%@", self.descString]
                                                                 image:self.logoImage
                                                              location:nil
                                                           urlResource:resource
                                                   presentedController:curViewController
                                                            completion:^(UMSocialResponseEntity *response) {
                                                                if (response.responseCode == UMSResponseCodeSuccess) {
                                                                    NSLog(@"分享成功");
                                                                }
                                                            }];
            
            break;
        }
        case 2: //Wechat Timeline
        {
            [UMSocialData defaultData].extConfig.wxMessageType = UMSocialWXMessageTypeWeb;
            [UMSocialData defaultData].extConfig.wechatSessionData.url = self.href;
            [UMSocialData defaultData].extConfig.wechatTimelineData.url = self.href;
            [UMSocialData defaultData].extConfig.title = self.title;
            
            [[UMSocialDataService defaultDataService] postSNSWithTypes:@[UMShareToWechatTimeline]
                                                               content:[NSString stringWithFormat:@"%@", self.descString]
                                                                 image:self.logoImage
                                                              location:nil
                                                           urlResource:resource
                                                   presentedController:curViewController
                                                            completion:^(UMSocialResponseEntity *response) {
                                                                NSLog(@"%u",response.responseCode);
                                                                if (response.responseCode == UMSResponseCodeSuccess) {
                                                                    NSLog(@"分享成功");
                                                                }
                                                            }];
            break;
        }
        case 3: //WechatSession
        {
            [UMSocialData defaultData].extConfig.wechatSessionData.url = self.href;
            [UMSocialData defaultData].extConfig.wechatSessionData.title = self.title;
            
            [[UMSocialDataService defaultDataService] postSNSWithTypes:@[UMShareToWechatSession]
                                                               content:[NSString stringWithFormat:@"%@", self.descString]
                                                                 image:self.logoImage
                                                              location:nil
                                                           urlResource:resource
                                                   presentedController:curViewController
                                                            completion:^(UMSocialResponseEntity *response) {
                                                                if (response.responseCode == UMSResponseCodeSuccess) {
                                                                    NSLog(@"分享成功");
																}
                                                            }];
            
            break;
        }
        case 4: //qq
        {
            [UMSocialData defaultData].extConfig.qqData.title = self.title;
            [UMSocialData defaultData].extConfig.qqData.url = self.href;
            
            [[UMSocialDataService defaultDataService] postSNSWithTypes:@[UMShareToQQ]
                                                               content:[NSString stringWithFormat:@"%@", self.descString]
                                                                 image:self.logoImage
                                                              location:nil
                                                           urlResource:resource
                                                   presentedController:curViewController
                                                            completion:^(UMSocialResponseEntity *response) {
                                                                if (response.responseCode == UMSResponseCodeSuccess) {
                                                                    NSLog(@"分享成功");
                                                                }
                                                            }];
            
            break;
        }
        case 5: //brower
        {
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:self.href]];
            
            break;
        }
        case 6: //copy url
        {
            UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
            pasteboard.string = [NSString stringWithFormat:@"%@", self.href];
            MBProgressHUD *HUD = [Utils createHUD];
            HUD.mode = MBProgressHUDModeCustomView;
            HUD.label.text = @"已复制到剪切板";
            if (self.superview) {
                [self removeFromSuperview];
            }
            [HUD hideAnimated:YES afterDelay:1];
            
            break;
        }
        case 7:  //more
        {
            if (self.superview) {
                [self removeFromSuperview];
            }
            
            UIActivityViewController *activityViewController = [[UIActivityViewController alloc] initWithActivityItems:@[[NSString stringWithFormat:@"分享一个很有意思的内容 %@",self.href]] applicationActivities:nil];
            if ([activityViewController respondsToSelector:@selector(popoverPresentationController)]) {
                activityViewController.popoverPresentationController.sourceView = self;
            }

            [curViewController presentViewController:activityViewController animated:YES completion:nil];
            
            break;
        }
        case 8:     //tweet
        {
            if (self.superview) {
                [self removeFromSuperview];
            }
            
            BOOL isOuterIMP = NO;
            if ([_delegate respondsToSelector:@selector(customShareModeWithShareBoard:boardIndexButton:)]) {
                isOuterIMP = [_delegate customShareModeWithShareBoard:self boardIndexButton:button.tag];
            }
            
            if (isOuterIMP) break;
            
            OSCAbout* forwardInfo = [OSCAbout forwardInfoModelWithTitle:self.authordName
                                                                          content:self.descString type:self.aboutType fullWidth:[UIScreen mainScreen].bounds.size.width - 32];
            TweetEditingVC *tweetEditingVC = [[TweetEditingVC alloc] initWithAboutID:self.aboutId aboutType:self.aboutType forwardItem:forwardInfo];
            UINavigationController *tweetEditingNav = [[UINavigationController alloc] initWithRootViewController:tweetEditingVC];
            [curViewController presentViewController:tweetEditingNav animated:YES completion:nil];
            
            break;
        }
- (UIViewController *)topViewControllerForViewController:(UIViewController *)rootViewController {
    if ([rootViewController isKindOfClass:[UINavigationController class]]) {
        UINavigationController *navigationController = (UINavigationController *)rootViewController;
        return [self topViewControllerForViewController:navigationController.visibleViewController];
    }
    
    if (rootViewController.presentedViewController) {
        return [self topViewControllerForViewController:rootViewController.presentedViewController];
    }
    
    return rootViewController;
}

3、"取消"按钮隐藏弹出框:if (self.superview) {  [self removeFromSuperview]; }

4、用UITouch来控制点击背景隐藏弹出框(contentView为背景view):


#pragma mark --- touch handle 
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    _touchTrack = NO;
    UITouch* t = [touches anyObject];
    CGPoint p1 = [t locationInView:_contentView];
    if (!CGRectContainsPoint(_contentView.bounds, p1)) {
        _touchTrack = YES;
    }else{
        [super touchesBegan:touches withEvent:event];
    }
}

- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    if (_touchTrack) {
        if (self.superview) {
            [self removeFromSuperview];
        }
    }else{
        [super touchesEnded:touches withEvent:event];
    }
}

- (void)touchesCancelled:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    if (_touchTrack) {
        if (self.superview) {
            [self removeFromSuperview];
        }
    }else{
        [super touchesCancelled:touches withEvent:event];
    }
}

5、便利构造器(当前view)
 

+ (instancetype)shareBoardWithShareType:(InformationType)infomationType
							  withModel:(id)model
{
    OSCShareBoard *curShareBoard = [[[UINib nibWithNibName:@"OSCShareBoard" bundle:nil] instantiateWithOwner:nil options:nil] lastObject];
    [curShareBoard settingShareType:infomationType model:model];
    return curShareBoard;
}

[curShareBoard settingShareType:infomationType model:model];//model有多种类型参数处理

6、设置代理

- (BOOL)customShareModeWithShareBoard:(OSCShareBoard* )shareBoard
                     boardIndexButton:(NSInteger)buttonTag;

7、在.h文件中新建NSObject类型的对象OSCShareManager,初始化,添加外调方法传数据并设置代理方法

@class OSCShareManager;
@protocol OSCShareManagerDelegate <NSObject>

@optional
- (void)shareManagerCustomShareModeWithManager:(OSCShareManager* )shareManager
                         shareBoardIndexButton:(NSInteger)buttonTag;

@end

@interface OSCShareManager : NSObject

+ (instancetype)shareManager;

- (void)showShareBoardWithShareType:(InformationType)infomationType
                          withModel:(id)model;

- (void)hiddenShareBoard;

@property (nonatomic, weak) id <OSCShareManagerDelegate> delegate;

@end

8、在.m中实现OSCShareManager方法

#define SCREEN_HEIGHT [UIScreen mainScreen].bounds.size.height
#define SHAREBOARD_HEIGHT curShareBoard.bounds.size.height
#define SHAREBOARD_WIDTH curShareBoard.bounds.size.width

@interface OSCShareManager ()<OSCShareBoardDelegate>
{
	__weak OSCShareBoard* _curShareBoard;
}

@end

@implementation OSCShareManager

static OSCShareManager* _shareManager ;
+ (instancetype)shareManager
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _shareManager = [OSCShareManager new];
    });
    return _shareManager;
}

- (void)showShareBoardWithShareType:(InformationType)infomationType
						  withModel:(id)model
{
    if ([Config getOwnID] == 0) {
        UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"NewLogin" bundle:nil];
        NewLoginViewController *loginVC = [storyboard instantiateViewControllerWithIdentifier:@"NewLoginViewController"];
        [[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:loginVC animated:YES completion:nil];
        return ;
    }
    
	if (_curShareBoard) { _curShareBoard = nil;}
	
	OSCShareBoard *curShareBoard = [OSCShareBoard shareBoardWithShareType:infomationType withModel:model];
	_curShareBoard = curShareBoard;
	curShareBoard.frame = [UIScreen mainScreen].bounds;
	curShareBoard = curShareBoard;
	curShareBoard.delegate = self;
	
	[[UIApplication sharedApplication].keyWindow addSubview:curShareBoard];
	
	//背景蒙层的动画:alpha值从0.0变化到0.5
	[curShareBoard.bgView setAlpha:0.0];
	[UIView animateWithDuration:0.3 delay:0.0 options:UIViewAnimationOptionCurveLinear animations:^{
		[curShareBoard.bgView setAlpha:0.5];
	} completion:^(BOOL finished) { }];
	
	//分享面板的动画:从底部向上滚动弹出来
	[curShareBoard.contentView setFrame:CGRectMake(0, SCREEN_HEIGHT , SHAREBOARD_WIDTH, SHAREBOARD_HEIGHT )];
	[UIView animateWithDuration:0.3 delay:0.0 options:UIViewAnimationOptionCurveLinear animations:^{
		[curShareBoard.contentView setFrame:CGRectMake(0,SCREEN_HEIGHT - SHAREBOARD_HEIGHT,SHAREBOARD_WIDTH,SHAREBOARD_HEIGHT)];
	} completion:^(BOOL finished) {}];
}

- (void)hiddenShareBoard
{
    if (_curShareBoard.superview) {
        [_curShareBoard removeFromSuperview];
    }
}

#pragma mark --- OSCShareBoardDelegate
- (BOOL)customShareModeWithShareBoard:(OSCShareBoard* )shareBoard
                     boardIndexButton:(NSInteger)buttonTag
{
    if ([_delegate respondsToSelector:@selector(shareManagerCustomShareModeWithManager:shareBoardIndexButton:)]) {
        [_delegate shareManagerCustomShareModeWithManager:self shareBoardIndexButton:buttonTag];
        return YES;
    }
    return NO;
}

@end

9、页面调用该自定义的第三方分享弹出框(activityDetail为字典类型的数据)

OSCShareManager *shareManeger = [OSCShareManager shareManager];
    [shareManeger showShareBoardWithShareType:InformationTypeActivity withModel:_activityDetail];

 

© 著作权归作者所有

共有 人打赏支持
萍水相逢OSC
粉丝 104
博文 16
码字总数 4469
作品 0
深圳
程序员
私信 提问
第三方app分享内容到朋友圈,点击分享后可以弹出登录界面,登陆后没有分享提示框

第三方app分享内容到朋友圈,点击分享后可以弹出登录界面,登陆后没有分享提示框,以下是我的代码: 点击事件里面的代码,在这个类的onCreate里面注册的appid。 WXEntryActivity代码,类在包...

圈圈_cheng
2014/07/01
2.9K
1
ZUI 1.2 发布,增加新特性、优化细节、完善文档

前端html5框架ZUI发布1.2正式版本了! 此版本增加了很多新特性,同时修复了大量问题,文档也得到大幅完善。 欢迎大家使用并反馈问题 下载地址1:http://zui.sexy/ 下载地址2:http://git.os...

炒蚕豆吃蹦豆
2014/11/18
4.7K
0
Windows Phone 7 开发小技巧

1.使用Popup来实现自定义的弹出效果。Popup控件弹出的块会一直在屏幕的最前方,所以使用Popup可以实现各种各样的弹出框,并且给了你极大的自定义的空间,很多第三方的弹出框控件的原理其实就...

junwong
2012/02/28
806
0
js第三方插件(消息 图形)

1、SeaJS 实现JavaScript的模块化开发及加载机制 2、raphaeljs 实现跨浏览器的矢量图形实现方案 3、lhgcore.js 实现消息弹出框、表单验证、日历、下拉框、在线编辑组建 4、ymPrompt.js 实现消...

mingle
2015/11/30
97
0
iOS程序员眼中的客户端免登陆(数据迁移已更新)

2017.01.15 一、前言,为什么要做免登陆 2017年1月9日,蓄势已久的小程序正式上线,着实,张小龙 用完即走 的理念发挥的淋漓尽致,无需下载,扫码可用,用完即走 2017年互联网人口红利结束了...

si1ence
2017/12/14
0
0

没有更多内容

加载失败,请刷新页面

加载更多

composer安装

前言:随着开源的东西越来越多,一些好的代码我们是可以直接拿过来用的,github更是加快了这一节奏,在github上我们可以看到一些开源的项目、代码块、函数库、类结构等,我们可以直接Fork,然...

echojson
24分钟前
1
0
Longest Palindrome(leetcode409)

Given a string which consists of lowercase or uppercase letters, find the length of the longest palindromes that can be built with those letters. This is case sensitive, for exa......

woshixin
43分钟前
0
0
mysql grant 用户权限总结

用户权限管理主要有以下作用: 1. 可以限制用户访问哪些库、哪些表 2. 可以限制用户对哪些表执行SELECT、CREATE、DELETE、DELETE、ALTER等操作 3. 可以限制用户登录的IP或域名 4. 可以限制用...

Airship
58分钟前
1
0
RabbitMQ学习(3)

1. 消费端 1. 消费端通过推模式或者拉模式从RabbitMQ中获取并消费消息,当消费者确认处理消息后,可以手动确认消息已被接收,然后就会将该消息从RabbitMQ的队列中标记再清除,消费者端还可以...

江左煤郎
今天
1
0
linux mysql(5.7)开启慢查询

一、有3个配置需要设置, 1:相关开关 2:日志目录文件 3:慢查询的时间限制 二、设置完之后重启mysql service mariadb restart 三、重启后做个测试 连接mysql 并查询: select sleep(6); 四...

chro008
今天
2
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部