iPhone:我们可以使用UIWebView打开pdf文件吗? [英] iPhone : can we open pdf file using UIWebView?

查看:139
本文介绍了iPhone:我们可以使用UIWebView打开pdf文件吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们可以从UIWebView打开pdf文件吗?

Can we open the pdf file from UIWebView ?

如果有人实现了这个,请帮助我。

If any one has implemented this then please help me.

任何代码段或任何网络链接都会有帮助。

Any code snippet or any weblink would help.

推荐答案

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

Yes, this can be done with the UIWebView.

如果您试图在某个地方显示驻留在服务器上的PDF文件,您可以直接在网页视图中加载它:

If you are trying to display a PDF file residing on a server somewhere, you can simply load it in your web view directly:

Objective-C

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

NSURL *targetURL = [NSURL URLWithString:@"https://www.example.com/document.pdf"];
NSURLRequest *request = [NSURLRequest requestWithURL:targetURL];
[webView loadRequest:request];

[self.view addSubview:webView];

Swift

let webView = UIWebView(frame: CGRect(x: 10, y: 10, width: 200, height: 200))

let targetURL = NSURL(string: "https://www.example.com/document.pdf")! // This value is force-unwrapped for the sake of a compact example, do not do this in your code
let request = NSURLRequest(URL: targetURL)
webView.loadRequest(request)

view.addSubview(webView)

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

Or if you have a PDF file bundled with your application (in this example named "document.pdf"):

Objective-C

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

NSURL *targetURL = [[NSBundle mainBundle] URLForResource:@"document" withExtension:@"pdf"];
NSURLRequest *request = [NSURLRequest requestWithURL:targetURL];
[webView loadRequest:request];

[self.view addSubview:webView];

Swift

let webView = UIWebView(frame: CGRect(x: 10, y: 10, width: 200, height: 200))

let targetURL = NSBundle.mainBundle().URLForResource("document", withExtension: "pdf")! // This value is force-unwrapped for the sake of a compact example, do not do this in your code
let request = NSURLRequest(URL: targetURL)
webView.loadRequest(request)

view.addSubview(webView)

您可以在此处找到更多信息:技术QA1630:使用UIWebView显示选择的文档类型

You can find more information here: Technical QA1630: Using UIWebView to display select document types.

这篇关于iPhone:我们可以使用UIWebView打开pdf文件吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