文档章节

自定义第三方分享弹出框

萍水相逢OSC
 萍水相逢OSC
发布于 2017/02/10 17:19
字数 910
阅读 26
收藏 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。 String videoUrl = "http://...

圈圈_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.6K
0
友盟分享UI标题问题

最近在做友盟分享开发,有个问题,选择完分享的平台后(比如选择了QQ的某个好友后),弹出的那个界面可以自定义UI吗,因为想添加一个edittext,做一个“说些什么”的输入框,还有左下角的标题...

deadPool
2016/07/22
40
0
iOS项目开发框架“完全体”(三)-- 奥义“手里剑”

本个章节进行BaseViewController的讲解,并放出部分源码哦 首先我们在开发一个项目搭建基类框架的时候,BaseViewController是一个非常重要的环节,后面涉及到继承,重写等各种问题,我们该考...

Suneday
2017/11/30
0
0
Windows Phone 7 开发小技巧

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

junwong
2012/02/28
779
0

没有更多内容

加载失败,请刷新页面

加载更多

白话SpringCloud | 第五章:服务容错保护(Hystrix)

前言 前一章节,我们知道了如何利用RestTemplate+Ribbon和Feign的方式进行服务的调用。在微服务架构中,一个服务可能会调用很多的其他微服务应用,虽然做了多集群部署,但可能还会存在诸如网...

oKong
27分钟前
0
0
【解惑】领略Java内部类的“内部”

内部类有两种情况: (1) 在类中定义一个类(私有内部类,静态内部类) (2) 在方法中定义一个类(局部内部类,匿名内部类) 1、私有内部类 —— 在方法之间定义的内部类,非静态 我们首先看看类中...

偶尔诗文
今天
1
0
sqlserver 2008 r2 直接下载地址(百度云)

之前下载的sqlserver2008发现不能附加,就卸载了,重新找到了sqlserver2008R2的百度云资源 卸载sqlserver2008还是有点麻烦,不过就是需要删除注册表中的信息 自己来回卸载了3次终于重装sqlse...

dillonxiao
今天
1
0
[Java]JVM调优总结 -Xms -Xmx -Xmn -Xss

JVM调优总结 -Xms -Xmx -Xmn -Xss 博客分类: Java General JVM应用服务器电信CMS算法 堆大小设置 JVM 中最大堆大小有三方面限制:相关操作系统的数据模型(32-bt还是64-bit)限制;系统的可...

morpheusWB
今天
2
0
C++ std::function 和 std::bind

C++11提供了std::function和std::bind两个工具,用于引用可调用对象。这些可调用对象包括 普通函数,Lambda表达式,类的静态成员函数,非静态成员函数以及仿函数等。引用可调用对象,可以用于...

yepanl
今天
4
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部