1

I've have a share extension for an iOS app that has a custom UI. When running the extension the storyboard is loaded and the UI presented OK, but beginRequestWithExtensionContext isn't called.

The (simplified) view controller code:

class ShareViewController: UIViewController, NSExtensionRequestHandling {

    // This is called…
    override func viewDidLoad() {
        super.viewDidLoad() 
    }

    // …but this isn't!
    override func beginRequestWithExtensionContext(context: NSExtensionContext) {            
        println("beginRequestWithExtensionContext")
    }
}

NSExtensionAttributes:

<dict>
    <key>NSExtensionAttributes</key>
    <dict>
        <key>NSExtensionActivationRule</key>
        <dict>
            <key>NSExtensionActivationSupportsImageWithMaxCount</key>
            <integer>1</integer>
        </dict>
    </dict>
    <key>NSExtensionPointIdentifier</key>
    <string>com.apple.share-services</string>
    <key>NSExtensionMainStoryboard</key>
    <string>Share</string>
</dict>
</plist>

Any thoughts as to why beginRequestWithExtensionContext might not be getting called?

Ashley Mills
  • 50,474
  • 16
  • 129
  • 160

1 Answers1

2

OK, so after 2 days of going backwards and forwards with this, I figured it out 30 mins after posting a question on SO! So, in case anyone else has the same problem…

The storyboard has a UINavigationController as its initial view controller, so beginRequestWithExtensionContext was, of course, fired on that rather than ShareViewController.

I subclassed UINavigationController to resolve the issue:

class ShareNavigationController: UINavigationController, NSExtensionRequestHandling {

    override func beginRequestWithExtensionContext(context: NSExtensionContext) {

        super.beginRequestWithExtensionContext(context)

        (self.topViewController as! ShareViewController).handleContext(context)
    }
}
Ashley Mills
  • 50,474
  • 16
  • 129
  • 160
  • The same problem happens when adding a storyboard as a Main Interface to a Safari Web Extension. Calls to beginRequestWithExtensionContext() vanish. – Graham Leggett Nov 07 '21 at 18:29