// // DownFileViewController.m // smartRhino // // Created by tederen on 2019/11/8. // Copyright © 2019 tederen. All rights reserved. // #import "DownFileViewController.h" #import "NoteBookShareVC.h" #import "SendInfoGroupVC.h" #import "DrawerView.h" #import "TabBarController.h" #import "MyTDGroupViewController.h" #import "FavoritesViewController.h" #import "MyTDTopicViewController.h" #import #import "MoveViewController.h" #import "ShareListVC.h" @interface DownFileViewController () //@property (nonatomic, strong) UIImageView *imagview; //@property (nonatomic, strong) UIView *labelView; @property (nonatomic, strong) UIView *myFooterView; @property (nonatomic, strong) TDButton *otherButton; @property (nonatomic, strong) TDButton *shareButton; @property (strong, nonatomic) NoteBookShareVC * noteBookShareVC; @property (nonatomic, strong) WKWebView *webView; @property (nonatomic, strong) DrawerView *drawerView; @property(nonatomic,strong) UIDocumentInteractionController *documentInteractionController; // 分割 @property(nonatomic,strong) UIView *sepView; @property(nonatomic,strong) NSData *fileData; @end @implementation DownFileViewController - (void)viewDidLoad { [super viewDidLoad]; [self.view addSubview:self.myFooterView]; [self.myFooterView mas_makeConstraints:^(MASConstraintMaker *make) { make.left.right.mas_equalTo(self.view); make.height.mas_offset(50); if (@available(iOS 11.0, *)) { make.bottom.equalTo(self.view.mas_safeAreaLayoutGuideBottom); } else { make.bottom.equalTo(self.view.mas_bottom); } }]; [self.view addSubview:self.sepView]; [self.sepView mas_makeConstraints:^(MASConstraintMaker *make) { make.left.right.equalTo(self.view); make.bottom.equalTo(self.myFooterView.mas_top); make.height.mas_equalTo(6); }]; [self.view layoutIfNeeded]; [self.view addSubview:self.webView]; [self.webView mas_makeConstraints:^(MASConstraintMaker *make) { make.left.right.mas_equalTo(self.view); make.top.mas_equalTo(self.myNavigationBar.mas_bottom); make.bottom.equalTo(self.sepView.mas_top); }]; self.myNavigationBar.titleLabel.text = self.model.Title; [[UtilsTools getWindow] addSubview:self.noteBookShareVC.view]; NSString * url = [self.model.Url stringByRemovingPercentEncoding]; if ([url hasSuffix:@".txt"]) { NSString * body = [NSString stringWithContentsOfURL:[NSURL URLWithString:self.model.Url] encoding:NSUTF8StringEncoding error:nil]; NSString* responseStr = [NSString stringWithFormat: @"" "" "Text View" "" "" "
"
                                 "%@"
                                 "
