单击按钮时,如何显示可以读取pdf文件的所有应用程序(例如adobe pdf reader)?我搜索但我发现大多数人使用UIWebView来显示pdf文件。我怎么能用我描述的方式来做呢?
修改 我只有从服务器获取的pdf链接
答案 0 :(得分:1)
试试这个
var docController:UIDocumentInteractionController!
let pdfUrl = NSURL(string: "ENTER_URL_OF_PDF")
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
downloadDoc(pdfUrl: pdfUrl!)
}
@IBAction func buttonAction(_ sender: AnyObject) {
docController.presentOptionsMenu(from: self.view.frame, in: self.view, animated: true)
}
func downloadDoc(pdfUrl : NSURL) {
let urlTest = self.pdfUrl!.absoluteString
let pdfUrl = NSURL(string: urlTest!)
if(pdfUrl != nil){
let pdfRequest: NSURLRequest = NSURLRequest(url: pdfUrl! as URL)
NSURLConnection.sendAsynchronousRequest(pdfRequest as URLRequest, queue: OperationQueue.main) {(response, data, error) in
let httpResponse = response as? HTTPURLResponse
if(httpResponse?.statusCode == 200 && error == nil){
let documentsUrl = FileManager.default.urls(for: FileManager.SearchPathDirectory.documentDirectory, in: FileManager.SearchPathDomainMask.userDomainMask).first as! NSURL
if let fileName = self.pdfUrl!.lastPathComponent {
let destinationUrl = documentsUrl.appendingPathComponent(fileName)
if let data = data {
do {
try data.write(to: destinationUrl!, options: .atomic)
} catch {
print(error)
}
self.docController = UIDocumentInteractionController(url: destinationUrl!)
}
}
}
}
}
}
答案 1 :(得分:0)
你可以使用 UIDocumentInteractionController ,它会处理所有你喜欢缩放pdf,滚动,显示合适的应用来处理pdf。
SWIFT 2.3:
import UIKit
class ViewController:UIViewController, UIDocumentInteractionControllerDelegate {
var documentController: UIDocumentInteractionController = UIDocumentInteractionController()
override func viewDidLoad() {
super.viewDidLoad()
downloadFileForfileObject("https://d0.awsstatic.com/whitepapers/KMS-Cryptographic-Details.pdf")
}
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
}
func downloadFileForfileObject(url: String) { //Download pdf File asynchronosly
let documentURL = NSURL(string: url)
let documentsURLPath = NSFileManager.defaultManager().URLsForDirectory(.CachesDirectory, inDomains: .UserDomainMask).first! as NSURL
let fileExtension = ((documentURL!.pathComponents)?.last)! as String
let request: NSURLRequest = NSURLRequest(URL: documentURL!, cachePolicy: NSURLRequestCachePolicy.ReturnCacheDataElseLoad, timeoutInterval: 60)
let fileURLPath = documentsURLPath.URLByAppendingPathComponent("\(fileExtension)")
let sessionCobfig = NSURLSessionConfiguration()
let session = NSURLSession(configuration: sessionCobfig, delegate: nil, delegateQueue: nil)
let task = session.dataTaskWithRequest(request) { (data, response, error) in
if error == nil {
self.openSelectedDocumentFromURL((fileURLPath?.path!)!)
} else {
print(error?.localizedDescription)
}
}
task.resume()
}
func openSelectedDocumentFromURL(documentURLString: String) {
let documentURL: NSURL = NSURL(fileURLWithPath: documentURLString)
documentController = UIDocumentInteractionController(URL: documentURL)
documentController.delegate = self
documentController.presentPreviewAnimated(true)
}
// MARK: - UIDocumentInteractionViewController delegate methods
func documentInteractionControllerViewControllerForPreview(controller: UIDocumentInteractionController) -> UIViewController {
return self
}
}
使用 pdf url 作为参数,在viewDidLoad方法中调用downloadFileForfileObject()。 pdf将由UIDocumentInteractionController委托方法自动显示。
SWIFT 3:
import UIKit
class MOViewController:UIViewController, UIDocumentInteractionControllerDelegate {
var documentController: UIDocumentInteractionController = UIDocumentInteractionController()
override func viewDidLoad() {
super.viewDidLoad()
downloadFileForfileObject(url: "https://d0.awsstatic.com/whitepapers/KMS-Cryptographic-Details.pdf")
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
}
func downloadFileForfileObject(url: String) { //Download pdf File asynchronosly
let documentURL = NSURL(string: url)
let documentsURLPath = FileManager.default.urls(for: .cachesDirectory, in: .userDomainMask).first! as NSURL
let fileExtension = ((documentURL!.pathComponents)?.last)! as String
let request: URLRequest = URLRequest(url: documentURL! as URL, cachePolicy: NSURLRequest.CachePolicy.returnCacheDataElseLoad, timeoutInterval: 60)
let fileURLPath = documentsURLPath.appendingPathComponent("\(fileExtension)")
let sessionConfig = URLSessionConfiguration.default
let session = URLSession(configuration: sessionConfig, delegate: nil, delegateQueue: nil)
let teask = session.dataTask(with: request) { (data, response, error) in
if (error == nil) {
// Success
self.openSelectedDocumentFromURL(documentURLString: fileURLPath!.path)
} else {
print(error?.localizedDescription)
}
}
teask.resume()
}
func openSelectedDocumentFromURL(documentURLString: String) {
let documentURL: NSURL = NSURL(fileURLWithPath: documentURLString)
documentController = UIDocumentInteractionController(url: documentURL as URL)
documentController.delegate = self
documentController.presentPreview(animated: true)
}
// MARK: - UIDocumentInteractionViewController delegate methods
func documentInteractionControllerViewControllerForPreview(_ controller: UIDocumentInteractionController) -> UIViewController {
return self
}
文档下载验证 您可以按照以下方式检查文档是否正在下载,请参阅图像。
<强>输出:强>
感谢:)