获取相同的路径下载文件,但无法打开该文件

时间:2016-08-30 09:29:30

标签: swift pdf path

所以,我有集合视图,可以从中下载pdf文件,我得到像file:///Users/community/Library/Developer/CoreSimulator/Devices/F9AB746D-20C3-4333-AE70-2840538F4AED/data/Containers/Data/Application/977E843C-15D6-4169-9980-1EBD5CBE3660/Library/Caches/81791.pdf这样的路径它可以查看文件,但是当我使用这样的路径时

Optional(file:///Users/crocodic/Library/Developer/CoreSimulator/Devices/F9AB746D-20C3-4333-AE70-2840538F4AED/data/Contai ... 358.pdf) it can't view the file.

我该怎么办?在快速

这是我的代码

func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
  let mainStoryBoard:UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
  let vc:RedirectMagazineViewController = mainStoryBoard.instantiateViewControllerWithIdentifier("NEXT") as! RedirectMagazineViewController
if (self.localPathArray.count > indexPath.row) {
   vc.receiveData = NSURL(string: databaseResult[0].pathDatabase)! // here i pass the path downloaded file to next view controller
}
   vc.receiveTitle = titleMagazine[indexPath.item]       
   self.navigationController?.pushViewController(vc, animated: true)
}

并在我的第二个视图控制器上查看pdf文件

var path = ""
var receiveData: NSURL!
var receiveTitle: String = ""

override func viewDidLoad() {
    self.navigationController?.navigationBarHidden = false
    self.title = receiveTitle
    activityIndicator.startAnimating()
    self.webView.loadRequest(NSURLRequest(URL: receiveData))
}

1 个答案:

答案 0 :(得分:0)

您可以将pdf下载到文档目录中,将其加载到UIWebView。

if let pdf = NSBundle.mainBundle().URLForResource("81791", withExtension: "pdf", subdirectory: nil, localization: nil)  {
            let request = NSURLRequest(URL: pdf)
            let webView = UIWebView(frame: CGRectMake(20,20,self.view.frame.size.width-40,self.view.frame.size.height-40))
            webView.loadRequest(request)
            self.view.addSubview(webView)
        }
相关问题