最新友盟6.1.1集成遇到的坑,自定义分享界面实现(跳转控制器做分享)

先总结一下遇到的坑:

1.分享面板无法弹出

由于 1. 创建Xcode项目会默认添加Main.storyboard作为Main Interface(General - Deployment Info),也就是项目的主Window。 2. 如果没使用Main.storyboard而又另外在AppDelegate中创建了UIWindow对象,如

self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds]

如果项目中同时出现Main Interface以及代码创建UIWindow会导致分享面板无法正常弹出,解决方法是移除其一即可。如果移除了Main.storyboard,需要clean工程后再重新运行。

2.推荐cocoapods实现就不需要再导入依赖库,会省很多事

下面实现自定义分享界面:

有时候我们做第三方分享时,不希望用系统的sheet或者alet样式,该怎么办呢,下面是本人实现的代码;跳转控制器中实现即可:

1.在控制器中要引用头文件

#import <UMSocialCore/UMSocialCore.h>/** 友盟头文件 */

#import <UShareUI/UShareUI.h>/** 友盟头文件 */

#import "ActionButton.h"/** 自定义button:实现上图片,下文字 */

#import "Masonry.h"/** 用到自动布局 */

 2.由于用到了九宫格排布定义每行显示多少个的宏

#define Count 4

3.具体实现代码:

- (void)viewDidLoad {

    [super viewDidLoad];

    self.view.layer.contents = [UIImage imageNamed:@"dl_bjt"];

    self.title = @"分享";

    UIImageView *imgView = [[UIImageView alloc]initWithFrame:self.view.bounds];

    imgView.userInteractionEnabled = YES;

     imgView.image = [UIImage imageNamed:@"dl_bjt"];

    [self.view addSubview:imgView];

     [self setUI];/**自定义界面*/

    }

 -(void)setUI{

    /**这里实现了新浪,微信朋友圈,微信聊天,QQ聊天四个*/

    [UMSocialUIManager setPreDefinePlatforms:@[@(UMSocialPlatformType_Sina),@(UMSocialPlatformType_QQ),@( UMSocialPlatformType_WechatTimeLine ),@(UMSocialPlatformType_WechatSession)]];

    

    NSArray *titleArr = @[@"新浪微博",@"QQ好友",@"微信朋友圈",@"微信好友"];

    NSArray *imgArr = @[@"umsocial_sina",@"umsocial_qq",@"umsocial_wechat_timeline",@"umsocial_wechat"];

    

    UIView *shareView = [[UIView alloc]init];/**分享按钮界面,方便位置移动*/

    

    [self.view addSubview:shareView];

    

    [shareView mas_makeConstraints:^(MASConstraintMaker *make) {

        

        make.centerX.mas_equalTo(self.view.mas_centerX);

        

        make.width.mas_equalTo(kScreenW);

        

        make.height.mas_equalTo(100);

        

        make.bottom.mas_equalTo(self.view).offset(-50);

        

    }];

    

    for (int i = 0; i<titleArr.count; i++)

    {

        ActionButton *button = [ActionButton buttonWithType:UIButtonTypeCustom];

        int row = (int)i/Count;

        

        int col = i%Count;

        

        float btnW = 60;

        

        float btnH = 80;

        

        float magrin = (kScreenW-Count*btnW)/(Count+1);

        

        button.frame = CGRectMake(magrin+col*(magrin+btnW), 10+row*(btnH+10),btnW, btnH);

        

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

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

        button.tag = i;

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

        [shareView addSubview:button];

    }

}

