UIDocumentInteractionController and file from the Internet

I want to open a file from the Internet in other applications.

My code is:

NSURLRequest *req = [[NSURLRequest alloc] initWithURL:url]; [NSURLConnection sendAsynchronousRequest:req queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *resp, NSData *respData, NSError *error){ NSLog(@"resp data length: %i", respData.length); NSArray *names = [objDict[@"name"] componentsSeparatedByString:@"."]; NSString *fileName = [@"downloaded." stringByAppendingString:names[names.count-1]]; NSString * path = [NSTemporaryDirectory() stringByAppendingPathComponent:fileName]; NSError *errorC = nil; BOOL success = [respData writeToFile:path options:NSDataWritingFileProtectionComplete error:&errorC]; if (success) { UIDocumentInteractionController *documentController = [UIDocumentInteractionController interactionControllerWithURL:[NSURL fileURLWithPath:path]]; documentController.delegate = self; [documentController presentOptionsMenuFromRect:CGRectZero inView:self.view animated:YES]; } else { NSLog(@"fail: %@", errorC.description); } }]; 

The panel is displayed, but it crashes when you press any button (not only "Cancel").

I turned on zombie objects and he writes:

 -[UIDocumentInteractionController URL]: message sent to deallocated instance 
+7
source share
2 answers

Remove the UIDocumentInteractionController from the NSURLConnection sendAsynchronousRequest .

The document interaction controller should remain for a long time after the connection is completed, but it is bound to a block. After the block is completed, there will be no link to it, and it will be freed.

+3
source

I had a similar problem, but I did not initialize my UIDocumentInteractionController inside the block. I solved this by using a property in my interface file so that the class holds the UIDocumentInteractionController instance tightly.

 @property (nonatomic, strong) UIDocumentInteractionController *documentController; 
+5
source

All Articles