快速更改下载文件的路径目录时面临的问题
Posted
技术标签:
【中文标题】快速更改下载文件的路径目录时面临的问题【英文标题】:Issue facing in changing path directory for downloaded files in swift 【发布时间】:2015-07-29 09:09:46 【问题描述】:通过 API 下载 PDF 时遇到问题。问题如下:
我可以通过我的 API 链接下载 PDF。下载完成。但它的路径是,/Users/***********/Library/Developer/CoreSimulator/Devices/58BD307-ED3A-4F34-9FCB-3391FDCC068D/data/Containers/Data/Application/DBB11FA91-2387- 4124-9E07-2E92B22636B5/文档/tfData。 如何更改路径?
我需要将下载的 PDF 存储在核心数据中,并且我需要从核心数据显示在我的 Web 视图中。我做不到。请指导我。
2.1 首先,我不知道如何将下载的 PDF 显示到我的 web 视图中。
我的编码部分:
//FATAL ERROR RECEIVING
let filemanager = NSFileManager.defaultManager()
let documentsPath : AnyObject = NSSearchPathForDirectoriesInDomains(.DocumentDirectory,.UserDomainMask,true)[0]
let destinationPath:NSString = documentsPath.stringByAppendingString("/tfData.pdf")
println("destinationPath \(destinationPath)")
let pdf = NSURL(fileURLWithPath: destinationPath as String)
println("destinationPath pdf \(pdf)")
let req = NSURLRequest(URL: pdf!)
println("destinationPath req \(req)")
let webView = UIWebView(frame: CGRectMake(20,20,self.view.frame.size.width-40,self.view.frame.size.height-40))
webView.loadRequest(req)
self.view.addSubview(webView)
//NOT DISPLAYING IN WEB VIEW.
日志
destinationPath /Users/***********/Library/Developer/CoreSimulator/Devices/58BD307-ED3A-4F34-9FCB-3391FDCC068D/data/Containers/Data/Application/DBB11FA91-2387-4124-9E07-2E92B22636B5/Documents/tfData.pdf
destinationPath pdf Optional(file:///Users/***********/Library/Developer/CoreSimulator/Devices/58BD307-ED3A-4F34-9FCB-3391FDCC068D/data/Containers/Data/Application/DBB11FA91-2387-4124-9E07-2E92B22636B5/Documents/tfData.pdf)
destinationPath req <NSURLRequest: 0x7f8083706330> URL: file:///Users/***********/Library/Developer/CoreSimulator/Devices/58BD307-ED3A-4F34-9FCB-3391FDCC068D/data/Containers/Data/Application/DBB11FA91-2387-4124-9E07-2E92B22636B5/Documents/tfData.pdf
请指导我。如何解决这个问题?我一个人只能做第一件事。
为什么我这样做意味着,用户也可以在离线模式下阅读 pdf。所以我需要这样。
【问题讨论】:
【参考方案1】:您可以在 webView 中以这种方式加载 PDF:
if let pdf = NSBundle.mainBundle().URLForResource("yourPDFName", withExtension: "pdf", subdirectory: nil, localization: nil)
let req = NSURLRequest(URL: pdf)
let webView = UIWebView(frame: CGRectMake(20,20,self.view.frame.size.width-40,self.view.frame.size.height-40))
webView.loadRequest(req)
self.view.addSubview(webView)
【讨论】:
让我们continue this discussion in chat。以上是关于快速更改下载文件的路径目录时面临的问题的主要内容,如果未能解决你的问题,请参考以下文章