Take the 2-minute tour ×

I'm having difficulty debugging this error. I get an EXC_BAD_ACCESS error after I click on Adobe or Nook as the selected app to open the download PDF. I've gone through with the debugger and it breaks somewhere outside of the methods I wrote. Here is the code in question.

        //download protocol
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSString *storedEmail = [defaults stringForKey:@"email"];
    NSString *storedPassword = [defaults stringForKey:@"password"];
    NSString *projectID = self.study.ProjectID;
    NSString *urlString = [NSString stringWithFormat:@"http://service.pharmatech.com/Document/projectprotocol/%@/%@/%@", storedEmail, storedPassword, projectID];
    NSURL *url = [NSURL URLWithString:urlString];
    NSData *data = [NSData dataWithContentsOfURL:url];
    if (data) {
        NSString  *documentsDirectory = [paths objectAtIndex:0];

        NSString  *filePath = [NSString stringWithFormat:@"%@/%@", documentsDirectory, [NSString stringWithFormat:@"Protocol_%@.pdf", projectID]];
        [data writeToFile:filePath atomically:YES];

        UIDocumentInteractionController *docController = [UIDocumentInteractionController interactionControllerWithURL:[NSURL fileURLWithPath:filePath]];
        docController.delegate = self;
        docController.UTI = @"com.adobe.pdf";
        self.navigationController.toolbarHidden = NO;
        BOOL isValid = [docController presentOpenInMenuFromRect:CGRectZero inView:self.view animated:YES];
        NSLog([NSString stringWithFormat:@"DISPLAYED? %d", isValid]);

        NSArray *directoryContent = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:documentsDirectory error:NULL];
        for (int count = 0; count < (int)[directoryContent count]; count++) {
            NSLog(@"File %d: %@", (count + 1), [directoryContent objectAtIndex:count]);

        [self.navigationController setToolbarHidden:YES animated:YES];

The popup shows with the apps capable of showing pdfs. After I click one the Bad Access error occurs, but not until after leaving the method this is called in, which is just a button click action.

If I run the app w/o the debugger is simply closes the my app and opens nothing. Anyone have any tips. I'm still very new to this ios stuff.

EDIT: For those who visit this later and don't read the comments, the answer is that the UIDocumentInteractionController must be an instance variable, not local.

share|improve this question
I don't really know the answer to your question, but it might help to use the CLang Static Analyzer on your source. It's helped me immensely. You select "Analyze" from a submenu of one of the Xcode menus that's towards the right. –  Michael Crawford May 22 '13 at 17:09
iOS Apps don't display anything in the user interface when they crash, but they do produce crash logs that are similar to CrashReporter logs on Mac OS X. If I recall right, you can view them with /Applications/Utilities/Console.app. The logs are stored on the iPhone until they are invisbly transferred to somewhere within ~/Library by Xcode or iTunes. Your beta testers won't have debuggers, but they can email you crash logs. –  Michael Crawford May 22 '13 at 17:11
You need to keep a reference to the UIDocumentInteractionController in an instance variable, not a local variable. –  rmaddy May 22 '13 at 17:13
Yes! Thank you so much. You are awesome. –  erosebe May 22 '13 at 17:41
add comment

1 Answer

up vote 1 down vote accepted

It seems to be a problem with "docController", is a local variable and, i think, must be a instance variable.




docController = [UIDocumentInteractionController interactionControllerWithURL:[NSURL fileURLWithPath:filePath]];

and in your .h declare

UIDocumentInteractionController *docController;
share|improve this answer
add comment

Your Answer


