简体   繁体   中英

iOS Action Extension, share PDF from Safari fails

I have an Action Extension to which I'm trying to share PDF-files.

I'm using the boilerplate code for ActionRequestHandler.swift that was autogenerated for me:

func beginRequest(with context: NSExtensionContext) {
    // Do not call super in an Action extension with no user interface
    self.extensionContext = context
    for item in context.inputItems as! [NSExtensionItem] {
            if let attachments = item.attachments {
                for itemProvider in attachments {
                ...
                ...
                }
             }
    }
}

Working from other apps

When exporting from every application except Safari, this is what I get:

分享好

This is all ok, I can verify that it's an pdf by checking the com.adobe.pdf and then I use the public.file-url to fetch the shared file.

Failing from Safari

But when exporting from Safari (doesn't matter if I choose "Automatic" or "Pdf" for file type), I instead only get com.apple.property-list :

分享失败

Further info

Both dropbox and OneDrive works, so it's doable in some sort of way.

Also I realised that sharing an PDF from a url that's protected by some sort of login doesn't work with "Public.file-url" since that URL wont be accessible from inside swift-code.

That leads me to think that the java-script preprocessor might be the way to go? Fetch the pdf-contents with JS and pass it on to code?

Question

How do I use the com.apple.property-list to fetch the file? Or is some config I did faulty, since I get this property-list instead of the pdf/url combo?

While I didn't manage to figure out a solution to the original question, I did manage to solve the problem.

When adding an Action Extension, one gets to choose Action type :

  • Presents user interface
  • No user interface

I choosed No user interface since that was what I wanted.

That gave me an Action.js file and ActionRequestHandler.swift :

class ActionRequestHandler: NSObject, NSExtensionRequestHandling {
 ...
}

These files seem to work around a system where the Action.js is supposed to fetch/manipulate the source page and then send information to the backing Swift code. As stated in my original question, when sharing a PDF from Safari, no PDF-URL gets attached.

A working solution

If I instead choose Presents user interface , I got another setup, ActionViewController.swift :

class ActionViewController: UIViewController {
 
override func viewDidLoad() {
    super.viewDidLoad()

    // Get the item[s] we're handling from the extension context.

    for item in self.extensionContext!.inputItems as! [NSExtensionItem] {
        for provider in item.attachments! {
            if provider.hasItemConformingToTypeIdentifier(kUTTypePDF as String) {
                provider.loadItem(forTypeIdentifier: kUTTypePDF as String, options: nil, completionHandler: { (pdfUrl, error) in
                    OperationQueue.main.addOperation {
                            if let pdfUrl = pdfUrl as? URL {
                            // pdfUrl now contains the path to the shared pdf data
                    }
                }
          }
      }
 }

This file / solution works as expected, the extensionContext gets populated with one attachment that conforms to kUTTypePDF as expected.

Why this works, while the "no gui"-approach doesn't, I have no idea. Bug or feature?

I have not found any documentation of how/why this is supposed to work in Apple's developer section, the "share extension" documentation is very light.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM