LocalDocumentsViewController.m 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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. @interface LocalDocumentsViewController ()
  10. @end
  11. @implementation LocalDocumentsViewController
  12. - (BOOL)shouldAutorotate
  13. {
  14. return YES;
  15. }
  16. - (NSUInteger)supportedInterfaceOrientations
  17. {
  18. return UIInterfaceOrientationMaskPortrait;
  19. }
  20. - (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
  21. {
  22. return UIInterfaceOrientationPortrait;
  23. }
  24. -(void)initpanel
  25. {
  26. self.toolsinfo = [[NSMutableArray alloc] init];
  27. NSMutableDictionary* map = [[NSMutableDictionary alloc] init];
  28. NSFileManager *manager;
  29. manager = [NSFileManager defaultManager];
  30. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  31. NSString *path = [paths objectAtIndex:0];
  32. NSDirectoryEnumerator *direnum;
  33. direnum = [manager enumeratorAtPath: path];//枚举home下的目录
  34. NSLog(@"home=%@,direnum=%@",path,direnum);
  35. NSMutableArray *files;
  36. files = [NSMutableArray arrayWithCapacity:5];
  37. NSString *filename;
  38. while (filename = [direnum nextObject])
  39. {
  40. if ([[filename pathExtension] isEqualToString:@"pdf"])
  41. {
  42. [map setValue:filename forKey:@"title"];
  43. // [map setValue:@"rect_search_documents" forKey:@"img"];
  44. [self.toolsinfo addObject:map.copy];
  45. [files addObject: filename];
  46. NSLog (@"%@", filename);
  47. }
  48. }
  49. // NSEnumerator *fileenum;
  50. // fileenum = [files objectEnumerator];//枚举数组files
  51. // while (filename = [fileenum nextObject]) {
  52. // NSLog (@"%@", filename);
  53. // }
  54. //
  55. //
  56. //
  57. // [map setValue:@"View document" forKey:@"title"];
  58. // [map setValue:@"rect_view_download_documents" forKey:@"img"];
  59. // [self.toolsinfo addObject:map.copy];
  60. //
  61. }
  62. - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
  63. {
  64. self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
  65. if (self) {
  66. // Custom initialization
  67. }
  68. return self;
  69. }
  70. - (void)viewDidLoad
  71. {
  72. [super viewDidLoad];
  73. // Do any additional setup after loading the view.
  74. [self initpanel];
  75. }
  76. - (void)didReceiveMemoryWarning
  77. {
  78. [super didReceiveMemoryWarning];
  79. // Dispose of any resources that can be recreated.
  80. }
  81. /*
  82. #pragma mark - Navigation
  83. // In a storyboard-based application, you will often want to do a little preparation before navigation
  84. - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
  85. {
  86. // Get the new view controller using [segue destinationViewController].
  87. // Pass the selected object to the new view controller.
  88. }
  89. */
  90. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  91. {
  92. return 1;
  93. }
  94. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  95. {
  96. return self.toolsinfo.count;
  97. }
  98. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  99. {
  100. DebugLog(@"cellForRowAtIndexPath");
  101. NSString *CellIdentifier = @"documentitem";
  102. UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
  103. NSString* title = [self.toolsinfo[indexPath.row] valueForKey:@"title"];
  104. // cell.accessoryView
  105. // UIImageView* iv= [[UIImageView alloc] initWithImage:];
  106. cell.textLabel.text = title;
  107. // cell.imageView.image = [UIImage imageNamed:[self.toolsinfo[indexPath.row] valueForKey:@"img"]];
  108. return cell;
  109. }
  110. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  111. {
  112. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  113. NSString *path = [paths objectAtIndex:0];
  114. NSString* filename =[self.toolsinfo[indexPath.row] valueForKey:@"title"];
  115. NSString *filePath = [path stringByAppendingPathComponent:filename];
  116. [self openFile:filePath];
  117. // NSFileManager *manager;
  118. // manager = [NSFileManager defaultManager];
  119. //
  120. // NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  121. // NSString *path = [paths objectAtIndex:0];
  122. // NSString* filename =[self.toolsinfo[indexPath.row] valueForKey:@"title"];
  123. //
  124. // NSString *filePath = [path stringByAppendingPathComponent:filename];
  125. //
  126. //
  127. //
  128. // PdfViewController *ViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"PdfViewController"];
  129. // ViewController.filepath = filePath;
  130. // // ViewController. = self.function_name;
  131. // // ViewController.behavior =BEHAVIOR_SEARCH;
  132. //
  133. // [self.navigationController pushViewController:ViewController animated:YES];
  134. // SearchViewController *searchViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"NewsView"];
  135. // searchViewController.function_name = @"Download Document";
  136. // [self.navigationController pushViewController:searchViewController animated:YES];
  137. // return;
  138. // NSString* title = [self.toolsinfo[indexPath.row] valueForKey:@"title"];
  139. // if([title isEqualToString:@"Search documents"])
  140. // {
  141. // SearchViewController *searchViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"SearchViewController"];
  142. // searchViewController.function_name = @"Download Document";
  143. // [self.navigationController pushViewController:searchViewController animated:YES];
  144. // }
  145. // else if([title isEqualToString:@"View document"])
  146. // {
  147. // LocalDocumentsViewController *viewcontroller=[self.storyboard instantiateViewControllerWithIdentifier:@"LocalDocumentsViewController"];
  148. // // viewcontroller.module_name = @"Announcements";
  149. // [self.navigationController pushViewController:viewcontroller animated:YES];
  150. // }
  151. }
  152. //-(void)tableView:(UITableView *)tableView1 accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath{
  153. //
  154. // NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  155. // NSString *path = [paths objectAtIndex:0];
  156. // NSString* filename =[self.toolsinfo[indexPath.row] valueForKey:@"title"];
  157. //
  158. // NSString *filePath = [path stringByAppendingPathComponent:filename];
  159. // [self openFile:filePath];
  160. //
  161. //// NSURL *url = [[NSURL alloc] initFileURLWithPath:filePath];
  162. ////
  163. //// UIDocumentInteractionController *documentController =[UIDocumentInteractionController interactionControllerWithURL: url];
  164. //// documentController.delegate = self;
  165. //// [documentController presentOptionsMenuFromRect:self.view.frame inView:self.view animated:YES];
  166. ////
  167. //// return;
  168. //
  169. //// NSURL *url = [[NSURL alloc] initFileURLWithPath:filePath];
  170. //// UIDocumentInteractionController *documentController =[UIDocumentInteractionController interactionControllerWithURL: url];
  171. ////
  172. //// return;
  173. //}
  174. -(void)openFile: (NSString *) file_url {
  175. NSURL *file_URL = [NSURL fileURLWithPath:file_url];
  176. if (file_URL != nil) {
  177. if (self.fileInteractionController == nil) {
  178. //_fileInteractionController = [[UIDocumentInteractionController alloc] init];
  179. _fileInteractionController = [UIDocumentInteractionController interactionControllerWithURL:file_URL];
  180. _fileInteractionController.delegate = self;
  181. // [_fileInteractionController retain];//不适用ARC,记得RETAIN 使用ARC注释这一行
  182. }
  183. else {
  184. self.fileInteractionController.URL = file_URL;
  185. }
  186. // [self.fileInteractionController presentOpenInMenuFromRect:self.view.frame inView:self.view animated:YES];
  187. [self.fileInteractionController presentOptionsMenuFromRect:self.view.frame inView:self.view animated:YES];
  188. // [self.fileInteractionController presentPreviewAnimated:YES];
  189. }
  190. }
  191. #pragma mark methods for the UIDocumentInteractionControllerDelegate
  192. - (void)previewDocumentWithURL:(NSURL*)url
  193. {
  194. UIDocumentInteractionController* preview = [UIDocumentInteractionController interactionControllerWithURL:url];
  195. preview.delegate = self;
  196. [preview presentPreviewAnimated:YES];
  197. }
  198. - (void)documentInteractionControllerDidEndPreview:(UIDocumentInteractionController *)controller
  199. {
  200. }
  201. - (UIViewController *)documentInteractionControllerViewControllerForPreview:(UIDocumentInteractionController *)controller{
  202. return self;
  203. }
  204. - (CGRect)documentInteractionControllerRectForPreview:(UIDocumentInteractionController *)controller{
  205. return self.view.frame;
  206. }
  207. - (UIView *)documentInteractionControllerViewForPreview:(UIDocumentInteractionController *)controller{
  208. return self.view;
  209. }
  210. @end