Don_Mag icon

Using QLPreviewController

Don_Mag | PRO | 10/24/22 04:12:57 PM UTC | 0 ⭐ | 12807 👁️ | Never ⏰ | []
Objective C |

0 B

|

None

|

0 👍

/

0 👎

//
//  ViewController.m
//  QLPreview
//
//  Created by Don Mag on 10/24/22.
//
 
#import "ViewController.h"
 
@import QuickLook;
 
/*
 *  Quicklook Preview Item
 */
@interface PreviewItem : NSObject <QLPreviewItem>
@property(readonly, nullable, nonatomic) NSURL    *previewItemURL;
@property(readonly, nullable, nonatomic) NSString *previewItemTitle;
@end
@implementation PreviewItem
- (instancetype)initPreviewURL:(NSURL *)docURL
                     WithTitle:(NSString *)title {
    self = [super init];
    if (self) {
        _previewItemURL = [docURL copy];
        _previewItemTitle = [title copy];
    }
    return self;
}
@end
/***/
 
/*
 *  QuickLook Datasource for rending PDF docs
 */
@interface PDFDataSource : NSObject <QLPreviewControllerDataSource>
@property (strong, nonatomic) PreviewItem *item;
@end
@implementation PDFDataSource
- (instancetype)initWithPreviewItem:(PreviewItem *)item {
    self = [super init];
    if (self) {
        _item = item;
    }
    return self;
}
- (NSInteger)numberOfPreviewItemsInPreviewController:(QLPreviewController *)controller {
    return 1;
}
- (id<QLPreviewItem>)previewController:(QLPreviewController *)controller previewItemAtIndex:(NSInteger)index {
    return self.item;
}
@end
/***/
 
@interface ViewController ()
 
@property (strong, nonatomic) PDFDataSource *pdfDatasource;
 
@end
 
@implementation ViewController
 
- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = UIColor.systemBlueColor;
    
}
 
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    
    /*
     *  get the path to the pdf resource.
     */
    
    NSString *path =
    [[NSBundle mainBundle] pathForResource:@"article"
                                    ofType:@"pdf"];
    
    if (!path) {
        NSLog(@"Could not find article.pdf in bundle!");
        return;
    }
    
    NSURL *docURL = [NSURL fileURLWithPath:path];
    
    
    /*
     *  create the Quicklook controller.
     */
    
    QLPreviewController *qlController =
    [[QLPreviewController alloc] init];
    
    PreviewItem *item =
    [[PreviewItem alloc] initPreviewURL:docURL
                              WithTitle:@"Article"];
    self.pdfDatasource =
    [[PDFDataSource alloc] initWithPreviewItem:item];
    
    qlController.dataSource = self.pdfDatasource;
    
    
    /*
     *  present the document.
     */
    
    [self presentViewController:qlController
                       animated:YES completion:nil];
}
 
@end
 

Comments

  •  icon
    01/01/70 12:00:00 AM UTC
    Plain Text |

    0 B

    |

    👍

    /

    👎

    
        
  •  icon
    01/01/70 12:00:00 AM UTC
    Plain Text |

    0 B

    |

    👍

    /

    👎

    
        
  •  icon
    01/01/70 12:00:00 AM UTC
    Plain Text |

    0 B

    |

    👍

    /

    👎