2013-02-26 5 views
7

Ich möchte Datei aus dem Web in anderen Anwendungen öffnen.UIDocumentInteractionController und Datei aus dem Web

Mein Code:

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

Es zeigt Panel, sondern stürzt auf Klick auf eine beliebige Taste (nicht auf 'Abbrechen' nur).

I Zombie-Objekte aktiviert und es schreibt:

-[UIDocumentInteractionController URL]: message sent to deallocated instance 

Antwort

3

Nehmen Sie UIDocumentInteractionController aus dem NSURLConnectionsendAsynchronousRequest Block.

Der Document Interaction Controller muss lange nach dem Abschluss der Verbindung bleiben, ist aber auf den Block beschränkt. Nachdem die Blockierung abgeschlossen ist, gibt es keinen Verweis darauf und die Zuweisung wird aufgehoben.

5

Ich hatte ein ähnliches Problem, aber ich initialisierte nicht meine UIDocumentInteractionController innerhalb eines Blocks. Ich löste dies mit einer Eigenschaft in meiner Interface-Datei, so dass die Klasse die Instanz von UIDocumentInteractionController stark festhalten würde.

@property (nonatomic, strong) UIDocumentInteractionController *documentController; 
Verwandte Themen