2016-08-22 87 views
1

我试图打开一个.pdf文件,下载后用Alamofire下载。但我见过只使用“webview”。因此,该应用程序消耗大量内存并且不可行。如何在iOS中打开文件?

我想要的是用本机设备应用程序打开它。有什么建议么?谢谢。

编辑:这是我的下载文件中的代码:

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打开文件...

+0

赫德?如果不尝试阅读。我相信多数民众赞成你需要:)快乐编码:) –

+0

你应该显示你已经做了什么。 –

+0

更新操作。我会读@SandeepBhandari – FireUser

回答

1

您应该使用UIDocumentInteractionController。您可以在this Apple文档页面阅读有关它。

通过做一些谷歌搜索,你应该看到甚至一些示例实现。例如here你可以看到一些关于这个由“mattneub”完成的代码。

我让你,你可以使用一个以上代码:

UIDocumentInteractionController以往的
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 
} 
+0

这是否与alamofire localpath一起工作? – FireUser

+1

是的,它也适用于localpath。 –

+0

此行崩溃:let URL:NSURL = NSBundle.mainBundle()。URLForResource(“yourPDF”,withExtension:“pdf”)! ...错误:致命错误:意外地发现零,同时展开一个可选值...线程1 exc_bad_instruction(代码= exc_i386_invop子代码= 0x0) – FireUser