1

I have file test.pdf in my Application Bundle. I run test in iPhone Simulator (iPad) and I cannot get the CGPDFDocumentRef populated. Am I going crazy or what?!?

here is a piece of code from the sample application made especially for this (ViewController class):

- (void)viewDidLoad {
    [super viewDidLoad];
    // below code to show files in app bundle...
 NSString *path = [[NSBundle mainBundle] resourcePath];
 NSFileManager *fm = [NSFileManager defaultManager];
 NSError *error = [[NSError alloc] init];
 NSArray *directoryAndFileNames = [fm contentsOfDirectoryAtPath:path error:&error];

 //just to make sure the file is there : (test.pdf)
 for (NSString *ts in directoryAndFileNames) {
  NSLog(ts);  // NSLog confirms the file is there
 }

 //Function below is supposed to create the CDPDFDocumentRef
 [self updatePageRef]; // try to get the CGPDFDocumentRef
}

-(BOOL)updatePageRef {
 NSLog(@"Updating PAGE");

 NSString *pdfStringURL = [[NSBundle mainBundle] pathForResource:@"test" ofType:@"pdf"];
 NSURL *pdfURL = [NSURL fileURLWithPath:pdfStringURL];
 NSLog([pdfURL path]); // this is good

 NSLog(@"File Exists = %d", [[NSFileManager defaultManager] fileExistsAtPath:pdfStringURL]); // this gives me 1 (YES)

 pdf = CGPDFDocumentCreateWithURL((CFURLRef)pdfURL);    
    //from here on the pdf is nil??????????!!!!!!!!!!!!!

 if (pdf && CGPDFDocumentGetNumberOfPages(pdf) > 0) {
  return YES;
 }

 return NO;
}

Above the pdf is declared in the @interface as CGPDFDocumentRef pdf; I know the PDF file is good, because I have opened it successfully in 'preview'.

Everything compiles and runs without any warnings nor errors.

Help!

Brad Larson
  • 170,088
  • 45
  • 397
  • 571
Sasho
  • 3,532
  • 1
  • 31
  • 30
  • Why are you creating a blank error object there? You just need to pass the pointer to the variable where `contentsOfDirectoryAtPath:error:` should store the error object *it* creates if it fails. – Peter Hosey Aug 27 '10 at 21:16
  • You are absolutely right. The piece of code showing files in bundle I have pasted from somewhere on the net. However your comment is not getting me closer to a solution to my prob. – Sasho Aug 27 '10 at 22:05

1 Answers1

1

I have found a solution, sorry for asking on first place. The PDF was not good somehow although it was showing perfectly in 'preview'. I have put another pdf, named it 'test.pdf' and all went well.

Cheers!

Sasho
  • 3,532
  • 1
  • 31
  • 30