Create embedded extension
Create a new Single View Application (MyViewController)
Find the ‘+’ sign ‘Add a target’
Click the iMessage Extension and give it a name (MyMessageExtension)
Go ahead and Activate the scheme
Should now see a new target representing your extension.
If you run you should now see
Add the navigate back to host action
Add a button and outlet action to so when pressed extension redirects back to host.
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.
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
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.
Or do as follows
Move your mouse over ‘Information Property List’ until you see a plus sign form on the right. Click it.
Then start typing URL types and then hit return
Then click the arrow pointing right, beside URL types, to expand it
Click the right pointing arrow again, this time on Item 0 and add your extension value there.
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
Then expand the arrow to the left of your scheme and double click the value column to enter your value there
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
and you end up here (you can add a label to your host application if you want to verify you made it back.
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
While extension can be
Also the thing you want to look for when hooking these things up is this
You want to see the extension embedded in the host like so.
Jun 14, 2017 @ 12:58:31
In your URL object, shouldn’t it point to extensionhost://, and not “extension://” ?