2013-02-26 2 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

을 가지고 당신의 UIDocumentInteractionControllerNSURLConnectionsendAsynchronousRequest 블록 중.

연결이 완료된 후에 문서 상호 작용 컨트롤러가 오래 있어야하지만 블록에 범위가 있습니다. 블록이 끝나면 참조가 없으므로 블록이 할당 해제됩니다.

5

비슷한 문제가 있었지만 블록 내에서 UIDocumentInteractionController를 초기화하지 않았습니다. 클래스가 UIDocumentInteractionController 인스턴스를 강력하게 유지할 수 있도록 내 인터페이스 파일의 속성을 사용하여이 문제를 해결했습니다.

@property (nonatomic, strong) UIDocumentInteractionController *documentController; 
관련 문제