forked from coding/Coding-iOS
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLocalFileViewController.m
More file actions
144 lines (126 loc) · 5.77 KB
/
LocalFileViewController.m
File metadata and controls
144 lines (126 loc) · 5.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
//
// LocalFileViewController.m
// Coding_iOS
//
// Created by Ease on 15/9/22.
// Copyright © 2015年 Coding. All rights reserved.
//
#import <QuickLook/QuickLook.h>
#import "LocalFileViewController.h"
#import "Coding_NetAPIManager.h"
#import "WebContentManager.h"
#import "BasicPreviewItem.h"
@interface LocalFileViewController ()<QLPreviewControllerDataSource, QLPreviewControllerDelegate, UIDocumentInteractionControllerDelegate, UIWebViewDelegate>
@property (strong, nonatomic) QLPreviewController *previewController;
@property (nonatomic, strong) UIDocumentInteractionController *docInteractionController;
@property (strong, nonatomic) UIWebView *contentWebView;
@property (strong, nonatomic) UIActivityIndicatorView *activityIndicator;
@property (strong, nonatomic) NSString *fileName, *fileType;
@end
@implementation LocalFileViewController
- (void)setFileUrl:(NSURL *)fileUrl{
_fileUrl = fileUrl;
if (_fileUrl.path.length > 0) {
NSArray *valueList = [[[self.fileUrl.path componentsSeparatedByString:@"/"] lastObject] componentsSeparatedByString:@"|||"];
_fileName = valueList[0];
_fileType = [[[_fileName componentsSeparatedByString:@"."] lastObject] lowercaseString];
}
}
- (UIDocumentInteractionController *)docInteractionController{
if (_docInteractionController == nil){
_docInteractionController = [UIDocumentInteractionController interactionControllerWithURL:self.fileUrl];
}
return _docInteractionController;
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.title = self.fileName;
[self.navigationItem setRightBarButtonItem:[[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"share_Nav"] style:UIBarButtonItemStylePlain target:self action:@selector(rightNavBtnClicked)] animated:NO];
if ([self.fileType isEqualToString:@"md"]
|| [self.fileType isEqualToString:@"html"]
|| [self.fileType isEqualToString:@"txt"]
|| [self.fileType isEqualToString:@"plist"]){
[self loadWebView:self.fileUrl];
}else if ([QLPreviewController canPreviewItem:self.fileUrl]) {
[self showDiskFile:self.fileUrl];
}else {
[self.view configBlankPage:EaseBlankPageTypeFileTypeCannotSupport hasData:NO hasError:NO reloadButtonBlock:nil];
}
}
- (void)rightNavBtnClicked{
[self openByOtherApp];
}
- (void)openByOtherApp{
[self.docInteractionController presentOpenInMenuFromBarButtonItem:self.navigationItem.rightBarButtonItem animated:YES];
}
#pragma mark HandleData
- (void)loadWebView:(NSURL *)fileUrl{
self.previewController.view.hidden = YES;
if (!_contentWebView) {
//用webView显示内容
_contentWebView = [[UIWebView alloc] initWithFrame:self.view.bounds];
_contentWebView.delegate = self;
_contentWebView.backgroundColor = [UIColor clearColor];
_contentWebView.opaque = NO;
_contentWebView.scalesPageToFit = YES;
//webview加载指示
_activityIndicator = [[UIActivityIndicatorView alloc]
initWithActivityIndicatorStyle:
UIActivityIndicatorViewStyleGray];
_activityIndicator.hidesWhenStopped = YES;
[_activityIndicator setCenter:CGPointMake(CGRectGetWidth(_contentWebView.frame)/2, CGRectGetHeight(_contentWebView.frame)/2)];
[_contentWebView addSubview:_activityIndicator];
[self.view addSubview:_contentWebView];
[_contentWebView mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.left.right.bottom.equalTo(self.view);
}];
}
if ([self.fileType isEqualToString:@"md"]){
NSString *mdStr = [NSString stringWithContentsOfURL:fileUrl encoding:NSUTF8StringEncoding error:nil];
[self.activityIndicator startAnimating];
[[Coding_NetAPIManager sharedManager] request_MDHtmlStr_WithMDStr:mdStr inProject:nil andBlock:^(id data, NSError *error) {
[self.activityIndicator stopAnimating];
NSString *htmlStr;
htmlStr = data? data: @"加载失败";
NSString *contentStr = [WebContentManager markdownPatternedWithContent:htmlStr];
[self.contentWebView loadHTMLString:contentStr baseURL:nil];
}];
}else if ([self.fileType isEqualToString:@"html"]){
NSString* htmlString = [NSString stringWithContentsOfURL:fileUrl encoding:NSUTF8StringEncoding error:nil];
[self.contentWebView loadHTMLString:htmlString baseURL:nil];
}else if ([self.fileType isEqualToString:@"plist"]
|| [self.fileType isEqualToString:@"txt"]){
NSData *fileData = [NSData dataWithContentsOfURL:fileUrl];
[self.contentWebView loadData:fileData MIMEType:@"text/text" textEncodingName:@"UTF-8" baseURL:fileUrl];
}else{
[self.contentWebView loadRequest:[NSURLRequest requestWithURL:fileUrl]];
}
self.contentWebView.hidden = NO;
}
- (void)showDiskFile:(NSURL *)fileUrl{
self.contentWebView.hidden = YES;
if (!self.previewController) {
QLPreviewController* preview = [[QLPreviewController alloc] init];
preview.dataSource = self;
preview.delegate = self;
[self.view addSubview:preview.view];
[preview.view mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.left.right.bottom.equalTo(self.view);
}];
self.previewController = preview;
}
self.previewController.view.hidden = NO;
}
#pragma mark - QLPreviewControllerDataSource
- (NSInteger)numberOfPreviewItemsInPreviewController:(QLPreviewController *)controller{
NSInteger num = 0;
if (self.fileUrl) {
num = 1;
}
return num;
}
- (id <QLPreviewItem>)previewController:(QLPreviewController *)controller previewItemAtIndex:(NSInteger)index{
return [BasicPreviewItem itemWithUrl:self.fileUrl];
}
@end