2016-08-30 44 views
0

所以,我有收集視圖,並可以從那裏下載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)) 
} 
+0

請顯示您嘗試的代碼,它看起來像您正在使用您的路徑var可選包裝。 –

+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) 
     } 
相關問題