2013-02-26 16 views
7

Tôi muốn mở tệp từ web trong các ứng dụng khác.UIDocumentInteractionController và tệp từ web

Mã của tôi:

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

Nó cho thấy bảng điều khiển, nhưng tai nạn trên bấm vào nút bất kỳ (không phải là 'Hủy' chỉ).

tôi kích hoạt đối tượng zombie và nó viết:

-[UIDocumentInteractionController URL]: message sent to deallocated instance 

Trả lời

3

Hãy UIDocumentInteractionController ra khỏi khối NSURLConnectionsendAsynchronousRequest của bạn.

Bộ điều khiển tương tác tài liệu phải ở lại lâu sau khi kết nối hoàn tất, nhưng nó được đưa vào khối. Sau khi khối kết thúc, sẽ không có tham chiếu đến nó và nó sẽ được deallocated.

5

Tôi gặp sự cố tương tự, nhưng tôi đã không khởi tạo UIDocumentInteractionController của mình bên trong một khối. Tôi đã giải quyết vấn đề này bằng cách sử dụng một thuộc tính trong tệp giao diện của tôi để lớp đó sẽ nắm giữ thể hiện của UIDocumentInteractionController một cách mạnh mẽ.

@property (nonatomic, strong) UIDocumentInteractionController *documentController; 
Các vấn đề liên quan