我们可以在 iOS 上使用 UIWebView 打开 pdf 文件吗? [英] Can we open pdf file using UIWebView on iOS?

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

问题描述

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

Can we open the pdf file from UIWebView?

推荐答案

是的,这可以通过 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:

目标 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];

迅捷

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"):

目标 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];

迅捷

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)

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

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

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

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