iOS11 项目资源里面,查看PDF文档的时候,提示Couldn't issue file extension for path
错误,解决办法就是把资源文件copy到沙盒里面,再从沙盒里面读取文件
#import "TEChargeStandardViewController.h"
#import <QuickLook/QuickLook.h>
@interface TEChargeStandardViewController ()<QLPreviewControllerDataSource,QLPreviewControllerDelegate>
@property (nonatomic,strong) NSURL *filePathUrl;
@end
@implementation TEChargeStandardViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = WhiteColor;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docDir = [paths objectAtIndex:0];
NSString *path = [docDir stringByAppendingPathComponent:@"chargestandard.pdf"];
NSString *path2 = [[NSBundle mainBundle] pathForResource:@"chargestandard.pdf" ofType:nil];
BOOL filesPresent = [self copyMissingFile:path2 toPath:docDir];//copy到沙盒
if (filesPresent) {
NSLog(@"OK");
self.filePathUrl = [NSURL fileURLWithPath:path];
/// 原理是使用自定义控制器,加载QLPreviewController的view,比较方便灵活
if ([QLPreviewController canPreviewItem:(id<QLPreviewItem>)self.filePathUrl]) {
QLPreviewController* previewer = [[QLPreviewController alloc] init];
previewer.dataSource = self;
previewer.delegate = self;
previewer.view.frame = self.view.bounds;
[self.view addSubview:previewer.view];
[self addChildViewController:previewer];
}
}
else
{
NSLog(@"NO");
}
}
#pragma mark --- 返回加载文件个数
- (NSInteger)numberOfPreviewItemsInPreviewController:(QLPreviewController *)controller {
return 1;
}
#pragma mark --- 返回加载路径
- (id <QLPreviewItem>)previewController:(QLPreviewController *)controller previewItemAtIndex:(NSInteger)index {
return self.filePathUrl;
}
//把资源文件copy到沙盒里面
- (BOOL)copyMissingFile:(NSString *)sourcePath toPath:(NSString *)toPath
{
BOOL retVal = YES; // If the file already exists, we'll return success…
NSString * finalLocation = [toPath stringByAppendingPathComponent:[sourcePath lastPathComponent]];
if (![[NSFileManager defaultManager] fileExistsAtPath:finalLocation])
{
retVal = [[NSFileManager defaultManager] copyItemAtPath:sourcePath toPath:finalLocation error:NULL];
}
return retVal;
}
@end