生活随笔
收集整理的這篇文章主要介紹了
iOS 友盟分享(微信)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
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 <UIKit/UIKit.h>
#import "InviteDriverModel.h"@protocol UMSharePanelViewDelegate <NSObject>@end@interface UMSharePanelView : UIView@property (nonatomic, weak) id<UMSharePanelViewDelegate> 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.shareTitledescription:self.inviteDriverModel.shareContenticon:self.inviteDriverModel.sharePicUrlurl:self.inviteDriverModel.hyperlink];[self dismiss];
}- (void)pyqButtonAction {[UMShareManager.shared shareInviteDriverToWeChatPYQWithTitle:self.inviteDriverModel.shareTitledescription:self.inviteDriverModel.shareContenticon:self.inviteDriverModel.sharePicUrlurl:self.inviteDriverModel.hyperlink];[self dismiss];
}- (void)cancelButtonAction {[self dismiss];
}- (void)tapAction {}@end
5.調用分享SDK
//調用示例[UMShareManager.shared shareInviteDriverToWeChatWithTitle:shareTitledescription:shareContenticon:sharePicUrlurl:hyperlink];
#import <Foundation/Foundation.h>
#import <UMShare/UMShare.h>@interface UMShareManager : NSObject+ (instancetype)shared;- (void)shareInviteDriverToWeChatWithTitle:(NSString *)titledescription:(NSString *)descriptionicon:(NSString *)iconurl:(NSString *)url;- (void)shareInviteDriverToWeChatPYQWithTitle:(NSString *)titledescription:(NSString *)descriptionicon:(NSString *)iconurl:(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 *)titledescription:(NSString *)descriptionicon:(NSString *)iconurl:(NSString *)url {[self shareWebPageToPlatformType:UMSocialPlatformType_WechatSessionthumbURL:icontitle:titledescription:descriptionwebpageUrl:url];
}- (void)shareInviteDriverToWeChatPYQWithTitle:(NSString *)titledescription:(NSString *)descriptionicon:(NSString *)iconurl:(NSString *)url {[self shareWebPageToPlatformType:UMSocialPlatformType_WechatTimeLinethumbURL:icontitle:titledescription:descriptionwebpageUrl:url];
}//分享到微信
- (void)shareWebPageToPlatformType:(UMSocialPlatformType)platformTypethumbURL:(NSString *)thumbURLtitle:(NSString *)titledescription:(NSString *)descriptionwebpageUrl:(NSString *)webpageUrl {[self shareWebPageToPlatformType:platformTypethumbURL:thumbURLtitle:titledescription:descriptionwebpageUrl:webpageUrlcompletion:^{}];
}- (void)shareWebPageToPlatformType:(UMSocialPlatformType)platformTypethumbURL:(NSString *)thumbURLtitle:(NSString *)titledescription:(NSString *)descriptionwebpageUrl:(NSString *)webpageUrlcompletion:(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
總結
以上是生活随笔為你收集整理的iOS 友盟分享(微信)的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。