我试图.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()
}
}
我需要从localpath打开文件...
您应该使用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
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句