" "" "", body]; [self.webView loadHTMLString:responseStr baseURL:nil]; }else{ [self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:self.model.Url]]]; } self.fileData = [NSData dataWithContentsOfURL:[NSURL URLWithString:url]]; } - (void)openFileWithURL:(NSURL *)URL { NSLog(@"now open %@",URL); if (URL) { // Initialize Document Interaction Controller self.documentInteractionController = [UIDocumentInteractionController interactionControllerWithURL:URL]; // Configure Document Interaction Controller self.documentInteractionController.delegate = self; // Preview File [self.documentInteractionController presentPreviewAnimated:YES]; } } #pragma mark - button hander - (void)otherButtonHander{ [self SystermOpenFile]; // [self shareBtClick]; } #pragma mark - Social 框架 - (void)share3{ // 判断服务是否可用 [SLComposeViewController isAvailableForServiceType:SLServiceTypeSinaWeibo]; if (![SLComposeViewController isAvailableForServiceType:SLServiceTypeSinaWeibo]) { NSLog(@"设置界面设置自己的账号"); return; } // 弹出分享内容输入界面 SLComposeViewController *cc = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeSinaWeibo]; [cc setInitialText:@"测试文字"]; // 初始化文字 [cc addImage:[UIImage imageNamed:@"zhujun"]]; // 配图 cc.completionHandler = ^(SLComposeViewControllerResult reulst) { if (reulst == SLComposeViewControllerResultDone) { NSLog(@"用户发送成功"); } else { NSLog(@"用户发送失败"); } }; [self presentViewController:cc animated:YES completion:nil]; // 额外设置 } - (void)shareBtClick { NSString *textToShare = @"同岑"; UIImage *imageToShare = [UIImage imageNamed:@"AppIcon"]; NSURL *urlToShare = [NSURL URLWithString:self.model.Url]; // 分享的图片不能为空 NSArray *activityItems = @[textToShare, imageToShare, urlToShare]; UIActivityViewController *activityVC = [[UIActivityViewController alloc]initWithActivityItems:activityItems applicationActivities:nil]; // 排除(UIActivityTypeAirDrop)AirDrop 共享、(UIActivityTypePostToFacebook)Facebook activityVC.excludedActivityTypes = @[UIActivityTypePostToFacebook, UIActivityTypeAirDrop]; [self presentViewController:activityVC animated:YES completion:nil]; // 通过block接收结果处理 UIActivityViewControllerCompletionWithItemsHandler completionHandler = ^(UIActivityType __nullable activityType, BOOL completed, NSArray * __nullable returnedItems, NSError * __nullable activityError){ if (completed) { // [self showAlertViewWithMsg:@"恭喜你,分享成功!"]; }else{ // [self showAlertViewWithMsg:@"很遗憾,分享失败!"]; } }; activityVC.completionWithItemsHandler = completionHandler; } #pragma mark - 调用系统其他app 打开文件 - (void)SystermOpenFile{ WS(weakSelf); NSURL *url = [NSURL URLWithString:self.model.Url]; NSURLRequest *request = [NSURLRequest requestWithURL:url]; AFHTTPSessionManager *manager = [AFHTTPSessionManager manager]; NSURLSessionDownloadTask *downloadTask = [manager downloadTaskWithRequest:request progress:^(NSProgress * _Nonnull downloadProgress) { NSLog(@"下载进度:%.0f%", downloadProgress.fractionCompleted * 100); } destination:^NSURL * _Nonnull(NSURL * _Nonnull targetPath, NSURLResponse * _Nonnull response) { NSString *downloadPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject]; //打开文件管理器 NSFileManager *fileManager = [NSFileManager defaultManager]; //创建Download目录 [fileManager createDirectoryAtPath:downloadPath withIntermediateDirectories:YES attributes:nil error:nil]; //拼接文件路径 NSString *filePath = [downloadPath stringByAppendingPathComponent:response.suggestedFilename]; //返回文件位置的URL路径 dispatch_async(dispatch_get_main_queue(), ^{ weakSelf.documentInteractionController = [UIDocumentInteractionController interactionControllerWithURL:[NSURL fileURLWithPath:filePath]]; [weakSelf.documentInteractionController setDelegate:weakSelf]; [weakSelf.documentInteractionController presentOpenInMenuFromRect:CGRectZero inView:weakSelf.view animated:YES]; }); return [NSURL fileURLWithPath:filePath]; } completionHandler:^(NSURLResponse * _Nonnull response, NSURL * _Nullable filePath, NSError * _Nullable error) { NSLog(@"下载完成"); }]; [downloadTask resume]; } #pragma mark - 调用系统 打开文件分享文件 - (void)systermShare{ NSString *shareTitle = @"分享的标题"; UIImage *shareImage = [UIImage imageNamed:@"me"]; NSURL *shareUrl = [NSURL URLWithString:@"https://www.jianshu.com/u/acdcce712303"]; NSArray *activityItems = @[shareTitle, shareImage, shareUrl]; // 必须要提供url 才会显示分享标签否则只显示图片 UIActivityViewController *activityVC = [[UIActivityViewController alloc] initWithActivityItems:activityItems applicationActivities:nil]; activityVC.excludedActivityTypes = [self excludetypes]; activityVC.completionWithItemsHandler = ^(UIActivityType _Nullable activityType, BOOL completed, NSArray * _Nullable returnedItems, NSError * _Nullable activityError) { NSLog(@"activityType: %@,\n completed: %d,\n returnedItems:%@,\n activityError:%@",activityType,completed,returnedItems,activityError); }; [self presentViewController:activityVC animated:YES completion:nil]; } -(NSArray *)excludetypes{ NSMutableArray *excludeTypesM = [NSMutableArray arrayWithArray:@[//UIActivityTypePostToFacebook, UIActivityTypePostToTwitter, UIActivityTypePostToWeibo, UIActivityTypeMessage, UIActivityTypeMail, UIActivityTypePrint, UIActivityTypeCopyToPasteboard, UIActivityTypeAssignToContact, UIActivityTypeSaveToCameraRoll, UIActivityTypeAddToReadingList, UIActivityTypePostToFlickr, UIActivityTypePostToVimeo, UIActivityTypePostToTencentWeibo, UIActivityTypeAirDrop, UIActivityTypeOpenInIBooks]]; if ([[UIDevice currentDevice].systemVersion floatValue] >= 11.0) { if (@available(iOS 11.0, *)) { [excludeTypesM addObject:UIActivityTypeMarkupAsPDF]; } else { // Fallback on earlier versions } } return excludeTypesM; } - (void)backButtonAction2:(UIButton *)sender { [self secondBarButtonItems]; } - (void)secondBarButtonItems{ [[UtilsTools getWindow] addSubview:self.drawerView]; WS(weakSelf); self.drawerView.SelectDrawerBlock = ^(NSIndexPath * _Nonnull indexPath) { if ([weakSelf.navigationController.viewControllers count] > 1) { [weakSelf.navigationController popToRootViewControllerAnimated:NO]; } if (indexPath.section == 0) { [[TabBarController sharedTabBarController] setSelectedIndex:indexPath.row]; }else{ ///@"站内信", @"通知", @"收藏",@"笔记", @"设置" switch (indexPath.row) { case 0: { [[TabBarController sharedTabBarController] setSelectedIndex:2]; } break; case 1: { [[TabBarController sharedTabBarController] setSelectedIndex:2]; } break; default: { [[TabBarController sharedTabBarController] setSelectedIndex:3]; } break; } dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ [[NSNotificationCenter defaultCenter] postNotificationName:DRAWERPUSHVC object:nil userInfo:@{VCINDEX:@(indexPath.row)}]; }); } }; self.drawerView.frame = CGRectMake(0,0, SCREEN_WIDTH, SCREEN_HEIGHT); } - (DrawerView *)drawerView { if(!_drawerView) { _drawerView = [[DrawerView alloc] init]; } return _drawerView; } - (TDButton *)otherButton{ if (!_otherButton) { _otherButton = [[TDButton alloc]init]; [_otherButton setTitle:@"其他应用打开" forState:UIControlStateNormal]; [_otherButton setTitleColor:UIColorHex(666666) forState:UIControlStateNormal]; [_otherButton setImage:IMG(@"更多") forState:UIControlStateNormal]; [_otherButton addTarget:self action:@selector(otherButtonHander) forControlEvents:UIControlEventTouchUpInside]; [[_otherButton titleLabel] setFont:[UIFont systemFontOfSize:15]]; [_otherButton layoutButtonWithEdgeInsetsStyle:GLButtonEdgeInsetsStyleTop imageTitleSpace:5]; } return _otherButton; } - (TDButton *)shareButton{ if (!_shareButton) { _shareButton = [[TDButton alloc]init]; [_shareButton setTitle:@"转发" forState:UIControlStateNormal]; [_shareButton setTitleColor:UIColorHex(666666) forState:UIControlStateNormal]; [_shareButton setImage:IMG(@"转发") forState:UIControlStateNormal]; [[_shareButton titleLabel] setFont:[UIFont systemFontOfSize:15]]; [_shareButton addTarget:self action:@selector(shareButtonHander) forControlEvents:UIControlEventTouchUpInside]; [_shareButton layoutButtonWithEdgeInsetsStyle:GLButtonEdgeInsetsStyleTop imageTitleSpace:10]; } return _shareButton; } - (UIView *)myFooterView{ if(!_myFooterView){ _myFooterView = [UIView new]; [_myFooterView addSubview:self.otherButton]; [_myFooterView addSubview:self.shareButton]; [self.otherButton mas_makeConstraints:^(MASConstraintMaker *make) { make.left.equalTo(self.myFooterView).offset(40); make.centerY.equalTo(self.myFooterView); make.width.mas_equalTo(120); make.height.mas_equalTo(50); }]; [self.shareButton mas_makeConstraints:^(MASConstraintMaker *make) { make.right.equalTo(self.myFooterView).offset(-20); make.centerY.equalTo(self.myFooterView); make.width.mas_equalTo(120); make.height.mas_equalTo(50); }]; } return _myFooterView; } - (WKWebView *)webView { if (!_webView) { // 进行配置控制器 WKWebViewConfiguration *configuration = [[WKWebViewConfiguration alloc] init]; // 实例化对象 configuration.userContentController = [WKUserContentController new]; // 调用JS方法 // [configuration.userContentController addScriptMessageHandler:self name:@"uploadPersonImage"]; //window.webkit.messageHandlers.uploadPersonImage.postMessage({body: 'goodsId=1212'}); js调用 // 进行偏好设置 WKPreferences *preferences = [WKPreferences new]; preferences.javaScriptEnabled = YES; preferences.javaScriptCanOpenWindowsAutomatically = YES; preferences.minimumFontSize = 40.0; configuration.preferences = preferences; _webView = [[WKWebView alloc] init]; // _webView = [[WKWebView alloc] initWithFrame:CGRectMake(0,kNavigationHeight,kGXScreenWidth,kGXScreenHeigh - kNavigationHeight - 71 - self.sepView.height) configuration:configuration]; _webView.navigationDelegate = self; _webView.opaque = NO; // _webView.backgroundColor = [UIColor whiteColor]; if (@available(ios 11.0,*)){ _webView.scrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;} } return _webView; } // 页面开始加载时调用 -(void)webView:(WKWebView *)webView didStartProvisionalNavigation:(WKNavigation *)navigation{ SHOWLOADING } // 当内容开始返回时调用 - (void)webView:(WKWebView *)webView didCommitNavigation:(WKNavigation *)navigation{ REMOVESHOW } // 页面加载完成之后调用 - (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation{//这里修改导航栏的标题,动态改变 REMOVESHOW } // 页面加载失败时调用 - (void)webView:(WKWebView *)webView didFailProvisionalNavigation:(WKNavigation *)navigation{ [webView reload]; } -(UIView *)sepView { if (!_sepView) { _sepView = [UIView new]; _sepView.backgroundColor = kColorFromRGB(0xeeeeee); } return _sepView; } #pragma mark -转发 - (void)shareButtonHander{ [self.noteBookShareVC initNoteBookShareData]; self.noteBookShareVC.view.hidden = !self.noteBookShareVC.view.hidden; } - (NoteBookShareVC *)noteBookShareVC{ if (_noteBookShareVC == nil) { _noteBookShareVC = [NoteBookShareVC initNoteBookShareVC]; [_noteBookShareVC.view setHidden:YES]; _noteBookShareVC.delegate = self; } return _noteBookShareVC; } -(void)userSelectType:(NSString *)typeName WithIndexPath:(NSIndexPath *)indexPath{ self.noteBookShareVC.view.hidden = !self.noteBookShareVC.view.hidden; if ([typeName isEqualToString:@"发给微信好友"]) { [self returnToWechatSession]; }else if ([typeName isEqualToString:@"发到朋友圈"]){ [self returnToWechatTimeLine]; }else if ([typeName isEqualToString:@"发到微博"]){ [self returnToSina]; }else if ([typeName isEqualToString:@"发给QQ好友"]){ [self returnToQQ]; }else if ([typeName isEqualToString:@"发到消息"]){ [self returnToMessage]; }else if ([typeName isEqualToString:@"发到小组"]){ [self returnToGroup]; }else if ([typeName isEqualToString:@"发到笔记"]){ [self returnToNote]; }else if ([typeName isEqualToString:@"发到话题"]){ [self returnToTopic]; }else{ } } - (void)returnToWechatSession { if (self.fileData) { [self shareWebPageToPlatformType:UMSocialPlatformType_WechatSession]; }else{ SHOWERROR(@"文件下载中"); } } - (void)returnToWechatTimeLine { if (self.fileData) { [self shareWebPageToPlatformType:UMSocialPlatformType_WechatTimeLine]; }else{ SHOWERROR(@"文件下载中"); } } - (void)returnToSina { if (self.fileData) { [self shareWebPageToPlatformType:UMSocialPlatformType_Sina]; }else{ SHOWERROR(@"文件下载中"); } } - (void)returnToQQ { if (self.fileData) { [self shareWebPageToPlatformType:UMSocialPlatformType_QQ]; }else{ SHOWERROR(@"文件下载中"); } } - (void)shareWebPageToPlatformType:(UMSocialPlatformType)platformType { //创建分享消息对象 UMSocialMessageObject *messageObject = [UMSocialMessageObject messageObject]; messageObject.title = self.model.Title; //创建文件内容对象 UMShareFileObject * fileObject = [UMShareFileObject shareObjectWithTitle:self.model.Title descr:@"" thumImage:[UIImage imageNamed:[ZYCTool getFileNameImage:self.model.Url]]]; fileObject.fileData = self.fileData; fileObject.fileExtension = [self.model.Url pathExtension]; //分享消息对象设置分享内容对象 messageObject.shareObject = fileObject; //调用分享接口 [[UMSocialManager defaultManager] shareToPlatform:platformType messageObject:messageObject currentViewController:self completion:^(id data, NSError *error) { if (error) { SHOWERROR([ZYCTool handerResultData:error]); NSLog(@"************Share fail with error %@*********",error); }else{ NSLog(@"response data is %@",data); } }]; } - (void)returnToMessage { ShareListVC * vc = [ShareListVC initShareListVC]; FlowAttachmentsModel * fModel = self.model; if (fModel.SoureTypeId != 301) { fModel.MinUrl = @""; } vc.sendModel = fModel; vc.isReturn = YES; [self.navigationController pushViewController:vc animated:YES]; } - (void)returnToGroup { MyTDGroupViewController * vc = [[MyTDGroupViewController alloc] init]; vc.type = CollectModel_Group; FlowAttachmentsModel * fModel = self.model; if (fModel.SoureTypeId != 301) { fModel.MinUrl = @""; } vc.sendModel = fModel; vc.isReturn = YES; [self.navigationController pushViewController:vc animated:YES]; } - (void)returnToNote { MyTDTopicCreateVC * vc = [MyTDTopicCreateVC initMyTDTopicCreateVC]; vc.type = CollectModel_NoteBook; FlowAttachmentsModel * fModel = self.model; if (fModel.SoureTypeId != 301) { fModel.MinUrl = @""; } vc.sendModel = fModel; vc.isReturn = YES; [self.navigationController pushViewController:vc animated:YES]; } - (void)returnToTopic { MyTDTopicCreateVC * vc = [MyTDTopicCreateVC initMyTDTopicCreateVC]; vc.type = CollectModel_NewTopic; FlowAttachmentsModel * fModel = self.model; if (fModel.SoureTypeId != 301) { fModel.MinUrl = @""; } vc.sendModel = fModel; vc.isReturn = YES; [self.navigationController pushViewController:vc animated:YES]; } @end