Bläddra i källkod

PDFViewer(ios) - 加载PDFView

dengkang 2 år sedan
förälder
incheckning
a6e8f0b796

+ 12 - 6
PDFViewer.xcodeproj/project.pbxproj

@@ -17,7 +17,8 @@
 		EC62979C29220F8400D339EE /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = EC62979A29220F8400D339EE /* LaunchScreen.storyboard */; };
 		EC62979F29220F8400D339EE /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = EC62979E29220F8400D339EE /* main.m */; };
 		EC6297B8292239A700D339EE /* CPDFModel.m in Sources */ = {isa = PBXBuildFile; fileRef = EC6297B7292239A700D339EE /* CPDFModel.m */; };
-		EC6297BD292239C200D339EE /* CPDFController.m in Sources */ = {isa = PBXBuildFile; fileRef = EC6297BC292239C200D339EE /* CPDFController.m */; };
+		EC6297BD292239C200D339EE /* CPDFListController.m in Sources */ = {isa = PBXBuildFile; fileRef = EC6297BC292239C200D339EE /* CPDFListController.m */; };
+		ECC8831D2923352B00F6392E /* CPDFViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = ECC8831C2923352B00F6392E /* CPDFViewController.m */; };
 /* End PBXBuildFile section */
 
 /* Begin PBXCopyFilesBuildPhase section */
@@ -50,8 +51,10 @@
 		EC62979E29220F8400D339EE /* main.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = main.m; sourceTree = "<group>"; };
 		EC6297B6292239A700D339EE /* CPDFModel.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = CPDFModel.h; sourceTree = "<group>"; };
 		EC6297B7292239A700D339EE /* CPDFModel.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = CPDFModel.m; sourceTree = "<group>"; };
-		EC6297BB292239C200D339EE /* CPDFController.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = CPDFController.h; sourceTree = "<group>"; };
-		EC6297BC292239C200D339EE /* CPDFController.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = CPDFController.m; sourceTree = "<group>"; };
+		EC6297BB292239C200D339EE /* CPDFListController.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = CPDFListController.h; sourceTree = "<group>"; };
+		EC6297BC292239C200D339EE /* CPDFListController.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = CPDFListController.m; sourceTree = "<group>"; };
+		ECC8831B2923352B00F6392E /* CPDFViewController.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = CPDFViewController.h; sourceTree = "<group>"; };
+		ECC8831C2923352B00F6392E /* CPDFViewController.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = CPDFViewController.m; sourceTree = "<group>"; };
 /* End PBXFileReference section */
 
 /* Begin PBXFrameworksBuildPhase section */
