来自web的UIDocumentInteractionController和文件

wer*_*ary 7 xcode cocoa-touch ios

我想在其他应用程序中从Web打开文件.

我的代码:

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);
    }
}];
Run Code Online (Sandbox Code Playgroud)

它显示面板,但在点击任何按钮时崩溃(不仅仅是"取消").

我启用了僵尸对象,它写道:

-[UIDocumentInteractionController URL]: message sent to deallocated instance
Run Code Online (Sandbox Code Playgroud)

小智 5

我遇到了类似的问题,但我没有在块中初始化我的UIDocumentInteractionController.我使用我的接口文件中的属性解决了这个问题,以便该类强烈地保留UIDocumentInteractionController的实例.

@property (nonatomic, strong) UIDocumentInteractionController *documentController;
Run Code Online (Sandbox Code Playgroud)


DBD*_*DBD 3

带你UIDocumentInteractionController走出NSURLConnection sendAsynchronousRequest

连接完成后,文档交互控制器必须保持很长时间,但其范围仅限于块。块完成后,将不再有对其的引用,并且它将被释放。