webView中的本地pdf文件

时间:2014-01-30 12:05:06

标签: ios objective-c webview

我试图将本地存储的pdf文件加载到webView Controller,pdf文件位于项目文件旁边。但是webview中根本没有出现任何内容。

- (void)viewDidLoad
{
    webView.delegate = self;
    NSString *indexPath = [NSBundle pathForResource:@"doc" ofType:@"pdf" inDirectory:nil];
    [webView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:indexPath]]];

    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

3 个答案:

答案 0 :(得分:1)

是的,这可以通过UIWebView完成。

如果您尝试在某处显示驻留在服务器上的PDF文件,可以直接将其加载到Web视图中:

UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(10, 10, 200, 200)];

NSURL *targetURL = [NSURL     URLWithString:@"http://developer.apple.com/iphone/library/documentation/UIKit/Reference/UIWebView_Class/UIWebView_Class.pdf"];
NSURLRequest *request = [NSURLRequest requestWithURL:targetURL];
[webView loadRequest:request];

[self.view addSubview:webView];
[webView release];

或者,如果您的应用程序捆绑了PDF文件(在此示例中名为“document.pdf”):

UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(10, 10, 200, 200)];

NSString *path = [[NSBundle mainBundle] pathForResource:@"document" ofType:@"pdf"];
NSURL *targetURL = [NSURL fileURLWithPath:path];
NSURLRequest *request = [NSURLRequest requestWithURL:targetURL];
[webView loadRequest:request];

[self.view addSubview:webView];
[webView release];

您可以在此处找到更多信息:Technical QA1630: Using UIWebView to display select document types.

答案 1 :(得分:0)

试试这个

    - (void)viewDidLoad
    {
        webView.delegate = self;
    NSString *indexPath = [[NSBundle mainBundle] pathForResource:@"doc" ofType:@"pdf" inDirectory:nil];

    [webView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:indexPath]]];

    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

答案 2 :(得分:0)

使用此..

NSString *path = [[NSBundle mainBundle] pathForResource:@"document" ofType:@"pdf"];
NSURL *targetURL = [NSURL fileURLWithPath:path];
NSURLRequest *request = [NSURLRequest requestWithURL:targetURL];
[webView loadRequest:request];