1.使用cocoapods导入友盟SDK
pod 'UMCAnalytics', '~> 5.5.2'
# 集成微信(精简版0.2M)
pod 'UMCShare/Social/ReducedWeChat', '~> 6.9.5'
2.在APPDelegate中的didFinishLaunchingWithOptions配置友盟key和微信key
- (void)configUM {
[UMConfigure initWithAppkey:@"aaa" channel:@"App Store"];
[UMConfigure setLogEnabled:YES];
/* 设置微信的appKey和appSecret */
[[UMSocialManager defaultManager] setPlaform:UMSocialPlatformType_WechatSession appKey:@"sss" appSecret:@"sss" redirectURL:@"http:www.baicu.com"];
}
3.在工程的TARGETS-Info-URL Types添加微信的schemes
4.自定义分享面板(也可使用友盟的分享面板UI)
#import
#import "InviteDriverModel.h"
@protocol UMSharePanelViewDelegate
@end
@interface UMSharePanelView : UIView
@property (nonatomic, weak) id delegate;
@property (nonatomic, strong) InviteDriverModel *inviteDriverModel;
- (void)showInView:(UIView *)view inviteDriverModel:(InviteDriverModel *)inviteDriverModel;
- (void)dismiss;
@end
#import "UMSharePanelView.h"
@interface UMSharePanelView ()
{
UIView *contenView;
}
@end
@implementation UMSharePanelView
- (instancetype)init
{
self = [super init];
if (self) {
[self initView];
}
return self;
}
- (void)initView {
self.frame = ScreenBounds;
self.backgroundColor = Color_Background_Alpha;
CGFloat height = IS_iPhoneX ? 190 : 173;
contenView = [[UIView alloc] init];
contenView.backgroundColor = [UIColor whiteColor];
[self addSubview:contenView];
[contenView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.right.mas_equalTo(self);
make.height.mas_equalTo(height);
make.bottom.mas_equalTo(self).offset(250);
}];
UIButton *wxButton = [[UIButton alloc] init];
[wxButton setImage:[UIImage imageNamed:@"ico_wx"] forState:UIControlStateNormal];
[wxButton addTarget:self action:@selector(wxButtonAction) forControlEvents:UIControlEventTouchUpInside];
[contenView addSubview:wxButton];
[wxButton mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.mas_equalTo(self->contenView).offset(98 * ScaleWidth);
make.top.mas_equalTo(self->contenView).offset(20);
make.width.height.mas_equalTo(48);
}];
UILabel *wxLabel = [[UILabel alloc] init];
wxLabel.font = [UIFont systemFontOfSize:12];
wxLabel.text = @"微信";
[contenView addSubview:wxLabel];
[wxLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.mas_equalTo(wxButton.mas_bottom).offset(18);
make.centerX.mas_equalTo(wxButton);
}];
UIButton *pyqButton = [[UIButton alloc] init];
[pyqButton setImage:[UIImage imageNamed:@"ico_pyq"] forState:UIControlStateNormal];
[pyqButton addTarget:self action:@selector(pyqButtonAction) forControlEvents:UIControlEventTouchUpInside];
[contenView addSubview:pyqButton];
[pyqButton mas_makeConstraints:^(MASConstraintMaker *make) {
make.right.mas_equalTo(self->contenView).offset(-98 * ScaleWidth);
make.top.mas_equalTo(self->contenView).offset(20);
make.width.height.mas_equalTo(48);
}];
UILabel *pyqLabel = [[UILabel alloc] init];
pyqLabel.font = [UIFont systemFontOfSize:12];
pyqLabel.text = @"朋友圈";
[contenView addSubview:pyqLabel];
[pyqLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.mas_equalTo(pyqButton.mas_bottom).offset(18);
make.centerX.mas_equalTo(pyqButton);
}];
UIView *line = [[UIView alloc] init];
line.backgroundColor = HEXCOLOR(@"#F3F4F5");
[contenView addSubview:line];
[line mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.mas_equalTo(wxLabel.mas_bottom).offset(20);
make.left.right.mas_equalTo(self->contenView);
make.height.mas_equalTo(1);
}];
UIButton *cancelButton = [[UIButton alloc] init];
cancelButton.titleLabel.font = [UIFont systemFontOfSize:14];
[cancelButton setTitle:@"取消" forState:UIControlStateNormal];
[cancelButton setTitleColor:[UIColor darkTextColor] forState:UIControlStateNormal];
[cancelButton addTarget:self action:@selector(cancelButtonAction) forControlEvents:UIControlEventTouchUpInside];
[contenView addSubview:cancelButton];
[cancelButton mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.mas_equalTo(line.mas_bottom);
make.left.right.mas_equalTo(self->contenView);
make.height.mas_equalTo(50);
}];
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(dismiss)];
[self addGestureRecognizer:tap];
UITapGestureRecognizer *tap1 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction)];
[contenView addGestureRecognizer:tap1];
}
- (void)showInView:(UIView *)view inviteDriverModel:(InviteDriverModel *)inviteDriverModel {
self.backgroundColor = [UIColor clearColor];
[view addSubview:self];
[self layoutIfNeeded];
self.inviteDriverModel = inviteDriverModel;
__weak typeof(self) weakSelf = self;
[UIView animateWithDuration:TimeInterval_Animation animations:^{
[self->contenView mas_updateConstraints:^(MASConstraintMaker *make) {
make.bottom.mas_equalTo(self).offset(0);
}];
weakSelf.backgroundColor = Color_Background_Alpha;
[self layoutIfNeeded];
}];
}
- (void)dismiss {
__weak typeof(self) weakSelf = self;
[UIView animateWithDuration:TimeInterval_Animation animations:^{
[self->contenView mas_updateConstraints:^(MASConstraintMaker *make) {
make.bottom.mas_equalTo(self).offset(250);
}];
weakSelf.backgroundColor = [UIColor clearColor];
[self layoutIfNeeded];
} completion:^(BOOL finished) {
[weakSelf removeFromSuperview];
}];
}
#pragma mark - action
- (void)wxButtonAction {
[UMShareManager.shared shareInviteDriverToWeChatWithTitle:self.inviteDriverModel.shareTitle
description:self.inviteDriverModel.shareContent
icon:self.inviteDriverModel.sharePicUrl
url:self.inviteDriverModel.hyperlink];
[self dismiss];
}
- (void)pyqButtonAction {
[UMShareManager.shared shareInviteDriverToWeChatPYQWithTitle:self.inviteDriverModel.shareTitle
description:self.inviteDriverModel.shareContent
icon:self.inviteDriverModel.sharePicUrl
url:self.inviteDriverModel.hyperlink];
[self dismiss];
}
- (void)cancelButtonAction {
[self dismiss];
}
- (void)tapAction {
}
@end
5.调用分享SDK
//调用示例
[UMShareManager.shared shareInviteDriverToWeChatWithTitle:shareTitle
description:shareContent
icon:sharePicUrl
url:hyperlink];
#import
#import
@interface UMShareManager : NSObject
+ (instancetype)shared;
- (void)shareInviteDriverToWeChatWithTitle:(NSString *)title
description:(NSString *)description
icon:(NSString *)icon
url:(NSString *)url;
- (void)shareInviteDriverToWeChatPYQWithTitle:(NSString *)title
description:(NSString *)description
icon:(NSString *)icon
url:(NSString *)url;
@end
#import "UMShareManager.h"
@implementation UMShareManager
+ (id)shared {
static dispatch_once_t once;
static id instance;
dispatch_once(&once, ^{
instance = [self new];
});
return instance;
}
//分享司机邀请链接
- (void)shareInviteDriverToWeChatWithTitle:(NSString *)title
description:(NSString *)description
icon:(NSString *)icon
url:(NSString *)url {
[self shareWebPageToPlatformType:UMSocialPlatformType_WechatSession
thumbURL:icon
title:title
description:description
webpageUrl:url
];
}
- (void)shareInviteDriverToWeChatPYQWithTitle:(NSString *)title
description:(NSString *)description
icon:(NSString *)icon
url:(NSString *)url {
[self shareWebPageToPlatformType:UMSocialPlatformType_WechatTimeLine
thumbURL:icon
title:title
description:description
webpageUrl:url
];
}
//分享到微信
- (void)shareWebPageToPlatformType:(UMSocialPlatformType)platformType
thumbURL:(NSString *)thumbURL
title:(NSString *)title
description:(NSString *)description
webpageUrl:(NSString *)webpageUrl {
[self shareWebPageToPlatformType:platformType
thumbURL:thumbURL
title:title
description:description
webpageUrl:webpageUrl
completion:^{
}];
}
- (void)shareWebPageToPlatformType:(UMSocialPlatformType)platformType
thumbURL:(NSString *)thumbURL
title:(NSString *)title
description:(NSString *)description
webpageUrl:(NSString *)webpageUrl
completion:(void(^)(void))block {
//创建分享消息对象
UMSocialMessageObject *messageObject = [UMSocialMessageObject messageObject];
//创建网页内容对象
UMShareWebpageObject *shareObject = [UMShareWebpageObject shareObjectWithTitle:title descr:description thumImage:thumbURL];
//设置网页地址
shareObject.webpageUrl = webpageUrl;
//分享消息对象设置分享内容对象
messageObject.shareObject = shareObject;
//调用分享接口
[[UMSocialManager defaultManager] shareToPlatform:platformType messageObject:messageObject currentViewController:nil completion:^(id data, NSError *error) {
block();
if (error) {
[SVProgressHUD showErrorWithStatus:@"分享失败"];
[SVProgressHUD dismissWithDelay:2];
NSLog(@"************Share fail with error %@*********",error);
} else {
[SVProgressHUD showSuccessWithStatus:@"分享成功"];
[SVProgressHUD dismissWithDelay:2];
if ([data isKindOfClass:[UMSocialShareResponse class]]) {
UMSocialShareResponse *resp = data;
//分享结果消息
NSLog(@"response message is %@",resp.message);
//第三方原始返回的数据
NSLog(@"response originalResponse data is %@",resp.originalResponse);
}else{
NSLog(@"response data is %@",data);
}
}
}];
}
@end