7

我想在其他应用程序中从网络打开文件。

我的代码:

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);
    }
}];

它显示面板,但在单击任何按钮时崩溃(不仅仅是“取消”)。

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

-[UIDocumentInteractionController URL]: message sent to deallocated instance
4

2 回答 2

5

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

@property (nonatomic, strong) UIDocumentInteractionController *documentController;
于 2013-03-21T01:57:21.073 回答
3

把你UIDocumentInteractionController带出NSURLConnection sendAsynchronousRequest街区。

连接完成后,文档交互控制器必须保留很长时间,但它的作用域是块。块完成后,将不再引用它,它将被释放。

于 2013-02-26T21:35:37.430 回答