Search code examples
iosswiftios-extensions

Create iOS Action Extension with no UI


I am trying to create an Action Extension similar to the system "Copy" action available in iOS.
I found different answers saying it's not possible to have non-fullscreen UI, but according to Apple Official Documentation it is possible to have no UI (like in the Copy action, I presume).

Action (iOS and macOS; UI and non-UI variants)

I have tried creating a transparent view, but the result is always a fullscreen black overlay.
I have already specified NSExtensionActionWantsFullScreenPresentation to NO in my Info.plist, but nothing changes.

Any idea about how to do it?


Solution

  • To answer my own question: it is actually possible to create a non-UI Action Extension on iOS by assigning the property NSExtensionPrincipalClass to a class implementing a NSExtensionRequestHandling protocol.

    Example:

    class ActionRequestHandler: NSObject, NSExtensionRequestHandling {
    
        var extensionContext: NSExtensionContext?
    
        func beginRequest(with context: NSExtensionContext) {
            // Do not call super in an Action extension with no user interface
            self.extensionContext = context
    
            // Do stuff with the context
    
        }
    }
    

    The easiest way to create an extension like this is to add a new target (File > New > Target), select Action Extension and then select No User Interface in the Action Type.