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 に答える