@@ -123,8 +126,10 @@
 		EC6297B52922393F00D339EE /* Controller */ = {
 			isa = PBXGroup;
 			children = (
-				EC6297BB292239C200D339EE /* CPDFController.h */,
-				EC6297BC292239C200D339EE /* CPDFController.m */,
+				EC6297BB292239C200D339EE /* CPDFListController.h */,
+				EC6297BC292239C200D339EE /* CPDFListController.m */,
+				ECC8831B2923352B00F6392E /* CPDFViewController.h */,
+				ECC8831C2923352B00F6392E /* CPDFViewController.m */,
 			);
 			path = Controller;
 			sourceTree = "<group>";
@@ -199,11 +204,12 @@
 			isa = PBXSourcesBuildPhase;
 			buildActionMask = 2147483647;
 			files = (
+				ECC8831D2923352B00F6392E /* CPDFViewController.m in Sources */,
 				EC62979429220F8300D339EE /* ViewController.m in Sources */,
 				EC6297B8292239A700D339EE /* CPDFModel.m in Sources */,
 				EC62978E29220F8300D339EE /* AppDelegate.m in Sources */,
 				EC62979F29220F8400D339EE /* main.m in Sources */,
-				EC6297BD292239C200D339EE /* CPDFController.m in Sources */,
+				EC6297BD292239C200D339EE /* CPDFListController.m in Sources */,
 				EC62979129220F8300D339EE /* SceneDelegate.m in Sources */,
 			);
 			runOnlyForDeploymentPostprocessing = 0;

BIN
PDFViewer.xcodeproj/project.xcworkspace/xcuserdata/kdan.xcuserdatad/UserInterfaceState.xcuserstate


+ 1 - 1
PDFViewer/Controller/CPDFController.h

@@ -12,7 +12,7 @@
 
 NS_ASSUME_NONNULL_BEGIN
 
-@interface CPDFController : UIViewController
+@interface CPDFListController : UIViewController
 
 @property (nonatomic,copy) NSString *path;
 

+ 24 - 16
PDFViewer/Controller/CPDFController.m

@@ -6,17 +6,18 @@
 //
 
 #import <ComPDFKit/ComPDFKit.h>
-#import "CPDFController.h"
+#import "CPDFListController.h"
 #import "CPDFModel.h"
+#import "CPDFViewController.h"
 
-@interface CPDFController () <UITableViewDataSource,UITableViewDelegate>
+@interface CPDFListController () <UITableViewDataSource,UITableViewDelegate>
 
 @property (nonatomic,strong) UITableView *tableView;
 @property (nonatomic,strong) NSMutableArray *dataArray;
 
 @end
 
-@implementation CPDFController
+@implementation CPDFListController
 
 - (instancetype)initWithModel:(CPDFModel *)model {
     if (self = [super init]) {
@@ -34,7 +35,11 @@
         NSFileManager *defaultManager = [NSFileManager defaultManager];
         NSArray *fileArray = [defaultManager contentsOfDirectoryAtPath:self.path error:nil];
         
-        for (NSInteger i = 1; i < fileArray.count; ++i) {
+        for (NSInteger i = 0; i < fileArray.count; ++i) {
+            
+            if ([fileArray[i] isEqualToString:@".DS_Store"]) {
+                continue;
+            }
             
             NSString *subFilePath = [[self.path stringByAppendingString:@"/"] stringByAppendingString:fileArray[i]];
             
@@ -95,20 +100,23 @@
 
 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
     
-    // Get the path of a PDF
-    NSString *pdfPath = [self.dataArray[indexPath.row] filePath];
-    
-    // Initialize a CPDFDocument object with the path to the PDF file
-    NSURL *url = [NSURL fileURLWithPath:pdfPath];
-    CPDFDocument *document = [[CPDFDocument alloc] initWithURL:url];
+    BOOL isDir = NO;
     
-    // Initialize a CPDFView object with the size of the entire screen
-    CPDFView *pdfView = [[CPDFView alloc] initWithFrame:self.view.bounds];
+    NSFileManager *defaultManager = [NSFileManager defaultManager];
+    [defaultManager fileExistsAtPath:[self.dataArray[indexPath.row] filePath] isDirectory:&isDir];
     
-    // Set the document to display
-    pdfView.document = document;
-    
-    [self.view addSubview:pdfView];
+    if (isDir) {
+        CPDFModel *model = [[CPDFModel alloc] init];
+        model.filePath = [[self.dataArray[indexPath.row] filePath] stringByAppendingString:@"/"];
+        CPDFListController *pdfListController = [[CPDFListController alloc] initWithModel:model];
+        [self.navigationController pushViewController:pdfListController animated:YES];
+        
+    } else {
+        
+        CPDFViewController *pdfViewcontroller = [[CPDFViewController alloc] initWithPath:[self.dataArray[indexPath.row] filePath]];
+        [self.navigationController pushViewController:pdfViewcontroller animated:YES];
+        
+    }
 }
 
 @end

+ 20 - 0
PDFViewer/Controller/CPDFViewController.h

@@ -0,0 +1,20 @@
+//
+//  CPDFViewController.h
+//  PDFViewer
+//
+//  Created by kdan on 2022/11/15.
+//
+
+#import <UIKit/UIKit.h>
+
+NS_ASSUME_NONNULL_BEGIN
+
+@interface CPDFViewController : UIViewController
+
+@property (nonatomic,copy) NSString *path;
+
+- (instancetype)initWithPath:(NSString *)inPath;
+
+@end
+
+NS_ASSUME_NONNULL_END

+ 35 - 0
PDFViewer/Controller/CPDFViewController.m

@@ -0,0 +1,35 @@
+//
+//  CPDFViewController.m
+//  PDFViewer
+//
+//  Created by kdan on 2022/11/15.
+//
+
+
+#import <ComPDFKit/ComPDFKit.h>
+#import "CPDFViewController.h"
+
+@interface CPDFViewController ()
+
+@end
+
+@implementation CPDFViewController
+
+- (instancetype)initWithPath:(NSString *)inPath {
+    if (self = [super init]) {
+        _path = inPath;
+    }
+    return self;
+}
+
+- (void)viewDidLoad {
+    [super viewDidLoad];
+    // Do any additional setup after loading the view.
+    NSURL *url = [NSURL fileURLWithPath:_path];
+    CPDFDocument *document = [[CPDFDocument alloc] initWithURL:url];
+    CPDFView *pdfView = [[CPDFView alloc] initWithFrame:self.view.bounds];
+    pdfView.document = document;
+    [self.view addSubview:pdfView];
+}
+
+@end

+ 7 - 4
PDFViewer/SceneDelegate.m

@@ -7,7 +7,7 @@
 
 #import <ComPDFKit/ComPDFKit.h>
 #import "SceneDelegate.h"
-#import "CPDFController.h"
+#import "CPDFListController.h"
 #import "CPDFModel.h"
 
 @interface SceneDelegate ()
@@ -31,9 +31,12 @@
      CPDFModel *model = [[CPDFModel alloc] init];
     model.filePath = [paths objectAtIndex:0];
     NSLog(@"%@",model.filePath);
-
-    CPDFController *pdfController = [[CPDFController alloc] initWithModel:model];
-    [self.window setRootViewController:pdfController];
+    
+    CPDFListController *pdfController = [[CPDFListController alloc] initWithModel:model];
+    
+    UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:pdfController];
+    pdfController.navigationItem.title = @"Documents";
+    [self.window setRootViewController:navigationController];
 }