2014-10-13 11 views
0

iBooks의 응용 프로그램 내에 로컬로 저장된 PDF 파일을 열려고합니다. 이 응용 프로그램은 현재 테이블 뷰에 "문학"목록을 가지고 있으며 각 셀을 탭하면 PDF 파일을 표시하는 webView와 연결됩니다 (훌륭한 기능). 나는 사용자가 iBooks에서 PDF를 열 수 있도록 navBar의 오른쪽 위에 BarButton을 만들었습니다 (그래서 그들은 그/그녀의 장치에 저장할 수 있습니다).iBooks에서 로컬 PDF 파일 열기

지금까지 버튼은 파일을 열 수있는 기기의 모든 앱을 표시하는 UIDocumentInteractionController을 엽니 다 (iBooks가 설치되어 있는지 확인한 후). 그런 다음 iBooks 아이콘을 클릭하면 앱이 다운됩니다. iBooks가 손상되지 않고 열리도록 코드를 수정할 수 있었지만 PDF 파일이 옮겨지지 않아 무의미했습니다 (코드가 충돌 할 때 다시 되돌아갑니다). 아래

코드합니다 ... barButton의 IBAction를 내부에 아이폰 OS 8 파일 시스템의 레이아웃에

NSString *path = [[NSBundle mainBundle] pathForResource:litFileName ofType:@"pdf"]; 
NSURL *targetURL = [NSURL fileURLWithPath:path]; 

UIDocumentInteractionController *docController = [UIDocumentInteractionController interactionControllerWithURL:targetURL]; 

if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"itms-bookss:"]]) 
{ 
    [docController presentOpenInMenuFromRect:CGRectZero inView:self.view animated:YES]; 
    NSLog(@"ibooks is installed"); 
} 
else 
{ 
    NSLog(@"no ibooks installed"); 
} 

답변

3

을 고정 : 여기

는 파일 경로를 만드는 방법입니다! 프로젝트에서 약간의 시간을 가져 갔고 2 주 후에 다시 돌아와서15 분에 알아 냈습니다.

docController의 메모리 문제이며 .h 파일에서 선언하고 (retain) 사용하면 완벽하게 작동합니다. 또한 [NSBundle mainBundle] 메서드도 사용할 수있었습니다. 그렇게하는 방법

.h 
@property (retain)UIDocumentInteractionController *docController; 

.m 
@synthesize docController; 

//in bar button IBAction 
NSString *path = [[NSBundle mainBundle] pathForResource:litFileName ofType:@"pdf"]; 
NSURL *targetURL = [NSURL fileURLWithPath:path]; 

docController = [UIDocumentInteractionController interactionControllerWithURL:targetURL]; 

if([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"itms-books:"]]) { 

    [docController presentOpenInMenuFromRect:CGRectZero inView:self.view animated:YES]; 
    NSLog(@"iBooks installed"); 

} else { 

    NSLog(@"iBooks not installed"); 

} 
1

변경하고 기본 번들이 더 이상 작동에서 직접 파일을 공유하지 않습니다. 파일을 문서 디렉토리에 복사하고 거기에서 공유하십시오. 그것을

NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject]; 
NSString *fileName = [NSString stringWithFormat:@"%@.pdf",litFileName]; 
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:fileName]; 
NSURL *url = [NSURL fileURLWithPath:filePath]; 
+0

확실하지 정확히 ... 나는 시도 :있는 NSArray * 경로 =의 NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES); NSString * documentsDirectory = [경로 objectAtIndex : 0]; NSString * filePath = [documentsDirectory stringByAppendingString : [NSString stringWithFormat : @ "% @ .pdf", litFileName]]; NSURL * url = [NSURL fileURLWithPath : filePath]; SAME ERROR – RyanZ

+0

경로에 파일을 잘못 추가했다면 문자열을 추가하는 것뿐만 아니라'stringByAppendingPathComponent :'를 사용해야합니다. – rckoenes