// // XCShareView.m // XCRoomMoudle // // Created by KevinWang on 2018/9/2. // Copyright © 2018年 YiZhuan. All rights reserved. // #import "XPShareView.h" ///Third #import #import #import #import #import "XCCurrentVCStackManager.h" ///Tool #import "TTPopup.h" ///View #import "XPShareItemCell.h" #import "XPMineShareViewController.h" #import "ClientConfig.h" @interface XPShareView() ///取消 @property (nonatomic, strong) UIButton *cancleButton; ///列表 @property (nonatomic, strong) UICollectionView *collectionView; ///数据源 @property (nonatomic, strong) NSArray *items; ///item的大小 @property (nonatomic,assign) CGSize itemSize; ///分享的内容 @property (nonatomic,strong) XPShareInfoModel *shareInfo; @end @implementation XPShareView #pragma mark - Life Style - (instancetype)initWithItems:(NSArray *)items itemSize:(CGSize)itemSize shareInfo:(XPShareInfoModel *)shareInfo { if (self = [super init]) { for (XPShareItem * item in items) { if (item.type == XPShareItemTagAppFriends || item.type == XPShareItemTagCopyLink) { item.disable = YES; } else { item.disable = [self isInstallClient:[self getSharePlatformType:item.type]]; } } self.items = [NSMutableArray arrayWithArray:items]; 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.leading.trailing.mas_equalTo(self).inset(15); }]; [self.cancleButton mas_makeConstraints:^(MASConstraintMaker *make) { make.height.mas_equalTo(45); make.leading.trailing.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 xpSafeObjectAtIndex: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 : @""; if ([urlString containsString:@"?"]){ urlString = [NSString stringWithFormat:@"%@&lang=%@",urlString,[NSBundle uploadLanguageText]]; }else{ urlString = [NSString stringWithFormat:@"%@?lang=%@",urlString,[NSBundle uploadLanguageText]]; } NSString *encodedUrl = [urlString stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]]; XPShareItem * item = [self.items xpSafeObjectAtIndex:indexPath.item]; if (item == nil){ [TTPopup dismiss]; return; }; if (item.type == XPShareItemTagAppSaveAlbum){ [TTPopup dismiss]; if (self.delegate && [self.delegate respondsToSelector:@selector(shareView:savePhoto:)]){ [self.delegate shareView:self savePhoto:self.shareInfo]; } 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; } if([self isInstallClient:[self getSharePlatformType:item.type]] == NO){ [XNDJTDDLoadingTool showErrorWithMessage:YMLocalizedString(@"XPShareView9")]; [TTPopup dismiss]; return; } // 添加类型安全检查,防止NSTaggedPointerString错误 if ([self.shareInfo isKindOfClass:[XPShareInfoModel class]] && [item isKindOfClass:[XPShareItem class]]) { self.shareInfo.shareType = item.type; } else { NSLog(@"警告:self.shareInfo不是XPShareInfoModel类型,而是%@类型", NSStringFromClass([self.shareInfo class])); [TTPopup dismiss]; return; } SSDKPlatformType platformType = SSDKPlatformTypeCopy; 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]; } return; } 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.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 _Nonnull)sharer didCompleteWithResults:(NSDictionary * _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.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 _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 _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) { MSBaseRTLFlowLayout *layout = [[MSBaseRTLFlowLayout 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