[英]How to open file in iOS?
我正在尝试在下载后打开一个.pdf
文件,该文件是使用Alamofire下载的。 但我见过只使用“webview”。 因此,该应用程序消耗大量内存并且不可行。
我想要的是用本机设备应用程序打开它。 有什么建议? 谢谢你。
编辑:这是我下载文件的代码:
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 文档页面上阅读它。
通过进行一些谷歌搜索,您甚至应该会看到一些示例实现。 例如在这里你可以看到一些由“ 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
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.