UIDocumentInteractionController crashing upon exit
Asked Answered
U

1

7

I have a regular UIButton on my main menu that currently launches a UIViewController; the contents of the corresponding .m file is as follows:

-(id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];

    documentPath = [[NSBundle mainBundle] pathForResource:@"file" ofType:@"pdf"];
    NSURL *targetURL = [NSURL fileURLWithPath:documentPath];

    document = [UIDocumentInteractionController interactionControllerWithURL: targetURL];
    document.delegate = self;
    [document retain];

    return self;
}

-(UIViewController *)documentInteractionControllerViewControllerForPreview: (UIDocumentInteractionController *) controller
{
    return self;
}

-(void)documentInteractionControllerDidEndPreview:(UIDocumentInteractionController *)controller
{
    [document autorelease];
}

-(void)viewDidLoad
{
    [super viewDidLoad];

    [document presentPreviewAnimated: YES]; // ** CRASH **
}

-(void)viewDidUnload
{
    [super viewDidUnload];
}

-(void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return YES;
}

-(void)dealloc
{
    [super dealloc];
}

My pdf file loads as expected, however when I hit the "done" button the document closes and I am left staring at my blank UIViewController - arguably as expected. But if I hit the navigation "back" button then the app crashes with a bad access error inside my viewDidLoad method, where the call to presentPreviewAnimated is found.

If somebody could please take a look, I would be most grateful.

(btw, there is no NIB file when this view controller is created. Yes, this in itself is wrong)

Unexpected answered 27/5, 2011 at 13:17 Comment(9)
I think your problem may be with the autorelease you're doing in the delegate (together with the retain in the initWithNib method).Waterworks
@Waterworks Without the retain it will crash with the following: Line 1: -[__NSCFType presentPreviewAnimated:]: unrecognized selector sent to instance 0x1a32e0 Line 2: *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFType presentPreviewAnimated:]: unrecognized selector sent to instance 0x1a32e0' The reason I added the retain was because Apple tell us to in the UIDocumentInteractionControllerDelegate protocol reference.Unexpected
Hmm... I use UIDocument quite a bit in one of my apps, don't do a retain and have no issues. What if you move the UIDocument code from initWithNib to viewDidLoad?Waterworks
Removing the autorelease and retain I had in, and moving the remaining 4 lines to viewDidLoad produces the same bad access error when I hit the "done" button. If I may ask, is your method to open a document the same/similar to what I have done so far?Unexpected
Yes, although I do it in response to a user action in a view and not during view creation.Waterworks
Should I instead create a UIView inside this UIViewController and try to get the document inside the view? Then I assume you just add the document controller as a subview of the new UIView?Unexpected
I am wondering whether the issue is that you do this during view creation. So that when the user closes the document preview, it gets back to an incompletely formed UIView. So perhaps first build and load the view and then do UIDocument from viewDidAppear ?Waterworks
Perfect my friend! Putting the init code for the UIDocumentInteractionController inside viewDidAppear: alongside a BOOL that tracked whether it was the first time viewed or not has worked perfectly. If it's the first view, the document is created and pushed. Once I hit "done," viewDidAppear fires again except that now my BOOL reads as NO, (not the first view) so I call popToRootViewControllerAnimated: which works well. Thank you for your support with this query.Unexpected
If you add your second to last comment as an answer, I can flag this as solved :)Unexpected
W
1

I am wondering whether the issue is that you do this during view creation. So that when the user closes the document preview, it gets back to an incompletely formed UIView. So perhaps first build and load the view and then do UIDocument from viewDidAppear ?

Waterworks answered 27/5, 2011 at 15:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.