| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491 |
- //
- // LocalDocumentsViewController.m
- // Apex Mobile
- //
- // Created by Ray on 14-5-5.
- // Copyright (c) 2014年 United Software Applications, Inc. All rights reserved.
- //
- #import "LocalDocumentsViewController.h"
- #import "MyQLPreviewController.h"
- #import "LPShareActivity.h"
- @interface LocalDocumentsViewController () <QLPreviewControllerDelegate,QLPreviewControllerDataSource,UIDocumentInteractionControllerDelegate>
- //@property (nonatomic,copy) NSString *documentPath;
- @property (nonatomic,strong) MyQLPreviewController *quickLook;
- @property (nonatomic,copy) NSString *selectedDocumentPath;
- //@property (nonatomic,strong) UIDocumentInteractionController *documentController;
- //@property (nonatomic,copy) NSString *email;
- //@property (nonatomic,copy) NSString *email_subject;
- //@property (nonatomic,copy) NSString *email_content;
- @end
- @implementation LocalDocumentsViewController
- - (BOOL)shouldAutorotate
- {
- return YES;
- }
- - (UIInterfaceOrientationMask)supportedInterfaceOrientations
- {
- return UIInterfaceOrientationMaskPortrait;
- }
- - (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
- {
- return UIInterfaceOrientationPortrait;
- }
- -(void)initpanel
- {
-
- self.toolsinfo = [[NSMutableArray alloc] init];
- NSMutableDictionary* map = [[NSMutableDictionary alloc] init];
-
-
- NSFileManager *manager;
- manager = [NSFileManager defaultManager];
- NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
- NSString *path = [paths objectAtIndex:0];
- NSDirectoryEnumerator *direnum;
- direnum = [manager enumeratorAtPath: path];//枚举home下的目录
- NSLog(@"home=%@,direnum=%@",path,direnum);
- NSMutableArray *files;
- files = [NSMutableArray arrayWithCapacity:5];
- NSString *filename;
- while (filename = [direnum nextObject])
- {
- if ([[filename pathExtension].lowercaseString isEqualToString:@"pdf"])
- {
- [map setValue:filename forKey:@"title"];
- // [map setValue:@"rect_search_documents" forKey:@"img"];
- [self.toolsinfo addObject:map.copy];
-
- [files addObject: filename];
- NSLog (@"%@", filename);
- }
- }
- // NSEnumerator *fileenum;
- // fileenum = [files objectEnumerator];//枚举数组files
- // while (filename = [fileenum nextObject]) {
- // NSLog (@"%@", filename);
- // }
- //
-
-
-
-
-
-
-
-
- //
- //
- // [map setValue:@"View document" forKey:@"title"];
- // [map setValue:@"rect_view_download_documents" forKey:@"img"];
- // [self.toolsinfo addObject:map.copy];
- //
-
- }
- - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
- {
- self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
- if (self) {
- // Custom initialization
- }
- return self;
- }
- - (void)viewDidLoad
- {
- [super viewDidLoad];
- // Do any additional setup after loading the view.
- [self initpanel];
-
- UIBarButtonItem *clearItem = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"ic_clear"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStylePlain target:self action:@selector(clearClick:)];
- self.navigationItem.rightBarButtonItem = clearItem;
- }
- - (void)viewWillAppear:(BOOL)animated {
- [super viewWillAppear:animated];
-
- self.selectedDocumentPath = nil;
- self.tableview.tableFooterView = [UIView new];
- }
- - (void)didReceiveMemoryWarning
- {
- [super didReceiveMemoryWarning];
- // Dispose of any resources that can be recreated.
- }
- - (void)clearClick:(id)sender {
-
- NSFileManager *manager = [NSFileManager defaultManager];
- NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
- NSString *path = [paths objectAtIndex:0];
- NSDirectoryEnumerator *direnum = [manager enumeratorAtPath: path];//枚举home下的目录
-
- NSString *filename;
- while (filename = [direnum nextObject])
- {
- // if ([[filename pathExtension] isEqualToString:@"pdf"])
- // {
- NSString *filePath = [path stringByAppendingPathComponent:filename];
- [manager removeItemAtPath:filePath error:nil];
- // }
- }
- [self.toolsinfo removeAllObjects];
- [self.tableview reloadData];
- }
- /*
- #pragma mark - Navigation
-
- // In a storyboard-based application, you will often want to do a little preparation before navigation
- - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
- {
- // Get the new view controller using [segue destinationViewController].
- // Pass the selected object to the new view controller.
- }
- */
- - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
- {
-
- return 1;
- }
- - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
- {
-
- return self.toolsinfo.count;
-
-
- }
- - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
- {
- DebugLog(@"cellForRowAtIndexPath");
-
- NSString *CellIdentifier = @"documentitem";
- UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
- NSString* title = [self.toolsinfo[indexPath.row] valueForKey:@"title"];
-
-
- // cell.accessoryView
- // UIImageView* iv= [[UIImageView alloc] initWithImage:];
- cell.textLabel.text = title;
- // cell.imageView.image = [UIImage imageNamed:[self.toolsinfo[indexPath.row] valueForKey:@"img"]];
-
- return cell;
-
-
- }
- -(void) share_activity
- {
- // 设置分享内容
- // NSString *text = @"分享内容";
- // NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
- // NSString *path = [paths objectAtIndex:0];
- // NSString *filePath = [path stringByAppendingPathComponent:[self.documentPath lastPathComponent]];
- //
-
- NSString *filePath = self.selectedDocumentPath;
- NSString *emailPath = [self emailPathForFile:filePath];
-
- NSData *data = [[NSData alloc] initWithContentsOfFile:emailPath];
- NSError *err = nil;
- NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&err];
-
- //self.e
-
- // 没有保存的情况
- // if ([[NSFileManager defaultManager] fileExistsAtPath:self.documentPath]) {
- // filePath = self.documentPath;
- // }
-
- NSURL *url = [NSURL fileURLWithPath:filePath];
-
- NSArray *activityItems = @[url];
-
- LPShareActivity *share = [[LPShareActivity alloc] init];
- share.to = [json[@"reciptions"] componentsSeparatedByString:@","];//@[@"676767@qq.com"];
- share.subject = json[@"subject"];//@"test subject";
- share.body = json[@"content"];// @"body";
- share.filePath = filePath;
- share.handle = ^(UIViewController *vc) {
- [self presentViewController:vc animated:YES completion:^{
- // [share activityDidFinish:YES];
- // ...
- }];
- };
-
- // 服务类型控制器
- UIActivityViewController *activityViewController = [[UIActivityViewController alloc] initWithActivityItems:activityItems applicationActivities:@[share]];
- // _activityViewController = activityViewController;
- // activityViewController.excludedActivityTypes = @[UIActivityTypeMail];
- activityViewController.modalInPopover = true;
- [self presentViewController:activityViewController animated:YES completion:nil];
-
- // 选中分享类型
- [activityViewController setCompletionWithItemsHandler:^(NSString * __nullable activityType, BOOL completed, NSArray * __nullable returnedItems, NSError * __nullable activityError){
-
- // 显示选中的分享类型
- NSLog(@"act type %@",activityType);
-
- if (completed) {
- NSLog(@"ok");
- }else {
- NSLog(@"not ok");
- }
-
- }];
-
-
-
- }
- - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
- {
-
- NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
- NSString *path = [paths objectAtIndex:0];
- NSString* filename =[self.toolsinfo[indexPath.row] valueForKey:@"title"];
-
- NSString *filePath = [path stringByAppendingPathComponent:filename];
- [self openFile:filePath];
-
- // NSFileManager *manager;
- // manager = [NSFileManager defaultManager];
- //
- // NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
- // NSString *path = [paths objectAtIndex:0];
- // NSString* filename =[self.toolsinfo[indexPath.row] valueForKey:@"title"];
- //
- // NSString *filePath = [path stringByAppendingPathComponent:filename];
- //
- //
- //
- // PdfViewController *ViewController = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"PdfViewController"];
- // ViewController.filepath = filePath;
- // // ViewController. = self.function_name;
- // // ViewController.behavior =BEHAVIOR_SEARCH;
- //
- // [self.navigationController pushViewController:ViewController animated:YES];
-
- // SearchViewController *searchViewController = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"NewsView"];
- // searchViewController.function_name = @"Download Document";
- // [self.navigationController pushViewController:searchViewController animated:YES];
- // return;
-
- // NSString* title = [self.toolsinfo[indexPath.row] valueForKey:@"title"];
- // if([title isEqualToString:@"Search documents"])
- // {
- // SearchViewController *searchViewController = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"SearchViewController"];
- // searchViewController.function_name = @"Download Document";
- // [self.navigationController pushViewController:searchViewController animated:YES];
- // }
- // else if([title isEqualToString:@"View document"])
- // {
- // LocalDocumentsViewController *viewcontroller=[[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"LocalDocumentsViewController"];
- // // viewcontroller.module_name = @"Announcements";
- // [self.navigationController pushViewController:viewcontroller animated:YES];
- // }
-
- }
- - (NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath {
-
- __weak typeof(self) weakSelf = self;
- UITableViewRowAction *delete = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"Delete" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
-
- NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
- NSString *path = [paths objectAtIndex:0];
- NSString* filename =[weakSelf.toolsinfo[indexPath.row] valueForKey:@"title"];
- NSString *filePath = [path stringByAppendingPathComponent:filename];
-
- NSError *err;
- [[NSFileManager defaultManager] removeItemAtPath:filePath error:&err];
- if (!err) {
- [weakSelf.toolsinfo removeObjectAtIndex:indexPath.row];
- [tableView reloadData];
- }
-
- NSString *emailPath = [self emailPathForFile:filePath];
- [[NSFileManager defaultManager] removeItemAtPath:emailPath error:nil];
-
- }];
-
- return @[delete];
- }
- //-(void)tableView:(UITableView *)tableView1 accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath{
- //
- // NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
- // NSString *path = [paths objectAtIndex:0];
- // NSString* filename =[self.toolsinfo[indexPath.row] valueForKey:@"title"];
- //
- // NSString *filePath = [path stringByAppendingPathComponent:filename];
- // [self openFile:filePath];
- //
- //// NSURL *url = [[NSURL alloc] initFileURLWithPath:filePath];
- ////
- //// UIDocumentInteractionController *documentController =[UIDocumentInteractionController interactionControllerWithURL: url];
- //// documentController.delegate = self;
- //// [documentController presentOptionsMenuFromRect:self.view.frame inView:self.view animated:YES];
- ////
- //// return;
- //
- //// NSURL *url = [[NSURL alloc] initFileURLWithPath:filePath];
- //// UIDocumentInteractionController *documentController =[UIDocumentInteractionController interactionControllerWithURL: url];
- ////
- //// return;
- //}
- -(void)openFile: (NSString *) file_url {
-
- self.selectedDocumentPath = file_url;
-
- if (self.selectedDocumentPath) {
- UIBarButtonItem *backItem = [[UIBarButtonItem alloc] init];
- backItem.title = @"Back";
- self.navigationItem.backBarButtonItem = backItem;
- [self.navigationController pushViewController:self.quickLook animated:YES];
- }
-
- // NSURL *file_URL = [NSURL fileURLWithPath:file_url];
- //
- // if (file_URL != nil) {
- // if (self.fileInteractionController == nil) {
- // //_fileInteractionController = [[UIDocumentInteractionController alloc] init];
- // _fileInteractionController = [UIDocumentInteractionController interactionControllerWithURL:file_URL];
- // _fileInteractionController.delegate = self;
- // // [_fileInteractionController retain];//不适用ARC,记得RETAIN 使用ARC注释这一行
- // }
- // else {
- // self.fileInteractionController.URL = file_URL;
- // }
- //
- //// [self.fileInteractionController presentOpenInMenuFromRect:self.view.frame inView:self.view animated:YES];
- // [self.fileInteractionController presentOptionsMenuFromRect:self.view.frame inView:self.view animated:YES];
- //// [self.fileInteractionController presentPreviewAnimated:YES];
- //
- // }
- }
- #pragma mark methods for the UIDocumentInteractionControllerDelegate
- - (void)previewDocumentWithURL:(NSURL*)url
- {
- UIDocumentInteractionController* preview = [UIDocumentInteractionController interactionControllerWithURL:url];
- preview.delegate = self;
- [preview presentPreviewAnimated:YES];
- }
- - (void)documentInteractionControllerDidEndPreview:(UIDocumentInteractionController *)controller
- {
- }
- - (UIViewController *)documentInteractionControllerViewControllerForPreview:(UIDocumentInteractionController *)controller{
- return self;
- }
- - (CGRect)documentInteractionControllerRectForPreview:(UIDocumentInteractionController *)controller{
- return self.view.frame;
- }
- - (UIView *)documentInteractionControllerViewForPreview:(UIDocumentInteractionController *)controller{
- return self.view;
- }
- #pragma mark - Share
- - (void)shareClick:(id)sender {
- [self share_activity];
- }
- //- (void)shareDocument {
- //
- // NSString *filePath = self.selectedDocumentPath;
- // NSString *emailPath = [self emailPathForFile:filePath];
- //
- // NSURL *fileURL = [NSURL fileURLWithPath:filePath];
- //
- // if (self.documentController == nil) {
- // UIDocumentInteractionController *documentController = [UIDocumentInteractionController interactionControllerWithURL:fileURL];
- // documentController.delegate = self;
- // documentController.UTI = @"com.adobe.pdf";
- // self.documentController = documentController;
- // } else {
- // self.documentController.URL = fileURL;
- // }
- // self.documentController.annotation = @{@"_toRecipients" : @[@"676034647@qq.com"]};
- // self.documentController.name = @"Test";
- //
- // // [self.documentController presentOpenInMenuFromRect:self.navigationController.topViewController.view.bounds
- // // inView:self.navigationController.topViewController.view
- // // animated:YES];
- //
- // [self.documentController presentOptionsMenuFromRect:self.navigationController.topViewController.view.bounds
- // inView:self.navigationController.topViewController.view
- // animated:YES];
- //}
- - (NSString *)emailPathForFile:(NSString *)file {
- if (file == nil) {
- return nil;
- }
- NSString *fileName = [file lastPathComponent];
- NSString *emailPath = [[file stringByDeletingLastPathComponent] stringByAppendingPathComponent:[NSString stringWithFormat:@".%@",[fileName stringByDeletingPathExtension]]];
- return emailPath;
- }
- #pragma mark - Quick Look
- - (MyQLPreviewController *)quickLook {
- if (!_quickLook)
- {
- _quickLook = [[MyQLPreviewController alloc] init];
- _quickLook.delegate = self;
- _quickLook.dataSource = self;
- UIBarButtonItem *shareBtn = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"ic_share_new"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStylePlain target:self action:@selector(shareClick:)];
- _quickLook.navigationItem.rightBarButtonItem = shareBtn;
- }
- else
- {
- [_quickLook reloadData];
- }
- return _quickLook;
- }
- #pragma mark - QuickLook Data Source
- - (NSInteger)numberOfPreviewItemsInPreviewController:(QLPreviewController *)controller {
- return 1;
- }
- - (id <QLPreviewItem>) previewController:(QLPreviewController *)controller previewItemAtIndex:(NSInteger)index {
-
- return [NSURL fileURLWithPath:self.selectedDocumentPath];
- }
- #pragma mark - QuickLook Delegate
- - (void) previewControllerDidDismiss:(QLPreviewController *)controller {
-
- }
- /**
- * 文件内部链接点击不进行外部跳转
- */
- - (BOOL) previewController:(QLPreviewController *)controller shouldOpenURL:(NSURL *)url forPreviewItem:(id<QLPreviewItem>)item {
- return NO;
- }
- @end
|