Advertisement
Don_Mag

Using QLPreviewController

Oct 24th, 2022
2,532
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //
  2. //  ViewController.m
  3. //  QLPreview
  4. //
  5. //  Created by Don Mag on 10/24/22.
  6. //
  7.  
  8. #import "ViewController.h"
  9.  
  10. @import QuickLook;
  11.  
  12. /*
  13.  *  Quicklook Preview Item
  14.  */
  15. @interface PreviewItem : NSObject <QLPreviewItem>
  16. @property(readonly, nullable, nonatomic) NSURL    *previewItemURL;
  17. @property(readonly, nullable, nonatomic) NSString *previewItemTitle;
  18. @end
  19. @implementation PreviewItem
  20. - (instancetype)initPreviewURL:(NSURL *)docURL
  21.                      WithTitle:(NSString *)title {
  22.     self = [super init];
  23.     if (self) {
  24.         _previewItemURL = [docURL copy];
  25.         _previewItemTitle = [title copy];
  26.     }
  27.     return self;
  28. }
  29. @end
  30. /***/
  31.  
  32. /*
  33.  *  QuickLook Datasource for rending PDF docs
  34.  */
  35. @interface PDFDataSource : NSObject <QLPreviewControllerDataSource>
  36. @property (strong, nonatomic) PreviewItem *item;
  37. @end
  38. @implementation PDFDataSource
  39. - (instancetype)initWithPreviewItem:(PreviewItem *)item {
  40.     self = [super init];
  41.     if (self) {
  42.         _item = item;
  43.     }
  44.     return self;
  45. }
  46. - (NSInteger)numberOfPreviewItemsInPreviewController:(QLPreviewController *)controller {
  47.     return 1;
  48. }
  49. - (id<QLPreviewItem>)previewController:(QLPreviewController *)controller previewItemAtIndex:(NSInteger)index {
  50.     return self.item;
  51. }
  52. @end
  53. /***/
  54.  
  55. @interface ViewController ()
  56.  
  57. @property (strong, nonatomic) PDFDataSource *pdfDatasource;
  58.  
  59. @end
  60.  
  61. @implementation ViewController
  62.  
  63. - (void)viewDidLoad {
  64.     [super viewDidLoad];
  65.    
  66.     self.view.backgroundColor = UIColor.systemBlueColor;
  67.    
  68. }
  69.  
  70. - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
  71.    
  72.     /*
  73.      *  get the path to the pdf resource.
  74.      */
  75.    
  76.     NSString *path =
  77.     [[NSBundle mainBundle] pathForResource:@"article"
  78.                                     ofType:@"pdf"];
  79.    
  80.     if (!path) {
  81.         NSLog(@"Could not find article.pdf in bundle!");
  82.         return;
  83.     }
  84.    
  85.     NSURL *docURL = [NSURL fileURLWithPath:path];
  86.    
  87.    
  88.     /*
  89.      *  create the Quicklook controller.
  90.      */
  91.    
  92.     QLPreviewController *qlController =
  93.     [[QLPreviewController alloc] init];
  94.    
  95.     PreviewItem *item =
  96.     [[PreviewItem alloc] initPreviewURL:docURL
  97.                               WithTitle:@"Article"];
  98.     self.pdfDatasource =
  99.     [[PDFDataSource alloc] initWithPreviewItem:item];
  100.    
  101.     qlController.dataSource = self.pdfDatasource;
  102.    
  103.    
  104.     /*
  105.      *  present the document.
  106.      */
  107.    
  108.     [self presentViewController:qlController
  109.                        animated:YES completion:nil];
  110. }
  111.  
  112. @end
  113.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement