Recently in the project to do a document preview of the function, do it with the original iOS Qlpreviewcontroller class, here to do a record share
First introduce header file
#import <QuickLook/QuickLook.h>
Follow agent
Qlpreviewcontrollerdatasource
Declare a Qlpreviewcontroller variable
@property (Strong, nonatomic) Qlpreviewcontroller *previewcontroller;
Then declare a Nsurl object that holds the file path to return
@property (copy, nonatomic) Nsurl *fileurl;
Class in Viewdidload.
-(void) viewdidload {
[super viewdidload];
Self.previewcontroller = [[Qlpreviewcontroller alloc] init];
Self.previewController.dataSource = self;
}
Write the following code in the event to preview the local file
Gets the local file path
self.fileurl = [Nsurl fileurlwithpath:[[nsbundle mainbundle]pathforresource:@ "Libihua couplets appreciation. Doc" OfType: Nil]];
[Self PresentViewController:self.previewController animated:yes completion:nil];
Follow proxy methods
#pragma mark-qlpreviewcontrollerdatasource
-(id<qlpreviewitem>) Previewcontroller: (Qlpreviewcontroller *) Controller Previewitematindex: (Nsinteger) Index {return
self.fileurl;
}
-(Nsinteger) Numberofpreviewitemsinpreviewcontroller: (Qlpreviewcontroller *) previewcontroller{return
1;
}
So a preview of the local file becomes
When you add a file to a project, be aware that after you put the file on a project, you add the file to the resource pool in the copy Bundle resources of build phases, or you can't find the file and crash when you use it
Next is the preview of the network file
Write the following code in the event preview of the network file (AF is required)
NSURLSessionConfiguration * configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
AFURLSessionManager * manager = [[AFURLSessionManager alloc] initWithSessionConfiguration: configuration];
Nsstrings * urlStr = @ "https://www.tutorialspoint.com/ios/ios_tutorial.pdf";
NSString *fileName = [urlStr lastPathComponent]; // gets the file name
NSURL *URL = [NSURL URLWithString:urlStr];
NSURLRequest *request = [NSURLRequest request with URL:URL];
// judge whether it exists
If ([self isFileExist: fileName]) {
NSURL * documentsDirectoryURL = [[NSFileManager defaultManager] URLForDirectory: NSDocumentDirectory inDomain: NSUserDomainMask appropriateForURL: nil create: NO error: nil];
NSURL * url = [documentsDirectoryURL URLByAppendingPathComponent: fileName];
Self. FileURL = url;
[the self presentViewController: self previewController animated: YES completion: nil];
} else {
[SVProgressHUD showWithStatus: @download];
NSURLSessionDownloadTask * downloadTask = [manager downloadTaskWithRequest: request progress: ^ (NSProgress * downloadProgress) {
Destination :^NSURL *(NSURL *targetPath, NSURLResponse *response) {
NSURL * documentsDirectoryURL = [[NSFileManager defaultManager] URLForDirectory: NSDocumentDirectory inDomain: NSUserDomainMask appropriateForURL: nil create: NO error: nil];
NSURL * url = [documentsDirectoryURL URLByAppendingPathComponent: fileName];
Return the url;
} completionHandler:^(NSURLResponse *response, NSURL *filePath, NSError *error) {
[SVProgressHUD dismiss];
Self. FileURL = filePath;
[the self presentViewController: self previewController animated: YES completion: nil];
}];
[downloadTask resume];
}
Whether to add a file has a judgment method
Determine if the file already exists in the sandbox
-(BOOL) Isfileexist: (NSString *) fileName
{
Nsarray *paths = Nssearchpathfordirectoriesindomains (NSDocumentDirectory, Nsuserdomainmask, YES);
NSString *path = [Paths objectatindex:0];
NSString *filepath = [path stringbyappendingpathcomponent:filename];
Nsfilemanager *filemanager = [Nsfilemanager Defaultmanager];
BOOL result = [FileManager Fileexistsatpath:filepath];
return result;
}
So the network file preview becomes
Enclosed demo Address