2

I have a test PDF file that I want to open in iBooks through my app. I saved it in my temp directory and I use this code to load it to iBooks:

NSURL *targetURL = [NSURL fileURLWithPath:tempFullPath];
NSLog(@"Path is %@", tempFullPath);
UIDocumentInteractionController *controller = [UIDocumentInteractionController interactionControllerWithURL:targetURL];
controller.delegate = self;
controller.UTI = @"com.adobe.pdf";

[controller presentOpenInMenuFromRect:self.view.bounds inView:self.view animated:YES];

The menu pops up just fine but when I tap the iBooks button the app crashes and hangs my Xcode.

The path to the file I get in my NSLog output is like this:

Path is /private/var/mobile/Applications/65EC4182-A79B-431C-9E74-BD72D91A31AB/tmp/TestFile.pdf

What am I doing wrong? Thanks in advance!

Sergey Grischyov
  • 11,995
  • 20
  • 81
  • 120
  • 1
    What is the error shown in the console? What is the stack trace? Where does your app crash? – rmaddy Mar 27 '13 at 16:27
  • @rmaddy app crashes exactly after I hit the iBooks icon in the `UIDocumentInteractionController`pop up and I only get `Thread 1: EXC_BAD_ACCESS (code = 1, address = blah blah)` message in Xcode. – Sergey Grischyov Mar 27 '13 at 16:28
  • 1
    Enable zombies and see if a message is being sent to a deallocated object. Most likely the problem is that you don't keep a reference to the `UIDocumentInteractionController` so it is being deallocated too soon. – rmaddy Mar 27 '13 at 16:31
  • @rmaddy You were totally right! Please create an answer out of your comment so I can accept it! – Sergey Grischyov Mar 27 '13 at 16:34
  • Changing documentInteractionController = [UIDocumentInteractionController interactionControllerWithURL:fileURL]; to documentInteractionController = [[UIDocumentInteractionController interactionControllerWithURL:fileURL] retain]; fixed my problem. Thanks @rmaddy – Alejandro Luengo Mar 29 '13 at 17:51
  • making the `documentInteractionController` a global variable might also have solved the problem with the crash. But the `iBooks` apps still doesn't loads. – geekay May 28 '14 at 13:16

1 Answers1

8

The use of a UIDocumentInteractionController requires that you keep a reference around until it is complete. This means you should use an instance variable, not a local variable. Implement the proper delegate methods so you can reset the ivar when you are done with the controller.

Enabling zombies will help debug such an issue. Most likely you will see that a message is being sent to a deallocated object (your controller).

rmaddy
  • 314,917
  • 42
  • 532
  • 579
  • Exactly, use this controller as UIPopoverController, must retain it until completing! – Tony Jun 23 '13 at 05:47
  • @rmaddy how can i use? can you please suggest me – Vvk Nov 17 '16 at 18:07
  • @rmaddy worked like a charm. Its party time, Thanks for saving my time. @ Vvk, Just declare your object of UIDocumentInteractionController global not a local variable. and then try again. It works for me. – Ravi Jan 25 '21 at 07:43