123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524 |
- //
- // 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 <Social/Social.h>
- #import "MoveViewController.h"
- #import "ShareListVC.h"
- @interface DownFileViewController ()<WKNavigationDelegate,WKUIDelegate,NoteBookShareVCDelegate>
- //@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:
- @"<HTML>"
- "<head>"
- "<title>Text View</title>"
- "</head>"
- "<BODY>"
- "<pre>"
- "%@"
- "</pre>"
- "</BODY>"
- "</HTML>",
- 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
|