LocalDocumentsViewController.m 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  1. //
  2. // LocalDocumentsViewController.m
  3. // Apex Mobile
  4. //
  5. // Created by Ray on 14-5-5.
  6. // Copyright (c) 2014年 United Software Applications, Inc. All rights reserved.
  7. //
  8. #import "LocalDocumentsViewController.h"
  9. #import "MyQLPreviewController.h"
  10. #import "LPShareActivity.h"
  11. @interface LocalDocumentsViewController () <QLPreviewControllerDelegate,QLPreviewControllerDataSource,UIDocumentInteractionControllerDelegate>
  12. //@property (nonatomic,copy) NSString *documentPath;
  13. @property (nonatomic,strong) MyQLPreviewController *quickLook;
  14. @property (nonatomic,copy) NSString *selectedDocumentPath;
  15. //@property (nonatomic,strong) UIDocumentInteractionController *documentController;
  16. //@property (nonatomic,copy) NSString *email;
  17. //@property (nonatomic,copy) NSString *email_subject;
  18. //@property (nonatomic,copy) NSString *email_content;
  19. @end
  20. @implementation LocalDocumentsViewController
  21. - (BOOL)shouldAutorotate
  22. {
  23. return YES;
  24. }
  25. - (UIInterfaceOrientationMask)supportedInterfaceOrientations
  26. {
  27. return UIInterfaceOrientationMaskPortrait;
  28. }
  29. - (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
  30. {
  31. return UIInterfaceOrientationPortrait;
  32. }
  33. -(void)initpanel
  34. {
  35. self.toolsinfo = [[NSMutableArray alloc] init];
  36. NSMutableDictionary* map = [[NSMutableDictionary alloc] init];
  37. NSFileManager *manager;
  38. manager = [NSFileManager defaultManager];
  39. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  40. NSString *path = [paths objectAtIndex:0];
  41. NSDirectoryEnumerator *direnum;
  42. direnum = [manager enumeratorAtPath: path];//枚举home下的目录
  43. NSLog(@"home=%@,direnum=%@",path,direnum);
  44. NSMutableArray *files;
  45. files = [NSMutableArray arrayWithCapacity:5];
  46. NSString *filename;
  47. while (filename = [direnum nextObject])
  48. {
  49. if ([[filename pathExtension].lowercaseString isEqualToString:@"pdf"])
  50. {
  51. [map setValue:filename forKey:@"title"];
  52. // [map setValue:@"rect_search_documents" forKey:@"img"];
  53. [self.toolsinfo addObject:map.copy];
  54. [files addObject: filename];
  55. NSLog (@"%@", filename);
  56. }
  57. }
  58. // NSEnumerator *fileenum;
  59. // fileenum = [files objectEnumerator];//枚举数组files
  60. // while (filename = [fileenum nextObject]) {
  61. // NSLog (@"%@", filename);
  62. // }
  63. //
  64. //
  65. //
  66. // [map setValue:@"View document" forKey:@"title"];
  67. // [map setValue:@"rect_view_download_documents" forKey:@"img"];
  68. // [self.toolsinfo addObject:map.copy];
  69. //
  70. }
  71. - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
  72. {
  73. self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
  74. if (self) {
  75. // Custom initialization
  76. }
  77. return self;
  78. }
  79. - (void)viewDidLoad
  80. {
  81. [super viewDidLoad];
  82. // Do any additional setup after loading the view.
  83. [self initpanel];
  84. UIBarButtonItem *clearItem = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"ic_clear"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStylePlain target:self action:@selector(clearClick:)];
  85. self.navigationItem.rightBarButtonItem = clearItem;
  86. }
  87. - (void)viewWillAppear:(BOOL)animated {
  88. [super viewWillAppear:animated];
  89. self.selectedDocumentPath = nil;
  90. self.tableview.tableFooterView = [UIView new];
  91. }
  92. - (void)didReceiveMemoryWarning
  93. {
  94. [super didReceiveMemoryWarning];
  95. // Dispose of any resources that can be recreated.
  96. }
  97. - (void)clearClick:(id)sender {
  98. NSFileManager *manager = [NSFileManager defaultManager];
  99. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  100. NSString *path = [paths objectAtIndex:0];
  101. NSDirectoryEnumerator *direnum = [manager enumeratorAtPath: path];//枚举home下的目录
  102. NSString *filename;
  103. while (filename = [direnum nextObject])
  104. {
  105. // if ([[filename pathExtension] isEqualToString:@"pdf"])
  106. // {
  107. NSString *filePath = [path stringByAppendingPathComponent:filename];
  108. [manager removeItemAtPath:filePath error:nil];
  109. // }
  110. }
  111. [self.toolsinfo removeAllObjects];
  112. [self.tableview reloadData];
  113. }
  114. /*
  115. #pragma mark - Navigation
  116. // In a storyboard-based application, you will often want to do a little preparation before navigation
  117. - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
  118. {
  119. // Get the new view controller using [segue destinationViewController].
  120. // Pass the selected object to the new view controller.
  121. }
  122. */
  123. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  124. {
  125. return 1;
  126. }
  127. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  128. {
  129. return self.toolsinfo.count;
  130. }
  131. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  132. {
  133. DebugLog(@"cellForRowAtIndexPath");
  134. NSString *CellIdentifier = @"documentitem";
  135. UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
  136. NSString* title = [self.toolsinfo[indexPath.row] valueForKey:@"title"];
  137. // cell.accessoryView
  138. // UIImageView* iv= [[UIImageView alloc] initWithImage:];
  139. cell.textLabel.text = title;
  140. // cell.imageView.image = [UIImage imageNamed:[self.toolsinfo[indexPath.row] valueForKey:@"img"]];
  141. return cell;
  142. }
  143. -(void) share_activity
  144. {
  145. // 设置分享内容
  146. // NSString *text = @"分享内容";
  147. // NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  148. // NSString *path = [paths objectAtIndex:0];
  149. // NSString *filePath = [path stringByAppendingPathComponent:[self.documentPath lastPathComponent]];
  150. //
  151. NSString *filePath = self.selectedDocumentPath;
  152. NSString *emailPath = [self emailPathForFile:filePath];
  153. NSData *data = [[NSData alloc] initWithContentsOfFile:emailPath];
  154. NSError *err = nil;
  155. NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&err];
  156. //self.e
  157. // 没有保存的情况
  158. // if ([[NSFileManager defaultManager] fileExistsAtPath:self.documentPath]) {
  159. // filePath = self.documentPath;
  160. // }
  161. NSURL *url = [NSURL fileURLWithPath:filePath];
  162. NSArray *activityItems = @[url];
  163. LPShareActivity *share = [[LPShareActivity alloc] init];
  164. share.to = [json[@"reciptions"] componentsSeparatedByString:@","];//@[@"676767@qq.com"];
  165. share.subject = json[@"subject"];//@"test subject";
  166. share.body = json[@"content"];// @"body";
  167. share.filePath = filePath;
  168. share.handle = ^(UIViewController *vc) {
  169. [self presentViewController:vc animated:YES completion:^{
  170. // [share activityDidFinish:YES];
  171. // ...
  172. }];
  173. };
  174. // 服务类型控制器
  175. UIActivityViewController *activityViewController = [[UIActivityViewController alloc] initWithActivityItems:activityItems applicationActivities:@[share]];
  176. // _activityViewController = activityViewController;
  177. // activityViewController.excludedActivityTypes = @[UIActivityTypeMail];
  178. activityViewController.modalInPopover = true;
  179. [self presentViewController:activityViewController animated:YES completion:nil];
  180. // 选中分享类型
  181. [activityViewController setCompletionWithItemsHandler:^(NSString * __nullable activityType, BOOL completed, NSArray * __nullable returnedItems, NSError * __nullable activityError){
  182. // 显示选中的分享类型
  183. NSLog(@"act type %@",activityType);
  184. if (completed) {
  185. NSLog(@"ok");
  186. }else {
  187. NSLog(@"not ok");
  188. }
  189. }];
  190. }
  191. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  192. {
  193. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  194. NSString *path = [paths objectAtIndex:0];
  195. NSString* filename =[self.toolsinfo[indexPath.row] valueForKey:@"title"];
  196. NSString *filePath = [path stringByAppendingPathComponent:filename];
  197. [self openFile:filePath];
  198. // NSFileManager *manager;
  199. // manager = [NSFileManager defaultManager];
  200. //
  201. // NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  202. // NSString *path = [paths objectAtIndex:0];
  203. // NSString* filename =[self.toolsinfo[indexPath.row] valueForKey:@"title"];
  204. //
  205. // NSString *filePath = [path stringByAppendingPathComponent:filename];
  206. //
  207. //
  208. //
  209. // PdfViewController *ViewController = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"PdfViewController"];
  210. // ViewController.filepath = filePath;
  211. // // ViewController. = self.function_name;
  212. // // ViewController.behavior =BEHAVIOR_SEARCH;
  213. //
  214. // [self.navigationController pushViewController:ViewController animated:YES];
  215. // SearchViewController *searchViewController = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"NewsView"];
  216. // searchViewController.function_name = @"Download Document";
  217. // [self.navigationController pushViewController:searchViewController animated:YES];
  218. // return;
  219. // NSString* title = [self.toolsinfo[indexPath.row] valueForKey:@"title"];
  220. // if([title isEqualToString:@"Search documents"])
  221. // {
  222. // SearchViewController *searchViewController = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"SearchViewController"];
  223. // searchViewController.function_name = @"Download Document";
  224. // [self.navigationController pushViewController:searchViewController animated:YES];
  225. // }
  226. // else if([title isEqualToString:@"View document"])
  227. // {
  228. // LocalDocumentsViewController *viewcontroller=[[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"LocalDocumentsViewController"];
  229. // // viewcontroller.module_name = @"Announcements";
  230. // [self.navigationController pushViewController:viewcontroller animated:YES];
  231. // }
  232. }
  233. - (NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath {
  234. __weak typeof(self) weakSelf = self;
  235. UITableViewRowAction *delete = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"Delete" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
  236. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  237. NSString *path = [paths objectAtIndex:0];
  238. NSString* filename =[weakSelf.toolsinfo[indexPath.row] valueForKey:@"title"];
  239. NSString *filePath = [path stringByAppendingPathComponent:filename];
  240. NSError *err;
  241. [[NSFileManager defaultManager] removeItemAtPath:filePath error:&err];
  242. if (!err) {
  243. [weakSelf.toolsinfo removeObjectAtIndex:indexPath.row];
  244. [tableView reloadData];
  245. }
  246. NSString *emailPath = [self emailPathForFile:filePath];
  247. [[NSFileManager defaultManager] removeItemAtPath:emailPath error:nil];
  248. }];
  249. return @[delete];
  250. }
  251. //-(void)tableView:(UITableView *)tableView1 accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath{
  252. //
  253. // NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  254. // NSString *path = [paths objectAtIndex:0];
  255. // NSString* filename =[self.toolsinfo[indexPath.row] valueForKey:@"title"];
  256. //
  257. // NSString *filePath = [path stringByAppendingPathComponent:filename];
  258. // [self openFile:filePath];
  259. //
  260. //// NSURL *url = [[NSURL alloc] initFileURLWithPath:filePath];
  261. ////
  262. //// UIDocumentInteractionController *documentController =[UIDocumentInteractionController interactionControllerWithURL: url];
  263. //// documentController.delegate = self;
  264. //// [documentController presentOptionsMenuFromRect:self.view.frame inView:self.view animated:YES];
  265. ////
  266. //// return;
  267. //
  268. //// NSURL *url = [[NSURL alloc] initFileURLWithPath:filePath];
  269. //// UIDocumentInteractionController *documentController =[UIDocumentInteractionController interactionControllerWithURL: url];
  270. ////
  271. //// return;
  272. //}
  273. -(void)openFile: (NSString *) file_url {
  274. self.selectedDocumentPath = file_url;
  275. if (self.selectedDocumentPath) {
  276. UIBarButtonItem *backItem = [[UIBarButtonItem alloc] init];
  277. backItem.title = @"Back";
  278. self.navigationItem.backBarButtonItem = backItem;
  279. [self.navigationController pushViewController:self.quickLook animated:YES];
  280. }
  281. // NSURL *file_URL = [NSURL fileURLWithPath:file_url];
  282. //
  283. // if (file_URL != nil) {
  284. // if (self.fileInteractionController == nil) {
  285. // //_fileInteractionController = [[UIDocumentInteractionController alloc] init];
  286. // _fileInteractionController = [UIDocumentInteractionController interactionControllerWithURL:file_URL];
  287. // _fileInteractionController.delegate = self;
  288. // // [_fileInteractionController retain];//不适用ARC,记得RETAIN 使用ARC注释这一行
  289. // }
  290. // else {
  291. // self.fileInteractionController.URL = file_URL;
  292. // }
  293. //
  294. //// [self.fileInteractionController presentOpenInMenuFromRect:self.view.frame inView:self.view animated:YES];
  295. // [self.fileInteractionController presentOptionsMenuFromRect:self.view.frame inView:self.view animated:YES];
  296. //// [self.fileInteractionController presentPreviewAnimated:YES];
  297. //
  298. // }
  299. }
  300. #pragma mark methods for the UIDocumentInteractionControllerDelegate
  301. - (void)previewDocumentWithURL:(NSURL*)url
  302. {
  303. UIDocumentInteractionController* preview = [UIDocumentInteractionController interactionControllerWithURL:url];
  304. preview.delegate = self;
  305. [preview presentPreviewAnimated:YES];
  306. }
  307. - (void)documentInteractionControllerDidEndPreview:(UIDocumentInteractionController *)controller
  308. {
  309. }
  310. - (UIViewController *)documentInteractionControllerViewControllerForPreview:(UIDocumentInteractionController *)controller{
  311. return self;
  312. }
  313. - (CGRect)documentInteractionControllerRectForPreview:(UIDocumentInteractionController *)controller{
  314. return self.view.frame;
  315. }
  316. - (UIView *)documentInteractionControllerViewForPreview:(UIDocumentInteractionController *)controller{
  317. return self.view;
  318. }
  319. #pragma mark - Share
  320. - (void)shareClick:(id)sender {
  321. [self share_activity];
  322. }
  323. //- (void)shareDocument {
  324. //
  325. // NSString *filePath = self.selectedDocumentPath;
  326. // NSString *emailPath = [self emailPathForFile:filePath];
  327. //
  328. // NSURL *fileURL = [NSURL fileURLWithPath:filePath];
  329. //
  330. // if (self.documentController == nil) {
  331. // UIDocumentInteractionController *documentController = [UIDocumentInteractionController interactionControllerWithURL:fileURL];
  332. // documentController.delegate = self;
  333. // documentController.UTI = @"com.adobe.pdf";
  334. // self.documentController = documentController;
  335. // } else {
  336. // self.documentController.URL = fileURL;
  337. // }
  338. // self.documentController.annotation = @{@"_toRecipients" : @[@"676034647@qq.com"]};
  339. // self.documentController.name = @"Test";
  340. //
  341. // // [self.documentController presentOpenInMenuFromRect:self.navigationController.topViewController.view.bounds
  342. // // inView:self.navigationController.topViewController.view
  343. // // animated:YES];
  344. //
  345. // [self.documentController presentOptionsMenuFromRect:self.navigationController.topViewController.view.bounds
  346. // inView:self.navigationController.topViewController.view
  347. // animated:YES];
  348. //}
  349. - (NSString *)emailPathForFile:(NSString *)file {
  350. if (file == nil) {
  351. return nil;
  352. }
  353. NSString *fileName = [file lastPathComponent];
  354. NSString *emailPath = [[file stringByDeletingLastPathComponent] stringByAppendingPathComponent:[NSString stringWithFormat:@".%@",[fileName stringByDeletingPathExtension]]];
  355. return emailPath;
  356. }
  357. #pragma mark - Quick Look
  358. - (MyQLPreviewController *)quickLook {
  359. if (!_quickLook)
  360. {
  361. _quickLook = [[MyQLPreviewController alloc] init];
  362. _quickLook.delegate = self;
  363. _quickLook.dataSource = self;
  364. UIBarButtonItem *shareBtn = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"ic_share_new"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStylePlain target:self action:@selector(shareClick:)];
  365. _quickLook.navigationItem.rightBarButtonItem = shareBtn;
  366. }
  367. else
  368. {
  369. [_quickLook reloadData];
  370. }
  371. return _quickLook;
  372. }
  373. #pragma mark - QuickLook Data Source
  374. - (NSInteger)numberOfPreviewItemsInPreviewController:(QLPreviewController *)controller {
  375. return 1;
  376. }
  377. - (id <QLPreviewItem>) previewController:(QLPreviewController *)controller previewItemAtIndex:(NSInteger)index {
  378. return [NSURL fileURLWithPath:self.selectedDocumentPath];
  379. }
  380. #pragma mark - QuickLook Delegate
  381. - (void) previewControllerDidDismiss:(QLPreviewController *)controller {
  382. }
  383. /**
  384. * 文件内部链接点击不进行外部跳转
  385. */
  386. - (BOOL) previewController:(QLPreviewController *)controller shouldOpenURL:(NSURL *)url forPreviewItem:(id<QLPreviewItem>)item {
  387. return NO;
  388. }
  389. @end