-(void)click:(UIButton *)sender{

    

    switch (sender.tag) {

        case 0:

            [self runShareWithType:UMSocialPlatformType_Sina];;

            break;

        case 1:

              [self runShareWithType:UMSocialPlatformType_QQ];

            break;

        case 2:

            [self runShareWithType:UMSocialPlatformType_WechatTimeLine];

            break;

        default:

            [self runShareWithType:UMSocialPlatformType_WechatSession];

            break;

    }

}

 -(void)systemUI{

 /**友盟自带界面*/

    [UMSocialUIManager setPreDefinePlatforms:@[@(UMSocialPlatformType_Sina),@(UMSocialPlatformType_QQ),@( UMSocialPlatformType_WechatTimeLine ),@(UMSocialPlatformType_WechatSession)]];

    

    //    //设置用户自定义的平台

    //

//        [UMSocialUIManager addCustomPlatformWithoutFilted:UMSocialPlatformType_UserDefine_Begin+2

//    

//                                         withPlatformIcon:[UIImage imageNamed:@"zhongjianggonggao_xyzj"]

//    

//                                         withPlatformName:@"复制链接"];

    

    [UMSocialShareUIConfig shareInstance].sharePageGroupViewConfig.sharePageGroupViewPostionType = UMSocialSharePageGroupViewPositionType_Bottom;/**显示位置底部*/

    [UMSocialShareUIConfig shareInstance].sharePageScrollViewConfig.shareScrollViewPageItemStyleType = UMSocialPlatformItemViewBackgroudType_IconAndBGRadius;/**分享按钮显示样式*/

    

    [UMSocialUIManager showShareMenuViewInWindowWithPlatformSelectionBlock:^(UMSocialPlatformType platformType, NSDictionary *userInfo) {

        //在回调里面获得点击的

        if (platformType == UMSocialPlatformType_UserDefine_Begin+2)

        {

            NSLog(@"你点击了复制链接按钮");

        }

        else

        {

            [self runShareWithType:platformType];

        }

    }];

 }

 - (void)runShareWithType:(UMSocialPlatformType)type

{

    //创建分享消息对象

    UMSocialMessageObject *messageObject = [UMSocialMessageObject messageObject];

    //设置文本

    messageObject.text = @"#燕南科技#";

    //调用分享接口

    [[UMSocialManager defaultManager] shareToPlatform:type messageObject:messageObject currentViewController:self completion:^(id data, NSError *error) {

        if (error) {

            UMSocialLogInfo(@"************Share fail with error %@*********",error);

        }else{

            if ([data isKindOfClass:[UMSocialShareResponse class]]) {

                UMSocialShareResponse *resp = data;

                //分享结果消息

                UMSocialLogInfo(@"response message is %@",resp.message);

                //第三方原始返回的数据

                UMSocialLogInfo(@"response originalResponse data is %@",resp.originalResponse);

            }

            else

            {

                UMSocialLogInfo(@"response data is %@",data);

            }

        }

        [self alertWithError:error];

    }];

  }

- (void)alertWithError:(NSError *)error

{

    NSString *result = nil;

    if (!error) {

        result = [NSString stringWithFormat:@"分享成功"];

    }else{

        NSMutableString *str = [NSMutableString string];

        if (error.userInfo) {

            for (NSString *key in error.userInfo) {

                [str appendFormat:@"%@ = %@ ", key, error.userInfo[key]];

            }

        }

        if (error) {

            result = [NSString stringWithFormat:@"Share fail with error code: %d %@",(int)error.code, str];

        }

        else{

            result = [NSString stringWithFormat:@"分享失败"];

        }

    }

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"分享"

                                                    message:result

                                                   delegate:nil

                                          cancelButtonTitle:@"确定"

                                          otherButtonTitles:nil];

    [alert show];

}

4.自定义button:

- (id)initWithFrame:(CGRect)frame

{

    self = [super initWithFrame:frame];

    if (self) {

        self.titleLabel.textAlignment = NSTextAlignmentCenter;

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

//        [self setTitleColor:[UIColor colorWithRed:0.44f green:0.44f blue:0.44f alpha:1.00f] forState:UIControlStateNormal];

        

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

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

    }

    return self;

}

 -(void)layoutSubviews {

    [super layoutSubviews];

    

    self.titleLabel.textAlignment = NSTextAlignmentCenter;

    

    self.imageView.frame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.width);

    

    self.imageView.layer.cornerRadius = self.frame.size.width/2;

    

    self.imageView.backgroundColor = [UIColor whiteColor];

    

    self.imageView.clipsToBounds = YES;

    

    self.titleLabel.frame = CGRectMake(0, CGRectGetMaxY(self.imageView.frame), self.frame.size.width, self.frame.size.height-self.frame.size.width);

}

 最终显示样式:

 

原文地址:https://www.cnblogs.com/niumingming920322/p/6202511.html