---
title: "#10: Action Extensions 🎥"
url: https://littlebitesofcocoa.com/10-action-extensions
author: Jake Marsh
date: 2015-05-29
published: 2015-05-29
topics: ["Extensions"]
description: "Action Extensions are incredibly powerful. Not only can they accept many different forms of input data, but they can return data back to the original..."
---

# #10: Action Extensions 🎥

By Jake Marsh, May 29, 2015. HTML: https://littlebitesofcocoa.com/10-action-extensions

_Action Extensions_ are incredibly powerful. Not only can they accept many different forms of input data, but they can return data back to the original application as well. Let's build an _Action Extension_ to help us sound smart while writing. It will accept a word, let us choose a more intelligent sounding word, then return our selection to the original app.

<center>
<img src="https://media.littlebitesofcocoa.com/bite-assets/010-creating-an-action-extension-standalone.gif" style="max-width: 304px; margin: 20px;" />
</center>

First we'll add the extension:

**File** > **New** > **Target...** then use the **Action extension** template.

We grab the input word, then load replacement words:

``` swift
let c = self.extensionContext!
let item = c.inputItems[0] as! NSExtensionItem
let provider = item.attachments![0] as! NSItemProvider
let textType = kUTTypeText as String

if provider.hasItemConformingToTypeIdentifier(textType) {
  provider.loadItemForTypeIdentifier(textType, options: nil) { (string, error) in
    if let word = string as? String {
      self.loadSmarterSoundingWords(word) {
        dispatch_async(dispatch_get_main_queue()) {
          self.tableView.reloadData()
        }
      }
    }
  }
}
```

Finally, we'll return our chosen replacement word back to the original app:

``` swift
func completeWithWord(word: String) {
  var c = self.extensionContext!
  var typeID = kUTTypeText as String
  var provider = NSItemProvider(item: word, typeIdentifier: typeID)

  var item = NSExtensionItem()
  item.attachments = [provider]

  c.completeRequestReturningItems([item], completionHandler: nil)
}

// then inside didSelectRowAtIndexPath:
completeWithWord(words[indexPath.row])
```

[You can download a complete working project here](http://j.mp/bite010).

Run the **MakeMeSmarter** scheme. Select a word to replace, then press the action button.

**Bonus:** The project also shows how to retrieve the value back from an extension.
