Question

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) {
        NSArray       *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        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.

Was it helpful?

Solution

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

Change

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

for

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

and in your .h declare

UIDocumentInteractionController *docController;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top