Xcode 6 Swift - 显示本地 PDF
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27675764/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Xcode 6 Swift - Displaying a local PDF
提问by StopReadingThisUsername
I wanted to be able to display a local PDF in my app but couldn't find an effective method to do so. I have already tried creating a Webview and downloading the PDF each time the app runs, but I would like for the app to display a local version. I found multiple tutorials on YouTube, however, they were all in Objective-C. Here are the links (in objective-c) of something similar to what I'm trying to do:
我希望能够在我的应用程序中显示本地 PDF,但找不到有效的方法。我已经尝试在每次应用程序运行时创建 Webview 并下载 PDF,但我希望该应用程序显示本地版本。我在 YouTube 上找到了多个教程,但是,它们都是使用 Objective-C 编写的。这是类似于我正在尝试做的事情的链接(在objective-c中):
https://www.youtube.com/watch?v=TtKoddECri0&spfreload=10https://www.youtube.com/watch?v=lgu8HbTsY1M
https://www.youtube.com/watch?v=TtKoddECri0&spfreload=10 https://www.youtube.com/watch?v=lgu8HbTsY1M
I'm new to programming in general, so it would be great if your steps would be as detailed as possible. Thanks in advance.
一般来说,我是编程新手,所以如果您的步骤尽可能详细,那就太好了。提前致谢。
回答by Krunal Darji
1.Inside your Storyboard ViewController add Web View from Object Library
1.在你的 Storyboard ViewController 中从 Object Library 添加 Web View
2.Right Click on Web View and drag & drop new referencing layout to your swift class , give some meaning full name (e.g..pdfWebView)
2.右键单击Web视图并将新的引用布局拖放到您的swift类中,赋予一些含义全名(例如.pdfWebView)
3.Inside View Controller add below code to load local PDF :
3.Inside View Controller 添加以下代码以加载本地 PDF:
var pdfLoc = NSURL(fileURLWithPath:NSBundle.mainBundle().pathForResource("PDF_file", ofType:"pdf")!) //replace PDF_file with your pdf die name
var request = NSURLRequest(URL: pdfLoc);
self.pdfWebView.loadRequest(request);
回答by birdy
the same within WKWebView programatically:
以编程方式在 WKWebView 中相同:
let wkWebView: WKWebView = {
let v = WKWebView()
v.disableAutoresizingMasks()
let pdfURL = NSURL(fileURLWithPath:NSBundle.mainBundle().pathForResource("PDF_file", ofType:"pdf")!)
let request = NSURLRequest(URL: pdfURL)
v.loadRequest(request)
return v
}()