Create embedded extension

Create a new Single View Application (MyViewController)

Find the ‘+’ sign ‘Add a target’

Screen Shot 2016-09-29 at 7.07.55 AM.png

Click the iMessage Extension and give it a name (MyMessageExtension)

Screen Shot 2016-09-29 at 7.08.18 AM.png

Go ahead and Activate the scheme

Screen Shot 2016-09-29 at 7.09.46 AM.png

Should now see a new target representing your extension.

Screen Shot 2016-09-29 at 7.10.02 AM.png

If you run you should now see

Screen Shot 2016-09-29 at 7.10.53 AM.png

Screen Shot 2016-09-29 at 7.11.44 AM.png

Add the navigate back to host action

Add a button and outlet action to so when pressed extension redirects back to host.

Screen Shot 2016-09-29 at 7.16.38 AM.png

And add the follow code to the button action

import UIKit
import Messages

class MessagesViewController: MSMessagesAppViewController {

    @IBAction func hostPressed(_ sender: AnyObject) {
        let selectedName = "Jonathan"
        let URL = NSURL(string: "extension://name=\(selectedName)")!
        extensionContext!.open(URL as URL) { success in
            print("openURL returned: \(success)")

What this code is saying, is we are going to navigate back to our host, by creating a URL and passing a single variable in it.

Screen Shot 2016-09-29 at 7.18.50 AM.png

Now if you run this right now it won’t yet work. We need to register this URL scheme with our host application so it knows to wake up and receive URLs that start with ‘extension’.

Add URL schema to host

Click on your main Single Page View application target and click your Info.plist

Screen Shot 2016-09-29 at 7.23.17 AM.png

Screen Shot 2016-09-29 at 7.24.30 AM.png

This next part is tricky because adding URLs is a pain with the editor. But this is the URL schema we want to create is this. So go ahead.

Screen Shot 2016-09-29 at 7.25.21 AM.png

Or do as follows

Move your mouse over ‘Information Property List’ until you see a plus sign form on the right. Click it.

Screen Shot 2016-09-29 at 7.46.41 AM.png

Then start typing URL types and then hit return

Screen Shot 2016-09-29 at 7.46.50 AM.png

Then click the arrow pointing right, beside URL types, to expand it

Screen Shot 2016-09-29 at 7.49.37 AM.png

Click the right pointing arrow again, this time on Item 0 and add your extension value there.

Screen Shot 2016-09-29 at 7.50.44 AM.png

To add the value to host on Item 0 hit the ‘+’ sign to the right of Item 0 (adding a row) and start typing URL Schemes

Screen Shot 2016-09-29 at 7.53.24 AM.png

Then expand the arrow to the left of your scheme and double click the value column to enter your value there

Screen Shot 2016-09-29 at 7.55.51 AM.png

You should now be ready. Phew!

We can now go back to our extension target, run it, and when we press the button we should navigate from our embedded extension back to our host application.

You will know you are successful if you start here

Screen Shot 2016-09-29 at 7.36.21 AM.png

and you end up here (you can add a label to your host application if you want to verify you made it back.

Screen Shot 2016-09-29 at 7.36.29 AM.png

Note! Don’t worry if the simulator dies when you hit the button navigating back. That’s expected. The simulator kills the extension when you navigate back. You are now changing application back to your host application. So that’s OK.

If you want to see if run smoothly install it on your phone and try it there. Should work no problem.

Additional notes

The bundle identifiers between the host app and the extension must be similar.

Host can be

Screen Shot 2017-01-20 at 5.32.59 AM.png

While extension can be

Screen Shot 2017-01-20 at 5.33.32 AM.png

Also the thing you want to look for when hooking these things up is this

Screen Shot 2017-01-20 at 5.34.27 AM.png

You want to see the extension embedded in the host like so.