2

I'm trying to open a .pdf file after download which is downloaded with Alamofire. But I've seen only using a "webview". Thus the application consumes lots of memory and is not viable.

What I want is to open it with the native device application. Any suggestions? Thank you.

Edit: This is my code for download file:

 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() } } 

I need open file from localpath...

3
  • Heard of UIDocumentInteractionController ever ?? If not try reading it. I believe thats what you need :) Happy coding :) Commented Aug 22, 2016 at 6:25
  • You should show what have you done already. Commented Aug 22, 2016 at 6:27
  • update op. I will read @SandeepBhandari Commented Aug 22, 2016 at 6:41

1 Answer 1

2

You should use UIDocumentInteractionController. You can read about it on this Apple documentation page.

By doing some Googling you should see even some example implementations. For example here you can see some code about this done by "mattneub".

I let you one more code that you can use:

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 } 
Sign up to request clarification or add additional context in comments.

4 Comments

crash in this line: let URL: NSURL = NSBundle.mainBundle().URLForResource("yourPDF", withExtension: "pdf")! ...error: fatal error: unexpectedly found nil while unwrapping an Optional value... thread 1 exc_bad_instruction (code=exc_i386_invop subcode=0x0)
You had nil in the line. But happy to help. Happy coding :)
One more question. This works with a pdf file in the root directory of the application but if I use localpath(file:///Users/informatica/Library/Developer/CoreSimulator/Devices/6ABF0CE7-FE77-4D6F-9331-120A344B7396/data/Containers/Data/Application/847D3998-5A9D-4741-9A54-DC71C9225F98/Documents/109.pdf) It does not work or I did not implement it well. I need a real device or can be performed with the emulator ?.
It is harder to perform it on simulator. Try on real device instead.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.