Search code examples
iosios8ios-app-extension

Accessing extensionContext from a presented view controller


I've tried creating a custom view controller for a share extension.

A confusing situation happens when I present another view controller on top of the initial view controller that was set on the MainInterface.storyboard. This presented view controller is embedded in a navigation controller (it's the root view controller of it).

I did a check on the presentingViewController

(lldb) po [self presentingViewController]

<_UIViewServiceViewControllerOperator: 0x7a978000>

(lldb) po [[self presentingViewController] extensionContext]

nil

So, the extension context is nil at this point. I could access the extensionContext by passing it around from the presentingViewController to the presentedViewController.

But, I found this behavior is a bit strange. Is the app extension designed to only be accessed from one level of view controller hierarchy?


Solution

  • If you're going to use more than a single view controller in your extension storyboard, you'll have to pass a reference to the extensionContext of the original view controller to the view controller that will ultimately be responsible for completing the extension's request. In the initial view controller:

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        let destination = segue.destinationViewController as! FinalViewController
        destination.originalExtensionContext = self.extensionContext
    }
    

    And in your final view controller:

    @IBAction func dismissController(sender: UIButton!) {
        dismissViewControllerAnimated(true) { () -> Void in
            self.originalExtensionContext.completeRequestReturningItems(self.originalExtensionContext.inputItems, completionHandler: nil)
    }
    

    Note that you have to create a uniquely named property for the original extension context, since extensionContext already exists as a property name on the superclass UIViewController. You can't pass the existing extensionContext to the UIViewController's property extensionContext as it is a read-only attribute.