341 lines
14 KiB
Objective-C
341 lines
14 KiB
Objective-C
//
|
||
// XCShareView.m
|
||
// XCRoomMoudle
|
||
//
|
||
// Created by KevinWang on 2018/9/2.
|
||
// Copyright © 2018年 YiZhuan. All rights reserved.
|
||
//
|
||
|
||
#import "XPShareView.h"
|
||
///Third
|
||
#import <Masonry/Masonry.h>
|
||
#import <ShareSDK/ShareSDK.h>
|
||
#import <ShareSDKExtension/ShareSDK+Extension.h>
|
||
#import <FBSDKShareKit/FBSDKShareKit.h>
|
||
#import "XCCurrentVCStackManager.h"
|
||
///Tool
|
||
#import "XCCurrentVCStackManager.h"
|
||
#import "TTPopup.h"
|
||
#import "XPFirebbaseEvent.h"
|
||
///View
|
||
#import "XPShareItemCell.h"
|
||
#import "XPMineShareViewController.h"
|
||
#import "ClientConfig.h"
|
||
|
||
@interface XPShareView()<UICollectionViewDataSource,UICollectionViewDelegate,FBSDKSharingDelegate>
|
||
///取消
|
||
@property (nonatomic, strong) UIButton *cancleButton;
|
||
///列表
|
||
@property (nonatomic, strong) UICollectionView *collectionView;
|
||
///数据源
|
||
@property (nonatomic, strong) NSArray<XPShareItem *> *items;
|
||
///item的大小
|
||
@property (nonatomic,assign) CGSize itemSize;
|
||
///分享的内容
|
||
@property (nonatomic,strong) XPShareInfoModel *shareInfo;
|
||
@end
|
||
|
||
@implementation XPShareView
|
||
|
||
#pragma mark - Life Style
|
||
- (instancetype)initWithItems:(NSArray<XPShareItem *> *)items itemSize:(CGSize)itemSize shareInfo:(XPShareInfoModel *)shareInfo {
|
||
if (self = [super init]) {
|
||
|
||
NSMutableArray *itemList = [NSMutableArray array];
|
||
for (XPShareItem * item in items) {
|
||
if (item.type == XPShareItemTagAppFriends || item.type == XPShareItemTagCopyLink) {
|
||
item.disable = YES;
|
||
} else {
|
||
item.disable = [self isInstallClient:[self getSharePlatformType:item.type]];
|
||
}
|
||
if(item.disable == YES){
|
||
[itemList addObject:item];
|
||
}
|
||
}
|
||
if([ClientConfig shareConfig].canOpen){
|
||
self.items = [NSMutableArray arrayWithArray:items];
|
||
}else{
|
||
self.items = itemList;
|
||
}
|
||
|
||
self.itemSize =itemSize;
|
||
self.shareInfo = shareInfo;
|
||
[self initSubViews];
|
||
[self initSubViewConstraints];
|
||
}
|
||
return self;
|
||
}
|
||
|
||
#pragma mark - Private Method
|
||
- (void)initSubViews {
|
||
[self addSubview:self.collectionView];
|
||
[self addSubview:self.cancleButton];
|
||
}
|
||
|
||
- (void)initSubViewConstraints {
|
||
|
||
CGFloat collectionWidth = KScreenWidth - 15 * 2;
|
||
///一行有几个
|
||
int numberLine = collectionWidth / self.itemSize.width;
|
||
int page = self.items.count % numberLine > 0 ? (int)self.items.count / numberLine + 1 : (int)self.items.count / numberLine;
|
||
CGFloat collectionHeight = page * self.itemSize.height + 20 + (page-1) * 10 + 10;
|
||
[self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
|
||
make.top.mas_equalTo(self);
|
||
make.height.mas_equalTo(collectionHeight);
|
||
make.left.right.mas_equalTo(self).inset(15);
|
||
}];
|
||
|
||
[self.cancleButton mas_makeConstraints:^(MASConstraintMaker *make) {
|
||
make.height.mas_equalTo(45);
|
||
make.left.right.mas_equalTo(self.collectionView);
|
||
make.top.mas_equalTo(self.collectionView.mas_bottom).offset(15);
|
||
}];
|
||
|
||
[self mas_makeConstraints:^(MASConstraintMaker *make) {
|
||
make.width.mas_equalTo(KScreenWidth);
|
||
make.bottom.mas_equalTo(self.cancleButton.mas_bottom).offset(30);
|
||
}];
|
||
}
|
||
|
||
|
||
- (BOOL)isInstallClient:(SSDKPlatformType)platform {
|
||
return [ShareSDK isClientInstalled:platform];
|
||
}
|
||
|
||
- (SSDKPlatformType)getSharePlatformType:(XPShareItemTag)itemTag {
|
||
SSDKPlatformType type;
|
||
switch (itemTag) {
|
||
case XPShareItemTagFaceBook:
|
||
type = SSDKPlatformTypeFacebook;
|
||
break;
|
||
case XPShareItemTagLine:
|
||
type = SSDKPlatformTypeLine;
|
||
break;
|
||
default:
|
||
type = SSDKPlatformTypeUnknown;
|
||
break;
|
||
}
|
||
return type;
|
||
}
|
||
|
||
#pragma mark - UICollectionViewDelegate
|
||
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
|
||
return self.items.count;
|
||
}
|
||
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
|
||
XPShareItemCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([XPShareItemCell class]) forIndexPath:indexPath];
|
||
XPShareItem * item = [self.items safeObjectAtIndex1:indexPath.item];
|
||
item.disable = YES;
|
||
cell.shareItem = item;
|
||
return cell;
|
||
}
|
||
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
|
||
[collectionView deselectItemAtIndexPath:indexPath animated:YES];
|
||
|
||
NSMutableDictionary *shareParams = [NSMutableDictionary dictionary];
|
||
NSString * title = [self.shareInfo shareTitle].length > 0 ? self.shareInfo.shareTitle : @"";
|
||
NSString * content = self.shareInfo.shareContent.length > 0 ? self.shareInfo.shareContent : @"";
|
||
NSString * urlString = self.shareInfo.shareUrl.length > 0 ?self.shareInfo.shareUrl : @"";
|
||
NSString *encodedUrl = [urlString stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
|
||
|
||
XPShareItem * item = [self.items safeObjectAtIndex1:indexPath.item];
|
||
if (item == nil){
|
||
[TTPopup dismiss];
|
||
return;
|
||
};
|
||
|
||
if([self isInstallClient:[self getSharePlatformType:item.type]] == NO){
|
||
[XNDJTDDLoadingTool showErrorWithMessage:YMLocalizedString(@"XPShareView9")];
|
||
[TTPopup dismiss];
|
||
return;
|
||
}
|
||
if (item.type == XPShareItemTagAppFriends) {
|
||
[TTPopup dismiss];
|
||
XPMineShareViewController * shareVC = [[XPMineShareViewController alloc] init];
|
||
shareVC.shareType = MineShareType_Monents;
|
||
shareVC.shareInfo = self.shareInfo;
|
||
[[XCCurrentVCStackManager shareManager].getCurrentVC.navigationController pushViewController:shareVC animated:YES];
|
||
return;
|
||
} else if(item.type == XPShareItemTagCopyLink) {
|
||
NSString * urlString = self.shareInfo.shareUrl;
|
||
UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
|
||
[pasteboard setString:urlString];
|
||
[XNDJTDDLoadingTool showSuccessWithMessage:YMLocalizedString(@"XPShareView0")];
|
||
|
||
[TTPopup dismiss];
|
||
return;
|
||
}
|
||
|
||
|
||
self.shareInfo.shareType = item.type;
|
||
|
||
SSDKPlatformType platformType;
|
||
XPPlatformType FBtype;
|
||
if (item.type == XPShareItemTagLine) {
|
||
title = YMLocalizedString(@"XPShareView1");
|
||
platformType = SSDKPlatformTypeLine;
|
||
if (![ShareSDK isClientInstalled:platformType]) {
|
||
[XNDJTDDLoadingTool showErrorWithMessage:YMLocalizedString(@"XPShareView2")];
|
||
return;
|
||
}
|
||
NSString*contentKey= [encodedUrl stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet characterSetWithCharactersInString:@"`#%^{}\"[]|\\<> "].invertedSet];
|
||
NSString*contentType =@"text";
|
||
NSString*urlString = [NSString stringWithFormat:@"line://msg/%@/%@",contentType, contentKey];
|
||
|
||
[[UIApplication sharedApplication]openURL:[NSURL URLWithString:urlString] options:@{} completionHandler:^(BOOL success) {
|
||
|
||
}];
|
||
|
||
if (self.delegate && [self.delegate respondsToSelector:@selector(shareView:didSuccess:)]) {
|
||
[self.delegate shareView:self didSuccess:self.shareInfo];
|
||
}
|
||
|
||
// FBtype = PlatformOfLine;
|
||
// [shareParams SSDKSetupLineParamsByText:encodedUrl image:nil type:SSDKContentTypeAuto];
|
||
} else {
|
||
title = YMLocalizedString(@"XPShareView3");
|
||
platformType = SSDKPlatformTypeFacebook;
|
||
FBtype = PlatformOfFB;
|
||
content = YMLocalizedString(@"XPShareView4");
|
||
[shareParams SSDKSetupFacebookParamsByText:title image:@"https://pic.lecheng163.com/ananLogo256.png" url:[NSURL URLWithString:urlString] urlTitle:@"title" urlName:@"name" attachementUrl:nil hashtag:@"#Piko" quote:@"Piko" shareType:SSDKFacebookShareTypeNative type:SSDKContentTypeWebPage];
|
||
}
|
||
if(item.type == XPShareItemTagFaceBook){
|
||
FBSDKShareLinkContent*linkContent = [[FBSDKShareLinkContent alloc]init];
|
||
urlString = [urlString stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLFragmentAllowedCharacterSet]];
|
||
linkContent.contentURL= [NSURL URLWithString:urlString];
|
||
linkContent.quote = content;
|
||
|
||
FBSDKShareDialog *shareDialog = [[FBSDKShareDialog alloc]initWithViewController:[XCCurrentVCStackManager shareManager].getCurrentVC content:linkContent delegate:self];
|
||
// 需要指定模式,否则会调起web分享
|
||
shareDialog.mode = FBSDKShareDialogModeNative;
|
||
if (![shareDialog canShow]) {
|
||
shareDialog.mode = FBSDKShareDialogModeWeb;
|
||
}
|
||
|
||
|
||
[shareDialog show];
|
||
return;
|
||
}
|
||
|
||
[ShareSDK share:platformType parameters:shareParams onStateChanged:^(SSDKResponseState state, NSDictionary *userData, SSDKContentEntity *contentEntity, NSError *error) {
|
||
switch (state) {
|
||
case SSDKResponseStateSuccess:
|
||
{
|
||
if(self.isFromWebVeiw == NO){
|
||
[XPFirebbaseEvent shareEventWithContentID:@(self.shareInfo.roomUid).stringValue type:FBtype];
|
||
}else{
|
||
[XPFirebbaseEvent shareEventWithContentID:self.shareInfo.uid type:FBtype];
|
||
}
|
||
if (self.delegate && [self.delegate respondsToSelector:@selector(shareView:didSuccess:)]) {
|
||
[self.delegate shareView:self didSuccess:self.shareInfo];
|
||
}
|
||
}
|
||
break;
|
||
case SSDKResponseStateFail:
|
||
{
|
||
if (self.delegate && [self.delegate respondsToSelector:@selector(shareView:shareFail:)]) {
|
||
[self.delegate shareView:self shareFail:YMLocalizedString(@"XPShareView5")];
|
||
}
|
||
}
|
||
break;
|
||
case SSDKResponseStateCancel:
|
||
{
|
||
if (self.delegate && [self.delegate respondsToSelector:@selector(shareView:shareFail:)]) {
|
||
[self.delegate shareView:self shareFail:YMLocalizedString(@"XPShareView6")];
|
||
}
|
||
}
|
||
break;
|
||
default:
|
||
break;
|
||
}
|
||
}];
|
||
}
|
||
#pragma mark - FBSDKSharingDelegate
|
||
/// Sent to the delegate when sharing completes without error or cancellation.
|
||
/// @param sharer The sharer that completed.
|
||
/// @param results The results from the sharer. This may be nil or empty.
|
||
- (void)sharer:(id <FBSDKSharing> _Nonnull)sharer didCompleteWithResults:(NSDictionary<NSString *, id> * _Nonnull)results{
|
||
NSString *postId = results[@"postId"];
|
||
FBSDKShareDialog *dialog = (FBSDKShareDialog *)sharer;
|
||
if (dialog.mode == FBSDKShareDialogModeBrowser && (postId == nil || [postId isEqualToString:@""])) {
|
||
// 如果使用webview分享的,但postId是空的,
|
||
// 这种情况是用户点击了『完成』按钮,并没有真的分享
|
||
if (self.delegate && [self.delegate respondsToSelector:@selector(shareView:shareFail:)]) {
|
||
[self.delegate shareView:self shareFail:YMLocalizedString(@"XPShareView6")];
|
||
}
|
||
} else {
|
||
if(self.isFromWebVeiw == NO){
|
||
[XPFirebbaseEvent shareEventWithContentID:@(self.shareInfo.roomUid).stringValue type:PlatformOfFB];
|
||
}else{
|
||
[XPFirebbaseEvent shareEventWithContentID:self.shareInfo.uid type:PlatformOfFB];
|
||
}
|
||
if (self.delegate && [self.delegate respondsToSelector:@selector(shareView:didSuccess:)]) {
|
||
[self.delegate shareView:self didSuccess:self.shareInfo];
|
||
}
|
||
}
|
||
}
|
||
/// Sent to the delegate when the sharer encounters an error.
|
||
/// @param sharer The sharer that completed.
|
||
/// @param error The error.
|
||
- (void)sharer:(id <FBSDKSharing> _Nonnull)sharer didFailWithError:(NSError * _Nonnull)error{
|
||
FBSDKShareDialog *dialog = (FBSDKShareDialog *)sharer;
|
||
if (error == nil && dialog.mode == FBSDKShareDialogModeNative) {
|
||
// 如果使用原生登录失败,但error为空,那是因为用户没有安装Facebook app
|
||
// 重设dialog的mode,再次弹出对话框
|
||
dialog.mode = FBSDKShareDialogModeBrowser;
|
||
[dialog show];
|
||
} else {
|
||
if (self.delegate && [self.delegate respondsToSelector:@selector(shareView:shareFail:)]) {
|
||
[self.delegate shareView:self shareFail:YMLocalizedString(@"XPShareView5")];
|
||
}
|
||
}
|
||
}
|
||
/// Sent to the delegate when the sharer is cancelled.
|
||
/// @param sharer The sharer that completed.
|
||
- (void)sharerDidCancel:(id <FBSDKSharing> _Nonnull)sharer{
|
||
if (self.delegate && [self.delegate respondsToSelector:@selector(shareView:shareFail:)]) {
|
||
[self.delegate shareView:self shareFail:YMLocalizedString(@"XPShareView6")];
|
||
}
|
||
}
|
||
#pragma mark - Event Response
|
||
- (void)cancleButtonDidClck:(UIButton *)button{
|
||
if (self.delegate && [self.delegate respondsToSelector:@selector(shareViewDidClickCancel:)]) {
|
||
[self.delegate shareViewDidClickCancel:self];
|
||
}
|
||
}
|
||
|
||
#pragma mark - Getters And Setters
|
||
- (UICollectionView *)collectionView{
|
||
if (!_collectionView) {
|
||
UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
|
||
layout.itemSize = self.itemSize;
|
||
layout.minimumInteritemSpacing = 0;
|
||
layout.minimumLineSpacing = 10;
|
||
layout.sectionInset = UIEdgeInsetsMake(20, 0, 10, 0);
|
||
_collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
|
||
_collectionView.backgroundColor = [UIColor whiteColor];
|
||
_collectionView.dataSource = self;
|
||
_collectionView.delegate = self;
|
||
_collectionView.layer.masksToBounds = YES;
|
||
_collectionView.layer.cornerRadius = 15;
|
||
[_collectionView registerClass:[XPShareItemCell class] forCellWithReuseIdentifier:NSStringFromClass([XPShareItemCell class])];
|
||
}
|
||
return _collectionView;
|
||
}
|
||
|
||
- (UIButton *)cancleButton{
|
||
if (!_cancleButton) {
|
||
_cancleButton = [[UIButton alloc] init];
|
||
[_cancleButton setBackgroundColor:[UIColor whiteColor]];
|
||
[_cancleButton setTitle:YMLocalizedString(@"XPShareView7") forState:UIControlStateNormal];
|
||
_cancleButton.titleLabel.font = [UIFont fontWithName:@"PingFang-SC-Medium" size:15];
|
||
_cancleButton.layer.masksToBounds = YES;
|
||
_cancleButton.layer.cornerRadius = 45/2;
|
||
[_cancleButton setTitleColor:[DJDKMIMOMColor textThirdColor] forState:UIControlStateNormal];
|
||
[_cancleButton addTarget:self action:@selector(cancleButtonDidClck:) forControlEvents:UIControlEventTouchUpInside];
|
||
}
|
||
return _cancleButton;
|
||
}
|
||
|
||
@end
|