在swift中更改下载文件的路径目录时面临的问题

时间:2015-07-29 09:09:46

标签: swift nsurlconnection nsurlsessiondownloadtask

通过API下载PDF时遇到问题。问题如下:

  1. 我可以通过API链接下载PDF。下载完成。但它的路径是/ Users / *********** / Library / Developer / CoreSimulator / Devices / 58BD307-ED3A-4F34-9FCB-3391FDCC068D / data / Containers / Data / Application / DBB11FA91-2387- 4124-9E07-2E92B22636B5 /文档/ tfData。 如何更改路径?

  2. 我需要将下载的PDF存储在核心数据中,我需要在核心数据的Web视图中显示。我不能这样做。请指导我。

  3. 2.1首先,我不知道如何将下载的PDF显示到我的网页视图中。

    我的编码部分:

    //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 个答案:

答案 0 :(得分: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)
 }