UIDocumentInteractionController crashing upon exit

2019-02-22 04:48发布

问题:

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)

回答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 ?