I am trying to make a "Open In..." function with UIDocumentInteractionController()
Using a UIwebview
.
My goal is to have users be able to navigate a website and press the "Open In..." button to push a PDF/DOCX from a website to another app like Noteability.
What I'm trying to do doing in my code is downloading the page that the user is currently at and then try to open that file in the UIDocumentInteractionController()
to be pushed to another app.
It does not seem to work, and I need help figuring out what I have to do.
Here is the code I'm using.
class DetailViewController: UIViewController, UIWebViewDelegate, UITextFieldDelegate, UIPopoverPresentationControllerDelegate, SFSafariViewControllerDelegate {
@IBOutlet weak var WebSiteView: UIWebView!
...
var docController: UIDocumentInteractionController!
...
@IBAction func OpenInPressed(sender: AnyObject) {
let yourURL = WebSiteView.request?.URL!
let urlRequest = NSURLRequest(URL: yourURL!)
let theData = try? NSURLConnection.sendSynchronousRequest(urlRequest, returningResponse: nil)
var docURL = (NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask)).last!
docURL = docURL.URLByAppendingPathComponent( "myFileName.pdf")
theData?.writeToURL(docURL, atomically: true)
docController = UIDocumentInteractionController(URL: docURL)
docController.presentOptionsMenuFromRect(sender.frame, inView:self.view, animated:true)
}
}