2012-08-04 114 views
2

enter image description here如何閱讀webview中Resourse文件夾子文件夾中的PDF文件?

我在我的application.i國土資源文件夾的子文件夾的一些PDF文檔甲酸已嘗試這樣way.here是我的代碼讀取這些文件。

NSString *path = [[NSBundle mainBundle] pathForResource:chapter ofType:@"pdf"]; 
    NSURL *targetURL = [NSURL fileURLWithPath:path]; 
    NSURLRequest *request = [NSURLRequest requestWithURL:targetURL]; 
    [[webView scrollView] setContentOffset:CGPointMake(0,500) animated:YES]; 
    [webView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"window.scrollTo(0.0, 50.0)"]]; 
    [webView loadRequest:request]; 
    [self.view addSubview:webView]; 
    [webView release]; 

在這段代碼中的「路徑」返回Nill值,當我在console.but檢查當我檢查字符串「章」,它存儲我的願望的PDF文檔名,務必將其擁有的PDF文檔名之一。在控制檯中,當我的程序崩潰時,它顯示了一些這樣的事情。

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 
'-[UIWebView scrollView]: unrecognized selector sent to instance 0x6587d20' 

任何幫助將得到通過。

+0

這裏的pathForResource是字符串,並檢查你提供了正確的字符串。你提供的是哪一章?它是字符串嗎? – 2012-08-04 13:01:32

+0

章是包含我的願望文件名的字符串。 – jamil 2012-08-04 13:04:07

+0

remvove this line [webView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@「window.scrollTo(0.0,50。0)「]];刪除這一行作爲你的設置內容偏移量爲500 – 2012-08-04 13:25:47

回答

3

你在你的項目中籤出你的pdf文件嗎?

enter image description here

我測試下面的代碼。沒問題。

請檢查您的項目中的chapter.pdf文件。該點是相對路徑。

必須區分大小寫。 「章節」/「章節」不是equalString。

NSLog(@"path:%@",[[NSBundle mainBundle] pathForResource:@"chapter" ofType:@"pdf"]); 


console: 
    /var/mobile/Applications/72AFA069-D83E-469A-A687-AEAFDB0B4D97/TableViewTest.app/chapter.pdf 

NSLog(@"path:%@",[[NSBundle mainBundle] pathForResource:@"Chapter" ofType:@"pdf"]); 


console: 
    (null) 

self.myWebView = [[[UIWebView alloc] initWithFrame:webFrame] autorelease]; 
self.myWebView.backgroundColor = [UIColor whiteColor]; 
self.myWebView.scalesPageToFit = YES; 
self.myWebView.delegate = self; 
[self.view addSubview:self.myWebView]; 
[self.myWebView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"chapter" ofType:@"pdf"]]]]; 

enter image description here


在項目文件夾的名稱將在實際的iPhone捆綁消失。 您已創建一個名爲Resource-Common的文件夾,但最終會消失在NSBundle中。

enter image description here

見下面的圖片。兩者都是完整的。項目中的文件夾名稱只是xcode中的可見文件夾。

enter image description here

如果你想在一個NSBundle所有PDF文件列表。嘗試遵循代碼。

NSArray *pdfCollection = [[NSBundle mainBundle] pathsForResourcesOfType:@"pdf" inDirectory:nil]; 

enter image description here

+0

必須區分大小寫」章節「/」章節「不等於字符串 – 2012-08-04 12:09:21

+0

你是否在鏈接中下載代碼?知道問題是什麼 – 2012-08-04 12:48:06

+0

什麼是章節???請嘗試以下內容。NSString * str = @「your pdf file name」; // NSString * path = [[NSBundle mainBundle] pathForResource:str ofType:@「pdf 「]; – 2012-08-04 13:03:40

0

的UIWebView的滾動視圖是按照蘋果的doc只讀屬性:與Web視圖關聯的滾動視圖。 (只讀)。但你把它當作一個UIScrollView來處理,而不是。這是錯誤信息。

相關問題