2012-08-04 120 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来处理,而不是。这是错误信息。

相关问题