123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254 |
- //
- // ViewController.m
- // PDFReader
- //
- // Copyright © 2014-2022 PDF Technologies, Inc. All Rights Reserved.
- //
- // The PDF Reader Sample applications are licensed with a modified BSD license.
- // Please see License for details. This notice may not be removed from this file.
- //
- #import "FileListViewController.h"
- #import "PDFViewController.h"
- #import "PDFSettingsViewController.h"
- static const CGFloat TUpdateTimeInternal = 2.0;
- @interface FileListViewController () <UITableViewDelegate, UITableViewDataSource>
- @property (nonatomic,retain) NSMutableArray<NSString *> *fileArray;
- @property (nonatomic,retain) NSTimer *updateFolderTimer;
- @property (nonatomic,retain) UITableView *tableView;
- @end
- @implementation FileListViewController
- #pragma mark - Init Methods
- - (id)init {
- if (self = [super init]) {
-
- }
- return self;
- }
- - (void)dealloc {
- _tableView.delegate = nil;
- _tableView.dataSource = nil;
- [_tableView release];
-
- if ([_updateFolderTimer isValid]){
- [_updateFolderTimer invalidate];
- }
- [_updateFolderTimer release];
- [_fileArray release];
- [_path release];
- [super dealloc];
- }
- #pragma mark - UIViewController Methods
- - (void)viewDidLoad {
- [super viewDidLoad];
- // Do any additional setup after loading the view.
- if (!self.path) {
- self.path = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
- }
- self.title = [self.path lastPathComponent];
-
- [self loadSamplesFiles];
-
- self.tableView = [[[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain] autorelease];
- self.tableView.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
- self.tableView.delegate = self;
- self.tableView.dataSource = self;
- [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"Cell"];
- [self.view addSubview:self.tableView];
-
- UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Settings", nil)
- style:UIBarButtonItemStylePlain
- target:self
- action:@selector(buttonItemClicked_Settings)];
- self.navigationItem.rightBarButtonItem = item;
- [item release];
- }
- - (void)viewWillAppear:(BOOL)animated {
- [super viewWillAppear:animated];
- self.navigationController.navigationBar.translucent = NO;
-
- // 开启定时器
- [self registTimer];
-
- // 刷新数据
- [self reloadFileManagerData];
- }
- - (void)viewWillDisappear:(BOOL)animated {
- [super viewWillDisappear:animated];
-
- // 关闭定时器
- [self unregistTimer];
- }
- - (void)buttonItemClicked_Settings {
- PDFSettingsViewController *vc = [[PDFSettingsViewController alloc] init];
- [self.navigationController pushViewController:vc animated:YES];
- [vc release];
- }
- - (void)loadSamplesFiles {
- NSURL *samplesURL = [NSBundle.mainBundle.resourceURL URLByAppendingPathComponent:@"Samples"];
- NSString *docsFolder = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).firstObject;
-
-
- NSArray *tFileNames = [NSArray arrayWithObjects:@"Form_Widgets_Test.pdf", @"PDF32000_2008.pdf", @"Quick Start Guide.pdf", @"Quick Start Guide.docx", nil];
-
- NSFileManager *fileManager = [NSFileManager defaultManager];
-
- for ( int i = 0; i < [tFileNames count]; i++) {
- NSURL *tBFilePath = [samplesURL URLByAppendingPathComponent:[tFileNames objectAtIndex:i]];
- NSURL *tDFilePathName = [NSURL fileURLWithPath:[docsFolder stringByAppendingPathComponent:[tFileNames objectAtIndex:i]]];
-
- if (![fileManager fileExistsAtPath:tDFilePathName.path]) {
- if ([fileManager fileExistsAtPath:tBFilePath.path]) {
- [fileManager copyItemAtURL:tBFilePath toURL:tDFilePathName error:nil];
- }
- }
- }
- }
- #pragma mark - Private Methods
- - (void)registTimer {
- self.updateFolderTimer = [NSTimer timerWithTimeInterval:TUpdateTimeInternal
- target:self
- selector:@selector(updateFileManagerData)
- userInfo:nil
- repeats:YES];
- [[NSRunLoop currentRunLoop] addTimer:self.updateFolderTimer forMode:NSDefaultRunLoopMode];
- }
- - (void)unregistTimer {
- if ([self.updateFolderTimer isValid]){
- [self.updateFolderTimer invalidate];
- }
- self.updateFolderTimer = nil;
- }
- - (void)updateFileManagerData {
- [self reloadFileManagerData];
- }
- - (void)reloadFileManagerData {
- [self searchFiles];
- [self.tableView reloadData];
- }
- - (void)searchFiles {
- NSMutableArray *folders = [NSMutableArray array];
- NSMutableArray *files = [NSMutableArray array];
-
- NSURL *url = [NSURL fileURLWithPath:self.path];
- NSFileManager *fileManager = [NSFileManager defaultManager];
- NSArray *contents = [fileManager contentsOfDirectoryAtURL:url
- includingPropertiesForKeys:nil
- options:(NSDirectoryEnumerationSkipsSubdirectoryDescendants |
- NSDirectoryEnumerationSkipsPackageDescendants |
- NSDirectoryEnumerationSkipsHiddenFiles)
- error:nil];
- for (int i=0; i<[contents count]; i++) {
- NSString *filePath = [[contents objectAtIndex:i] path];
- BOOL isDirectory = NO;
- [[NSFileManager defaultManager] fileExistsAtPath:filePath isDirectory:&isDirectory];
- if (isDirectory) {
- [folders addObject:filePath];
- } else {
- NSString *pathExtension = [filePath.pathExtension lowercaseString];
- if ([pathExtension isEqualToString:@"pdf"] ||
- [pathExtension isEqualToString:@"docx"]) {
- [files addObject:filePath];
- }
- }
- }
-
- [self.fileArray removeAllObjects];
- self.fileArray = [NSMutableArray array];
- [self.fileArray addObjectsFromArray:folders];
- [self.fileArray addObjectsFromArray:files];
- }
- #pragma mark - UITableViewDataSource
- - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
- return self.fileArray.count;
- }
- - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
- UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
- NSString *filePath = self.fileArray[indexPath.row];
- BOOL isDirectory = NO;
- [[NSFileManager defaultManager] fileExistsAtPath:filePath isDirectory:&isDirectory];
- cell.textLabel.text = [filePath lastPathComponent];
- return cell;
- }
- #pragma mark - UITableViewDelegate
- - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
- [tableView deselectRowAtIndexPath:indexPath animated:YES];
- NSString *filePath = self.fileArray[indexPath.row];
- BOOL isDirectory = NO;
- [[NSFileManager defaultManager] fileExistsAtPath:filePath isDirectory:&isDirectory];
- if (isDirectory) {
- FileListViewController *vc = [[FileListViewController alloc] init];
- vc.path = filePath;
- [self.navigationController pushViewController:vc animated:YES];
- [vc release];
- } else {
-
- if ([filePath.pathExtension isEqualToString:@"pdf"]) {
-
- // Initializers, Make sure you enter the local document path in PDF format
- PDFViewController *vc = [[PDFViewController alloc] initWithFilePath:filePath];
- [self.navigationController pushViewController:vc animated:YES];
- [vc release];
-
- } else {
- UIActivityIndicatorView *aiView = [[[UIActivityIndicatorView alloc] init] autorelease];
- aiView.activityIndicatorViewStyle = UIActivityIndicatorViewStyleWhiteLarge;
- aiView.color = [UIColor grayColor];
- [aiView sizeToFit];
- aiView.center = self.view.center;
- aiView.autoresizingMask = UIViewAutoresizingFlexibleTopMargin|UIViewAutoresizingFlexibleBottomMargin|UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleRightMargin;
- [self.view addSubview:aiView];
- [aiView startAnimating];
- self.view.userInteractionEnabled = NO;
-
- NSString *fileName = [[filePath.lastPathComponent stringByDeletingPathExtension] stringByAppendingPathExtension:@"pdf"];
- NSString *pdfFilePath = [NSTemporaryDirectory() stringByAppendingPathComponent:fileName];
-
- NSFileManager *fileManager = [NSFileManager defaultManager];
- if ([fileManager fileExistsAtPath:pdfFilePath]) {
- [fileManager removeItemAtPath:pdfFilePath error:nil];
- }
-
- // Convert files (doc, docx, xls, xlsx, ppt, pptx, txt, jpeg, and png) to PDF (offline)
- [[CPDFKit sharedInstance] convertFilePath:filePath toPath:pdfFilePath completion:^(BOOL result) {
- self.view.userInteractionEnabled = YES;
- [aiView removeFromSuperview];
- if (result) {
- PDFViewController *vc = [[PDFViewController alloc] initWithFilePath:pdfFilePath];
- [self.navigationController pushViewController:vc animated:YES];
- [vc release];
- }
- }];
- }
- }
- }
- @end
|