1

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)


        }


    }
Eric Aya
  • 69,473
  • 35
  • 181
  • 253

1 Answers1

0

This is what I did to solve the problem.

You first need to install CocoaPods. Fortunately, CocoaPods is built on Ruby, which ships with all recent versions of Mac OS X. This has been the case since OS X 10.7.

In Terminal Type

sudo gem install cocoapods

Then enter this command in Terminal to complete the setup:

pod setup --verbose

Open Terminal and navigate to the directory that contains your project by using the cd command:

cd ~/Path/To/Folder/Containing/project

Next, enter this command:

pod init

This creates a Podfile for your project. Now we will have to add AlamoFire. (Alamofire is a Swift-based HTTP networking library for iOS and Mac OS X.) We are going to use this to download the file.

Type this command to open the Podfile using Xcode for editing:

open -a Xcode Podfile

Then replace the top lines of the Podfile with this:

source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '9.0'
use_frameworks!

Then you should also make this change:

target 'Project' do

pod 'Alamofire', '~> 3.3'

end

To add Alamofire to your project.

After you have done that save and close the Podfile. Return to terminal and type:

pod install

If the Xcode project is open, close it now and open project.xcworkspace instead. You are going to have to open the .xcworkspace or your project will not run as intended. Next you navigate to the .swift file with the webview you are trying to get a "Open in..." button on. and add this at the top:

import Alamofire

This will give you a error just ignore it and when you run your project it will install and fix the error itself. Next go to your @IBAction func In it add:

 @IBAction func OpenInPressed(sender: AnyObject) {
    var localPath: NSURL?
    Alamofire.download(.GET,
        (WebSiteView.request?.URL?.absoluteString)!,
        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 localPath != nil{
                self.docController = UIDocumentInteractionController(URL: localPath!)

                self.docController.presentOptionsMenuFromBarButtonItem(sender as! UIBarButtonItem, animated: true)

            }
         }
    }

This will let you now get a pdf/docx or any file that you can view on a website and push it to another app. If you want to use a button and not a BarButtonItem Just switch .presentOptionsMenuFromBarButtonItem to .presentOptionsMenuFromRect And then change sender as! UIBarButtonItem to sender.frame