Fabian Maucher Fabian Maucher - 1 year ago 79
iOS Question

Import PDF into own App with iOS Action Extension

I'm looking for a possibility to import a PDF in order to do some further tasks with it, just like described in this Question: Importing PDF files to app

After two days of looking around in the inter webs I found that an action extension might be the solution, this is how far I got:

override func viewDidLoad() {

let fileItem = self.extensionContext!.inputItems.first as! NSExtensionItem

let textItemProvider = fileItem.attachments!.first as! NSItemProvider

let identifier = kUTTypePDF as String

if textItemProvider.hasItemConformingToTypeIdentifier(identifier) {

textItemProvider.loadItemForTypeIdentifier(identifier, options: nil, completionHandler: handleCompletion)

func handleCompletion(pdfFile: NSSecureCoding?, error: NSError!) {

print("PDF loaded - What to do now?")

The completion handler is called properly so I assume the PDF is loaded - but then I don't now how to proceed. If the action extension only handles images or text it could easily be downcasted, but the only way to work with files I know is with path names - which I do not have and don't know how to obtain. Plus, I'm pretty sure Sandboxing is also part of the party.

I guess I only need a push in the right direction which Class or Protocol could be suitable for my need - any suggestions highly appreciated.

Answer Source

For anyone else looking for an answer - I found out by myself, and it's embarrassingly easy:

func handleCompletion(fileURL: NSSecureCoding?, error: NSError!) {

    if let fileURL = fileURL as? NSURL {

        let newFileURL = NSURL(fileURLWithPath: NSTemporaryDirectory().stringByAppendingString("test.pdf"))

        let fileManager = NSFileManager.defaultManager()

        do {
            try fileManager.copyItemAtURL(fileURL, toURL: newFileURL)
            // Do further stuff
        catch {
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download