0

I'm, unsuccessfully, trying to load a PDF on iOS, when debugging the code the document shows "0x0" as value.

NSString *appFolderPath = [[NSBundle mainBundle] resourcePath];
NSString *pdfPath = [appFolderPath stringByAppendingString:@"/Data/Raw/test.pdf"];
    
CFStringRef cfsPath = (__bridge CFStringRef)pdfPath;

CFURLRef url = CFURLCreateWithFileSystemPath(NULL, cfsPath, kCFURLPOSIXPathStyle, 0);
    
CGPDFDocumentRef document = CGPDFDocumentCreateWithURL(url);

What could be wrong with this snippet? I don't have much experience with iOS native development and Objective-C.

  • Hi Edu - in stead of all those string gymnastics, why not use ```NSURL``` in stead? Replace your URL with that and life will be a lot easier. As for the error, I suspect your path may be wrong or there may be no document there. – skaak Aug 04 '20 at 17:49

1 Answers1

0

First of all check if the file exists at the given location in the application bundle.

It's highly recommended to use the URL related API of NSBundle

NSURL *appFolderURL = [[NSBundle mainBundle] resourceURL];
NSURL *pdfURL = [appFolderURL URLByAppendingPathComponent:@"Data/Raw/test.pdf"];

And use the more convenient PDFDocument class of PDFKit rather than the CoreFoundation API

@import PDFKit;

PDFDocument *document = [[PDFDocument alloc] initWithURL:pdfURL];
vadian
  • 274,689
  • 30
  • 353
  • 361