QLPreviewController的使用

在iOS SDK包括了QLPreviewControllerAPI在[iOS]中用于方便预览文件,它支持的文件有:
iWork文档
微软Office97以上版本的文档
RTF文档
PDF文件
图片文件
文本文件和CSV文件

1.导入库
image.png
2.代码部分
#import "ViewController.h"
#import 

@interface ViewController ()
@property (nonatomic, strong) NSArray *array;
@end

@implementation ViewController
- (IBAction)did:(id)sender {
    QLPreviewController *vc = [QLPreviewController new];
    vc.dataSource = self;
    vc.delegate = self;
    [self presentViewController:vc animated:YES completion:nil];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // Do any additional setup after loading the view, typically from a nib.
}

/*!
 * @abstract Returns the number of items that the preview controller should preview.
 * @param controller The Preview Controller.
 * @result The number of items.
 */
- (NSInteger)numberOfPreviewItemsInPreviewController:(QLPreviewController *)controller;
{
    return self.array.count;
}

/*!
 * @abstract Returns the item that the preview controller should preview.
 * @param controller The Preview Controller.
 * @param index The index of the item to preview.
 * @result An item conforming to the QLPreviewItem protocol.
 */
- (id )previewController:(QLPreviewController *)controller previewItemAtIndex:(NSInteger)index;
{
    NSURL *url = [NSURL URLWithString:self.array[index]];
    return url;
}

- (NSArray *)array {
    return @[
             @"https://b.pdf",
             @"https://a.jpg",
             @"https://a.jpg"
             ];
}

@end

3.bug

找到.entitlements文件,将其中App Sandbox项置为NO
Error Domain=NSPOSIXErrorDomain Code=1 "Operation not permitted"

你可能感兴趣的:(QLPreviewController的使用)