[英]Unable to create ReaderDocument Vfr-Reader
我進行了很多搜索,但無法從Documents文件夾中使用vfr-reader打開PDF。
NSString *filePath = @"/Users/***/Library/Application Support/iPhone Simulator/5.0/Applications/F2B7E9DE-9996-4F05-BC81-2A2889B4F504/Documents/Number1.pdf";
ReaderDocument *document = [ReaderDocument withDocumentFilePath:filePath password:password];
if (document != nil)
{// document comes nil here
ReaderViewController *readerViewController = [[ReaderViewController alloc] initWithReaderDocument:document];
readerViewController.delegate = self; // Set the ReaderViewController delegate to self
[self.navigationController pushViewController:readerViewController animated:YES];
}
我確定文件路徑與pdf文件完全相同。
在閱讀器的示例代碼中,它從主包中打開pdf。 但是我需要從資源文件夾中打開。
謝謝
我面臨着同樣的問題,也許你也有同樣的問題。
如果您不使用ARC,則只需將-fobjc-arc寫入構建界面中的每個pdf閱讀器文件。 那將解決您的問題。
您應該使用[[NSBundle mainBundle] bundlePath]
和stringByAppendingPathComponent:
而不是對字符串進行硬編碼。 這太可怕了,只能在iOS Simulator上使用。
您應該給文件名像下面的代碼,不要直接給
NSArray *pathss = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsPaths = [pathss objectAtIndex:0];
NSString *filePaths = [documentsPaths stringByAppendingPathComponent:[NSString stringWithFormat:@"%@",fileName]];
我知道這是一篇過時的文章,但是我遇到了與@ user1392687類似的問題,並希望分享我如何解決此問題(我從各種目錄加載文件,而不僅僅是Documents文件夾)。
問題:從目錄中加載一系列PDF文件,用文件名和支持的元數據填充表格視圖,然后在選擇單元格后,使用VFR Reader打開PDF文件。
解決方案:X-Code中的文件夾是文件夾參考,用於啟用內容更新,而不必執行組參考的刪除/添加循環。 下面的函數用於讀取特定文件夾路徑的所有內容(URL),然后刪除返回的文件路徑中包含的所有/任何simlink。 在將URL傳遞到VRF以加載PDF文件之前,將URL用於RFC 1808(未轉義)路徑。
+ (NSArray *)enumerateContentsOfFolderWithPath:(NSURL *)aFolderPath
{
NSError *error = nil;
NSArray *contentProperties = @[NSURLIsDirectoryKey,
NSURLIsReadableKey,
NSURLCreationDateKey,
NSURLContentAccessDateKey,
NSURLContentModificationDateKey];
NSArray *contents = [[NSFileManager defaultManager] contentsOfDirectoryAtURL:aFolderPath
includingPropertiesForKeys:contentProperties
options:NSDirectoryEnumerationSkipsHiddenFiles
error:&error];
if (error != nil)
DLog(@"Content enumeration error: %@", error);
NSMutableArray *pdfURLs = [NSMutableArray array];
for (NSURL *item in contents)
{
NSURL *fileURL = [NSURL fileURLWithPath: [item path]];
NSURL *noSimlink = [fileURL URLByResolvingSymlinksInPath];
[pdfURLs addObject: noSimlink];
}
return pdfURLs;
}
在用文件夾的內容和所有支持的元數據填充表格視圖之后,並且當用戶觸摸一行以查看PDF文件時,VRF閱讀器的設置如下:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// Other setup code...
NSURL *item = [pdfURLs objectAtIndex:(NSUInteger) indexPath.row];
[self presentPdfViewerForItem: item];
}
- (void)presentPdfViewerForItem:(NSURL *)aItem
{
NSString *phrase = nil; // Document password (for unlocking most encrypted PDF files)
NSString *filePath = [aItem path];
ReaderDocument *document = [ReaderDocument withDocumentFilePath: filePath password:phrase];
if (document != nil) // Must have a valid ReaderDocument object in order to proceed
{
ReaderViewController *readerViewController = [[ReaderViewController alloc] initWithReaderDocument:document];
readerViewController.delegate = self;
readerViewController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
readerViewController.modalPresentationStyle = UIModalPresentationFullScreen;
[self presentViewController:readerViewController animated:YES completion:nil];
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.