2013-02-26 7 views
7

他のアプリでウェブからファイルを開きたいと思います。UIDocumentInteractionControllerとウェブからのファイル

マイコード:

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 

答えて

3

NSURLConnectionsendAsynchronousRequestブロックのうち、あなたのUIDocumentInteractionControllerしてください。

接続が完了してからドキュメントの対話コントローラは長く留まる必要がありますが、ブロックにスコープが設定されています。ブロックが終了すると、そのブロックへの参照がなくなり、割り当てが解除されます。

5

私は同様の問題がありましたが、ブロック内でUIDocumentInteractionControllerを初期化していませんでした。私はクラスがUIDocumentInteractionControllerのインスタンスを強く保持するように、私のインターフェイスファイルのプロパティを使ってこれを解決しました。

@property (nonatomic, strong) UIDocumentInteractionController *documentController; 
関連する問題