2010-07-24 5 views
30

UIDocumentInteractionController의 supported file types에서 클릭을 가로 채고이를 모달보기 (UIDocumentInteractionController의 presentPreviewAnimated: 메서드에서 제공)로 표시하고 싶습니다. 이 모달보기는 "Open In ..."기능을 제공하여 장치의 다른 호환 응용 프로그램에서 이러한 문서를 열 수 있습니다. 아래 코드는 이러한 클릭을 가로 채고 URL 문자열에 적절한 접미사가 있는지 감지하고이 데이터를로드 한 다음 디렉토리에 기록하고 다시 읽고 마지막으로 presentPreviewAnimated: 메소드로 데이터를 사용합니다. UIDocumentInteractionController가 로컬 데이터를 필요로하고 URL에서 직접 데이터를 사용할 수 없기 때문에 먼저 파일 시스템에 데이터를 써야합니다.UIDocumentInteractionController를 사용하여 presentPreviewAnimated를 표시하는 방법 : UIWebView를 통해

#pragma mark - 
#pragma mark Document Interaction Controller Delegate Methods 

- (UIViewController *)documentInteractionControllerViewControllerForPreview:(UIDocumentInteractionController *)controller { 
return self; 
} 

- (void)documentInteractionControllerDidEndPreview:(UIDocumentInteractionController *)controller { 
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentsDirectory = [paths objectAtIndex:0]; 
    NSString *appFile = [documentsDirectory stringByAppendingPathComponent:previewDocumentFileName]; 
NSFileManager *fileManager = [NSFileManager defaultManager]; 
[fileManager removeItemAtPath:appFile error:NULL]; 
//[controller release]; // Release here was causing crashes 
} 

애플 있습니다 : 뷰가 기각 된 후

- (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType { 
if (navigationType == UIWebViewNavigationTypeLinkClicked) { 
    ILog(@"URL loading for NavigationTypeLinkClicked: %@", request.URL.absoluteString); 

    if ([request.URL.absoluteString hasSuffix:@"pdf"] || [request.URL.absoluteString hasSuffix:@"doc"] || [request.URL.absoluteString hasSuffix:@"xls"] || [request.URL.absoluteString hasSuffix:@"ppt"] || [request.URL.absoluteString hasSuffix:@"rtf"] || [request.URL.absoluteString hasSuffix:@"key"] || [request.URL.absoluteString hasSuffix:@"numbers"] || [request.URL.absoluteString hasSuffix:@"pages"]) { 
    if (NSClassFromString(@"UIDocumentInteractionController")) { 
       NSArray *parts = [request.URL.absoluteString componentsSeparatedByString:@"/"]; 
       self.previewDocumentFileName = [parts lastObject]; 
       NSLog(@"The file name is %@", previewDocumentFileName); 

    // Get file online 
       NSData *fileOnline = [[NSData alloc] initWithContentsOfURL:request.URL]; 

    // Write file to the Documents directory 
       NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
       NSString *documentsDirectory = [paths objectAtIndex:0]; 
       if (!documentsDirectory) {NSLog(@"Documents directory not found!");} 
       NSString *appFile = [documentsDirectory stringByAppendingPathComponent:previewDocumentFileName]; 
       [fileOnline writeToFile:appFile atomically:YES]; 
    NSLog(@"Resource file '%@' has been written to the Documents directory from online", previewDocumentFileName); 
    [fileOnline release]; 

    // Get file again from Documents directory 
       NSURL *fileURL = [NSURL fileURLWithPath:appFile]; 

    UIDocumentInteractionController* docController = [UIDocumentInteractionController interactionControllerWithURL:fileURL]; 

    docController.delegate = self; 
    [docController retain]; 

    BOOL result = [docController presentPreviewAnimated:YES]; 

    if (!result) { 
    [docController release]; 
    } 
    return NO; 
    } 
    } 
} 

// Do lots of other URL-detecting stuff here 
} 

가 나는 또한 적절한보기를 사용하고 파일을 삭제하려면 다음 위임 메소드를 구현 (previewDocumentFileName는 클래스 변수입니다) 문서는 문서 제어기를 수동으로 유지해야한다는 사실을 구체적으로 언급 했으므로 그렇게했습니다. 나는 또한 컨트롤러가 사용되지 않았고 (최상위 메소드), 컨트롤러가 사용 된 후에 그것을 풀어 주려고 시도했을 때 (Delegate 메소드),하지만 그 릴리즈가 충돌을 일으켜 제거했다면 그렇게 할 것입니다. 그 견지에서 잘 작동하는 것.

내 배경은이 코드가 iPhone 시뮬레이터에서 작동하는 것처럼 보이지만 (온라인 문서는 presentPreviewAnimated:보기에 올바르게 표시되지만 '열기'는 포함되지 않습니다. .. '대화에서 사용할 수 없다.), iPad sim 또는 실제 iPhone 또는 iPad 기기에 presentPreviewMethod:을 표시하지 않습니다. 메서드가 제대로 호출되고 있다고 생각하지만 presentPreviewAnimated: 메서드는 NO을 반환합니다. 즉, 문서 미리보기를 표시 할 수 없습니다.

왜 이렇게 될까요? 파일을 제대로 저장하지 않으므로 * pdf 또는 * doc 등으로 감지되지 않습니까? 내가 뭘 잘못했을 수 있니? Apple Documentation 외에도 설명서 나 예제 코드가 거의 없으므로이 코드를 100 % 먼저 사용하고 싶지만이 코드가 도움이 될 수 있기를 바랍니다. 미리 감사드립니다. 할당 해제의 방법에

+0

'[docController release]': clang을 켜십시오. 자신이 컨트롤러를 소유하고 있지 않기 때문에 경고가 생성된다는 것을 발견해야합니다. – jww

+0

'[컨트롤러 릴리스];'-이 개체에 대한 책임이 없기 때문에 충돌이 발생합니다. 매개 변수에서 사용자에게 제공됩니다. 그것을 배포 할 수 있도록 할당하지 않았습니다. –

+0

그것은 나를 위해 잘 작동합니다. 감사합니다. Ricky –

답변

-1

당신은 단순히

- (void)dealloc { 
[controller release]; 
controller = nil; 
[super dealloc]; 
} 

그것이 응원을 작동합니다 생각 쓰기!

1

[docController retain][controller release]을 무시하면 [docController autorelease]을 사용하는 것이 좋습니다. 당신이 돌아온 직후에 뭔가를하려고 시도 할 기회가 있지만, 실행 중에는 나중에 할 수 없습니다. 이 경우 컨트롤러 조금 나중에 인 컨트롤러를 해제하려면 autorelease이 대상입니다. 그래도 충돌이 발생하면 개체를 소유하지 않습니다. 하지만, 귀하가 retain 일 경우, 귀하는 release을 책임집니다. 자동 프리 한 후에는 다시 nil으로 설정하여 다시 터치하지 마십시오.

관련 문제