IOS uses Qlpreviewcontroller to preview local and network files

Source: Internet
Author: User


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














Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.