如何在iOS中打开文件?
我试图打开一个.pdf
文件下载后与Alamofire下载。 但是我只看到使用“webview”。 因此,应用程序消耗大量的内存,不可行。
我想要的是用本机设备应用程序打开它。 有什么build议么? 谢谢。
编辑:这是我的代码下载文件:
var localPath: NSURL? Alamofire.download(.GET, url, destination: { (temporaryURL, response) in let directoryURL = NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask)[0] let pathComponent = response.suggestedFilename localPath = directoryURL.URLByAppendingPathComponent(pathComponent!) return localPath! }) .response { (request, response, _, error) in if error != nil { // got an error in getting the data, need to handle it print("Error: \(error!)") } //print(response) print("Download file en:\(localPath!)") self.view.hideToastActivity() //self.actioncall() } }
我需要从本地打开文件…
你应该使用UIDocumentInteractionController
。 你可以在这个 Apple文档页面上阅读。
通过做一些谷歌search,你甚至应该看到一些例子的实现。 例如, 在这里你可以看到一些关于“ mattneub ”完成的代码。
我再给你一个你可以使用的代码:
var documentInteractionController: UIDocumentInteractionController! @IBAction func openDocument(sender: UIButton) { let URL: NSURL = NSBundle.mainBundle().URLForResource("yourPDF", withExtension: "pdf")! if (URL != "") { // Initialize Document Interaction Controller self.documentInteractionController = UIDocumentInteractionController(URL: URL) // Configure Document Interaction Controller self.documentInteractionController.delegate = self // Present Open In Menu self.documentInteractionController.presentOptionsMenuFromRect(sender.frame, inView: self.view, animated: true) //presentOpenInMenuFromRect } } // UIDocumentInteractionControllerDelegate func documentInteractionControllerViewControllerForPreview(controller: UIDocumentInteractionController) -> UIViewController { return self }